#!/usr/bin/env python
# Potato Assembler Driver
# usage
#	./pad assembly.asm
#	to speficy memory size, either use the -p flag, or place the number in the .padconfig file
import sys
import assembler
import padformemory
import preprocessor
from os.path import splitext
from os.path import dirname

def main():
	# check for size flag
	if sys.argv[1] == "-p":
		size = int(sys.argv[2])
		filestring = sys.argv[3]
	else:
		filestring = sys.argv[1]
		configfile = open(dirname(sys.argv[0]) + "/.padconfig", "r")
		configoptions = configfile.readlines()
		size = int(configoptions[0])

	# prepare file names for tools
	filenameparts = splitext(filestring)
	preprocessfile = filestring
	assemblyfile = filenameparts[0] + ".pp"
	objectfile = filenameparts[0] + ".o"

	print "Preprocessing " + filenameparts[0] + filenameparts[1] + "",
	preprocessor.parse(preprocessfile, assemblyfile)
	print "\nAssembling " + filenameparts[0] + ".pp" + "",
	assembler.parse(assemblyfile, objectfile)
	print "\nPadding " + filenameparts[0] + ".o for memory of size " + str(size),
	padformemory.pad(objectfile, size, dirname(sys.argv[0]))

if __name__ == '__main__':
	main()
