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_file_concat(class_command):
	def __init__(self, parameters): class_command.__init__(self, parameters)
	def compile_unix(self): return ("cat %s > %s" % (" ".join(self.parameters["paths-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"]))

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

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



def main():
	
	dirs = {
		"source": "source",
		"temp": "temp",
		"build": "build",
	}
	
	parts_logic_server = [
		os.path.join(dirs["source"],"logic","server","misc.php"),
		os.path.join(dirs["source"],"logic","server","data.php"),
		os.path.join(dirs["source"],"logic","server","settings.php"),
		os.path.join(dirs["source"],"logic","server","table.php"),
		os.path.join(dirs["source"],"logic","server","sql.php"),
	]
	
	parts_content = [
		os.path.join(dirs["source"],"content","draft.html.php"),
		os.path.join(dirs["source"],"content","meta.html.php"),
		os.path.join(dirs["source"],"content","introduction.html.php"),
		os.path.join(dirs["source"],"content","preface.html.php"),
		os.path.join(dirs["source"],"content","key_parameters.html.php"),
		os.path.join(dirs["source"],"content","toc.html.php"),
		os.path.join(dirs["source"],"content","basics.html.php"),
		os.path.join(dirs["source"],"content","phonology_and_orthography.html.php"),
		os.path.join(dirs["source"],"content","grammar.html.php"),
		os.path.join(dirs["source"],"content","personal_pronouns.html.php"),
		os.path.join(dirs["source"],"content","correlatives.html.php"),
		os.path.join(dirs["source"],"content","conjugation.html.php"),
		os.path.join(dirs["source"],"content","infinite_verbforms.html.php"),
		os.path.join(dirs["source"],"content","modal_verbs.html.php"),
		os.path.join(dirs["source"],"content","tempora_and_modi.html.php"),
		os.path.join(dirs["source"],"content","negation.html.php"),
		os.path.join(dirs["source"],"content","declension.html.php"),
		os.path.join(dirs["source"],"content","numeri.html.php"),
		os.path.join(dirs["source"],"content","cases.html.php"),
		os.path.join(dirs["source"],"content","definiteness.html.php"),
		os.path.join(dirs["source"],"content","attributes.html.php"),
		os.path.join(dirs["source"],"content","sentence_structure.html.php"),
		os.path.join(dirs["source"],"content","vocabulary.html.php"),
		os.path.join(dirs["source"],"content","principles.html.php"),
		os.path.join(dirs["source"],"content","word_functions.html.php"),
		os.path.join(dirs["source"],"content","adpositions.html.php"),
		os.path.join(dirs["source"],"content","pronouns.html.php"),
		os.path.join(dirs["source"],"content","dictionary.html.php"),
		os.path.join(dirs["source"],"content","examples.html.php"),
		os.path.join(dirs["source"],"content","example_sentences.html.php"),
		os.path.join(dirs["source"],"content","example_texts.html.php"),
	]
	
	parts_data = [
		os.path.join(dirs["source"],"data","phonology_and_orthography.json"),
		os.path.join(dirs["source"],"data","personal_pronouns.json"),
		os.path.join(dirs["source"],"data","word_functions.json"),
		os.path.join(dirs["source"],"data","timeforms.json"),
		os.path.join(dirs["source"],"data","adpositions.json"),
		os.path.join(dirs["source"],"data","correlatives.json"),
	]
	
	parts_logic_client = [
		os.path.join(dirs["source"],"logic","client","tools.js"),
		os.path.join(dirs["source"],"logic","client","table.js"),
		os.path.join(dirs["source"],"logic","client","toc.js"),
		os.path.join(dirs["source"],"logic","client","main.js"),
	]
	
	parts_style = [
		os.path.join(dirs["source"],"style","functions.less"),
		os.path.join(dirs["source"],"style","theme.less"),
		os.path.join(dirs["source"],"style","common.less"),
		os.path.join(dirs["source"],"style","concrete.less"),
	]
	
	rules = []
	rules.append(
		class_rule(
			"all",
			[
				os.path.join(dirs["build"],"draft.html"),
				os.path.join(dirs["build"],"draft.css"),
				os.path.join(dirs["build"],"draft.js"),
				os.path.join(dirs["build"],"logo.svg"),
				os.path.join(dirs["build"],"favicon.png"),
			],
			[],
			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["build"],"draft.html"),
			parts_data + parts_logic_server + parts_content,
			[
				class_command_log({
					"message": "composing document ..."
				}),
				class_command_directory_create({
					"path": dirs["build"],
				}),
				class_command_php_compile({
					"path-from": os.path.join(dirs["source"],"content","draft.html.php"),
					"path-to": os.path.join(dirs["build"],"draft.html"),
				}),
			]
		)
	)
	rules.append(
		class_rule(
			os.path.join(dirs["build"],"draft.js"),
			parts_logic_client,
			[
				class_command_log({
					"message": "composing client-logic ..."
				}),
				class_command_directory_create({
					"path": dirs["build"],
				}),
				class_command_file_concat({
					"paths-from": parts_logic_client,
					"path-to": os.path.join(dirs["build"],"draft.js"),
				}),
			]
		)
	)
	rules.append(
		class_rule(
			os.path.join(dirs["build"],"logo.svg"),
			[os.path.join(dirs["source"],"media","logo.svg")],
			[
				class_command_log({
					"message": "copying logo ..."
				}),
				class_command_directory_create({
					"path": dirs["build"],
				}),
				class_command_file_copy({
					"path-from": os.path.join(dirs["source"],"media","logo.svg"),
					"path-to": os.path.join(dirs["build"],"logo.svg"),
				}),
			]
		)
	)
	rules.append(
		class_rule(
			os.path.join(dirs["build"],"favicon.png"),
			[os.path.join(dirs["source"],"media","folksprak_16x16.png")],
			[
				class_command_log({
					"message": "copying favicon ..."
				}),
				class_command_directory_create({
					"path": dirs["build"],
				}),
				class_command_file_copy({
					"path-from": os.path.join(dirs["source"],"media","folksprak_16x16.png"),
					"path-to": os.path.join(dirs["build"],"favicon.png"),
				}),
			]
		)
	)
	rules.append(
		class_rule(
			os.path.join(dirs["build"],"draft.css"),
			parts_style,
			[
				class_command_log({
					"message": "compiling style ..."
				}),
				class_command_directory_create({
					"path": os.path.join(dirs["temp"]),
				}),
				class_command_file_concat({
					"paths-from": parts_style,
					"path-to": os.path.join(dirs["temp"],"draft.less"),
				}),
				class_command_directory_create({
					"path": os.path.join(dirs["build"]),
				}),
				class_command_lessc_compile({
					"path-from": os.path.join(dirs["temp"],"draft.less"),
					"path-to": os.path.join(dirs["build"],"draft.css"),
				}),
			]
		)
	)
	
	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()