#!python

# print fix
import sys
import os
try:
  Print = eval("print") # python 3.0 case
except SyntaxError:
  try:
    D = dict()
    exec("from __future__ import print_function\np=print", D)
    Print = D["p"] # 2.6 case
    del D
  except SyntaxError:
    del D
    def Print(*args, **kwd): # 2.4, 2.5, define our own Print function
      fout = kwd.get("file", sys.stdout)
      w = fout.write
      if args:
        w(str(args[0]))
        sep = kwd.get("sep", " ")
        for a in args[1:]:
          w(sep)
          w(str(a))
      w(kwd.get("end", "\n"))
      
      
def get_project_root(path):
	if os.path.exists(os.path.join(path,"remake.conf")):
		return path
	else:
		if path == "/":
			return ""
		else:
			return get_project_root(os.path.dirname(path))

try:
	# blender 2.4x stuff
	import Blender
	paths = Blender.GetPaths(1)
	for f in paths:
		if f!="":
			Print("dependency: %s" % f)
except:
	# blender 2.5x stuff
	import bpy
	import os.path
	import re
	# blender 2.56 & 2.57 crashes when using "bpy.utils.blend_paths(1)", 
	# so we forced to fallback to relative paths:
	paths = bpy.utils.blend_paths(0)
	outputlist = []
	projectroots = []
	
	#projectroot = os.environ['PROJECTROOT']
	#if projectroot == '':
	#	Print("No PROJECTROOT variable defined!")
	#	sys.exit(1)
	#renderdir=os.path.join(projectroot,'render')
	
	for f in paths:
		if f!="":
			
			# Blender 2.58 treats paths starting with "///" well,
			# but bpy.path.abspath() converts such path to absolute.
			# We need to avoid that.
			f=re.sub('^//+','//',f)
			
			f=os.path.abspath(bpy.path.abspath(f))
			
			projectroot = ""
			for r in projectroots:
				if f.startswith(os.path.join(r,"render")):
					projectroot = r
					break
			if projectroot=="":
				projectroot = get_project_root(os.path.dirname(f))
				if projectroot != "":
					projectroots.append(projectroot)
				
			if projectroot!="" and f.startswith(os.path.join(projectroot,"render")):
				
				renderdir = os.path.join(projectroot,"render")
				
				# f = /projectroot/render/path.ext.png/file000x.png
				# or
				# f = /projectroot/render/path.ext.png
			
				f2 = f.replace(renderdir,'',1)[1:]
				# f2 = path.ext.png/file000x.png
				# or
				# f2 = path.ext.png
			
				f2 = os.path.splitext(f2)[0]
				f2 = os.path.join(projectroot,f2)
			
				# f2 = /projectroot/path.ext.png/file000x
				# or
				# f2 = /projectroot/path.ext
				
				if not os.path.exists(f2):
					# f2 = /projectroot/path.ext.png/file000x
				
					f = os.path.dirname(f)
					# f = /projectroot/render/path.ext.png
				
					if not f in outputlist:
						outputlist.append(f)

				else:
					# f2 = /projectroot/path.ext
				
					if not f in outputlist:
						outputlist.append(f)

			else:
				if not f in outputlist:
					outputlist.append(f)
	
	for f in outputlist:
		Print("dependency: %s" % f)
