#!/usr/bin/python

import os
import string
import sys
import re
import glob
import scriptutil

#from DebugMessage import stdMsg, dbgMsg, errMsg, setDebugging

#SrcPath="/cygdrive/d/Home/cygwin/gruberg/darkmod"
#DstPath="/cygdrive/d/Home/cygwin/gruberg/darkmod_reorg"
SrcPath="/home/sparhawk/darkmod"
DstPath="/home/sparhawk/darkmod_reorg"

material_list = {}

class MaterialLine:
	def __init__(self):
		self.LineNumber = -1
		self.Type = 'l'
		self.Indentation = 0
		self.Text = ""

	def __repr__(self):
		return "[ " + self.LineNumber.__repr__() + ", '" + self.Type + "', " + self.Indentation.__repr__() + ", '" + self.Text + "' ]"

class Material:
	def __init__(self, name):
		self.Filename = name
		self.Modified = 0
		self.Buffer = None
		self.Line = []
		self.CacheFile()

	def __repr__(self):
		return "[ '"+self.Filename+"', "+self.Modified.__repr__()+", "+len(self.Line).__repr__()+" ]"

	def CacheFile(self):
		f = open(SrcPath+"/"+self.Filename)
		self.Buffer = f.read()
		f.close()

		b = self.Buffer.split("\n")
		last = None
		r = MaterialLine()
		ind = 0
		prevind = 0
		nr = 0

		for l in b:
			t = l.split("\r")
			if(len(t) > 1):
				l = t[0]

			s = l.strip()
			if s[0:1] == '{':
				ind = ind + 1
			elif s[0:1] == '}':
				ind = ind - 1
			elif s[0:2] == '//':
				r.Type = 'c'

			nr = nr + 1
			r.Indentation = ind
			r.Text = l
			r.LineNumber = nr

			# Check if we have found a material name
			if ind == 1 and prevind == 0:
				last.Type = 'n'

			self.Line.append(r)
			last = r
			r = MaterialLine()
			prevind = ind

		return

class MaterialCheck:
	def __init__(self):
		self.MaterialFile = {}
		self.CacheMaterials()

	def CacheMaterials(self):
		print "Caching material files..."
		name = glob.glob(SrcPath+"/materials/*")
		for i in name:
			i = i.split(SrcPath+"/")[1]
			t = i.split("replacements.txt")
			if(len(t) > 1):
				continue

			t = i.split("gildoran_notes.txt")
			if(len(t) > 1):
				continue

			#print i
			self.MaterialFile[i] = Material(i)
		return

	def CheckMaterials(self):
		for n in self.MaterialFile:
			mprint = 0
			m = self.MaterialFile[n]
			#print "\n\n"+n
			for l in m.Line:
				if(l.Type == 'c'):
					continue

				if(l.Indentation > 0):
					t = l.Text.split("/common/")
					if(len(t) > 1):
						continue
					
					t = l.Text.split("/lights/")
					if(len(t) > 1):
						continue

					t = l.Text.split("/hell/")
					if(len(t) > 1):
						continue

					t = l.Text.split("/glass/")
					if(len(t) > 1):
						continue

					t = l.Text.split("/monsters/")
					if(len(t) > 1):
						continue

					t = l.Text.split("/sfx/")
					if(len(t) > 1):
						continue

					t = l.Text.split("/editor/")
					if(len(t) > 1):
						continue

					s = "models/"
					t = l.Text.strip().split(s)
					if(len(t) == 1):
						s = "textures/"
						t = l.Text.strip().split(s)

					if(len(t) > 1):
						tex = s+t[1]
						t = tex.split(",")
						if(len(t) > 1):
							tex = t[0]

						t = tex.split("(")
						if(len(t) > 1):
							tex = t[0]

						t = tex.split(")")
						if(len(t) > 1):
							tex = t[0]

						t = tex.split("//")
						if(len(t) > 1):
							tex = t[0].strip()

						t = tex.split(" ")
						if(len(t) > 1):
							tex = t[0].strip()

						t = tex.split("\t")
						if(len(t) > 1):
							tex = t[0].strip()

						t = tex.split(".")
						ext = "*"
						wildcard = 1
						if(len(t) > 1):
							if(t[1][0:1] == '.'):
								ext = t[1]
								wildcard = 0

						tex = t[0]
						#print tex+" ."+ext

						dds = 0
						fl = glob.glob(SrcPath+"/"+tex+"."+ext)
						if(len(fl) == 0):
							dds = 1
							fl = glob.glob(SrcPath+"/dds/"+tex+"."+ext)

						if(len(fl) == 0):
							if(mprint == 0):
								print "\n"+n
								mprint = 1

							if(wildcard == 1):
								print "Missing:"+l.LineNumber.__repr__()+": "+tex
							else:
								print "Missing:"+l.LineNumber.__repr__()+": "+tex+"."+ext
						else:
							if(dds == 1):
								if(mprint == 0):
									print "\n"+n
									mprint = 1

							#if(wildcard == 1):
								#print "DDS only:"+l.LineNumber.__repr__()+": "+tex
							#else:
								#print "DDS only:"+l.LineNumber.__repr__()+":"+tex+"."+ext
							

		return

def Usage():
	print "USAGE: materialcheck mod_root"
	sys.exit(1)

if __name__ == '__main__':
	if len(sys.argv) <= 1:
		Usage()

	SrcPath = sys.argv[1]
	mat = MaterialCheck()
	mat.CheckMaterials()

	sys.exit(0)