tfname = "syscall_numbers.h.template"
dfname = "syscall_numbers.txt"
ofname = "syscall_numbers.h"

header = """
/*
 ***************************************************************
 * This file is autogenerated. Modifications to its contents   *
 * will not be persistent. Modify the template source instead. *
 ***************************************************************
 */ 
"""

fstring = "#define %s%s \t(%d)"

tsym = "<~syscall-list~>"

tftext = open(tfname).read()
dflist = open(dfname).readlines()
ofile = open(ofname, 'w')

# remove comments
dflist = [dfline[:dfline.index('#')].replace('\n','') if not dfline.find('#') == -1 else dfline.replace('\n','') for dfline in dflist]

tempdflist = dflist[:]
dflist = []
for dfline in tempdflist:
	if not dfline.isspace() and not dfline == '':
		dflist.append(dfline)

maxlen = max([len(s) for s in dflist])

rtext = '\n'.join([fstring % (s, " "*(maxlen - len(s)), i) for i,s in enumerate(dflist)])

tftext = tftext.replace(tsym, rtext)

ofile.write(header + tftext)

ofile.close()
