#!/usr/bin/env python

import sys, re, os
from rapyd.compiler import parse_file, finalize_source

if __name__ == "__main__":
	orig_dir = os.getcwd()
	base_dir, orig_file = os.path.split(sys.argv[1])
	filename = orig_file.rsplit('.', 1)[0] # remove the extension

	if base_dir:
		os.chdir(base_dir)
	with open(filename+'.tmp', 'w') as output:
		handler = parse_file(orig_file, output)

	with open(filename+'.tmp', 'r') as fp:
		source = fp.read()
	output = finalize_source(source, handler)
	with open(filename + '.js', 'w') as new_file:
		new_file.write(output)
	os.chdir(orig_dir)
