#!/usr/bin/env python
#-------------------------------------------------------------------------------
#
#	Generate language file
#
#-------------------------------------------------------------------------------

usage = "Usage: genlang modid|assetkey langkey srcfile"

def fail(s):
	sys.stderr.write(s + "\n")
	sys.exit(1)

def badusage(s):
	fail(usage)

import os, sys

def main():
	args = sys.argv[1:]
	if len(args) != 3:
		badusage()
	modid = args[0]
	#modpackage = args[1]
	langkey = args[1].replace(".", "_")
	srcpath = args[2]
	assetkey = modid.lower()
	#langkey = modpackage.lower().replace(".", "_")
	#langkey = modid.lower()
	dstdir = os.path.join("src", "resources", "assets", assetkey, "lang")
	dstpath = os.path.join(dstdir, "en_US.lang")
	if os.path.exists(srcpath):
		f = open(srcpath)
		if not os.path.exists(dstdir):
			os.makedirs(dstdir)
		g = open(dstpath, "w")
		prefix = ""
		for line in f:
			line = line.strip()
			if line and not line.startswith("#"):
				if line.endswith(":"):
					if "*" in line:
						pattern = line[:-1]
						prefix, _, suffix = pattern.partition("*")
					else:
						prefix = line[:-1].strip() + "."
						if prefix in ("tile.", "item."):
							suffix = ".name"
						else:
							suffix = ""
				else:
					name, _, title = line.partition("=")
					name = name.strip()
					if "/" in name:
						parts = name.split("/")
						key = "%s_%s" % (langkey, "_".join(parts[:-1]))
						name = parts[-1]
					else:
						key = langkey
					g.write("%s%s:%s%s=%s\n" % (prefix, key, name, suffix, title.strip()))
		g.close()

main()
