import os
import sys


class class_rule(object):
	
	def __init__(self, name, dependencies, commands, phony = False):
		self.name = name
		self.dependencies = dependencies
		self.commands = commands
		self.phony = phony
		
	
	def __str__(self):
		output = ""
		output += "%s: %s\n" % (self.name, " ".join(self.dependencies))
		for command in self.commands:
			output += ("\t@ %s\n" % (command.compile_unix()))
		if (self.phony): output += (".PHONY: %s\n" % self.name)
		return output
		
	

class class_command(object):
	def __init__(self, parameters): self.parameters = parameters
	def compile_unix(self): raise NotImplementedError
	def compile_ant(self): raise NotImplementedError

class class_command_log(class_command):
	def __init__(self, parameters): class_command.__init__(self, parameters)
	def compile_unix(self): return ("echo -e '-- %s'" % (self.parameters["message"]))

class class_command_directory_create(class_command):
	def __init__(self, parameters): class_command.__init__(self, parameters)
	def compile_unix(self): return ("mkdir --parents %s" % (self.parameters["path"]))

class class_command_file_copy(class_command):
	def __init__(self, parameters): class_command.__init__(self, parameters)
	def compile_unix(self): return ("cp --recursive %s %s" % (self.parameters["path-from"], self.parameters["path-to"]))

class class_command_directory_remove(class_command):
	def __init__(self, parameters): class_command.__init__(self, parameters)
	def compile_unix(self): return ("rm --recursive --force %s" % (self.parameters["path"]))

class class_command_image_convert(class_command):
	def __init__(self, parameters): class_command.__init__(self, parameters)
	def compile_unix(self): return ("convert %s %s" % (self.parameters["path-from"], self.parameters["path-to"]))

class class_command_latex_compile(class_command):
	def __init__(self, parameters): class_command.__init__(self, parameters)
	def compile_unix(self): return ("xelatex -output-directory=%s %s > /dev/null" % (self.parameters["output"], self.parameters["path"]))



def main():
	
	dirs = {
		"source": "source",
		"temp": "temp",
		"build": "build",
	}
	
	texs = [
		os.path.join(dirs["source"],"tex","folksprak.tex"),
		os.path.join(dirs["source"],"tex","packages.tex"),
		os.path.join(dirs["source"],"tex","settings.tex"),
		os.path.join(dirs["source"],"tex","meta.tex"),
		os.path.join(dirs["source"],"tex","commands.tex"),
		os.path.join(dirs["source"],"tex","preface.tex"),
		os.path.join(dirs["source"],"tex","grammar.tex"),
		os.path.join(dirs["source"],"tex","phonology_and_orthography.tex"),
		os.path.join(dirs["source"],"tex","conjugation.tex"),
		os.path.join(dirs["source"],"tex","declension.tex"),
		os.path.join(dirs["source"],"tex","attributes.tex"),
	]
	
	rules = []
	rules.append(
		class_rule(
			"all",
			[os.path.join(dirs["build"],"folksprak.pdf")],
			[],
			True
		)
	)
	rules.append(
		class_rule(
			"clean",
			[],
			[
				class_command_directory_remove({
					"path": dirs["temp"],
				}),
			],
			True
		)
	)
	rules.append(
		class_rule(
			"clear",
			["clean"],
			[
				class_command_directory_remove({
					"path": dirs["build"],
				}),
			],
			True
		)
	)
	rules.append(
		class_rule(
			os.path.join(dirs["temp"],"media","logo.pdf"),
			[os.path.join(dirs["source"],"media","logo.svg")],
			[
				class_command_log({
					"message": "converting logo ..."
				}),
				class_command_directory_create({
					"path": os.path.join(dirs["temp"],"media"),
				}),
				class_command_image_convert({
					"path-from": os.path.join(dirs["source"],"media","logo.svg"),
					"path-to": os.path.join(dirs["temp"],"media","logo.pdf"),
				}),
			]
		)
	)
	rules.append(
		class_rule(
			os.path.join(dirs["temp"],"folksprak.pdf"),
			texs + [os.path.join(dirs["temp"],"media","logo.pdf")],
			[
				class_command_log({
					"message": "compiling document ..."
				}),
				class_command_directory_create({
					"path": dirs["temp"],
				}),
				class_command_latex_compile({
					"path": os.path.join(dirs["source"],"tex","folksprak.tex"),
					"output": os.path.join(dirs["temp"]),
				}),
				class_command_latex_compile({
					"path": os.path.join(dirs["source"],"tex","folksprak.tex"),
					"output": os.path.join(dirs["temp"]),
				}),
			]
		)
	)
	rules.append(
		class_rule(
			os.path.join(dirs["build"],"folksprak.pdf"),
			[os.path.join(dirs["temp"],"folksprak.pdf")],
			[
				class_command_directory_create({
					"path": os.path.join(dirs["build"]),
				}),
				class_command_file_copy({
					"path-from": os.path.join(dirs["temp"],"folksprak.pdf"),
					"path-to": os.path.join(dirs["build"],"folksprak.pdf"),
				}),
			]
		)
	)
	
	for rule in rules: print(rule)
	sys.stderr.write("\n[information]\tThe output of this script is meant to be redirected into a GNU-makefile\n")
	

main()