#!/usr/bin/python2
# Used to create symlink for git hooks 
from sys import argv, exit
from getopt import getopt
from re import match, sub
import os

def print_help():
    print "Help screen"

def parse_stack_file(stackfile):
    f = open(stackfile, "r")
    rd = f.read().splitlines()
    for line in rd:
        if match("^\s*typedef.*$", line) and match("^.*stack_t.*$", line):
            return line

def create_board_interface(stack_define, outfile):
    f = open(outfile, "w")
    f.write("/* Stack include file */\n\n")
    f.write("#ifndef STACK_H\n")
    f.write("#define STACK_H\n\n")
    f.write(stack_define + "\n")
    f.write("\n#endif /* STACK_H */\n")

def main(argv):
    options, remainder = getopt(argv, "hi:o:", ["help", "infile=", "outfile="])
    infile = ""
    outfile = ""
    if len(options) < 2:
        print_help()
        exit(-1)
    for opt, arg in options:
        if opt in ("-h", "--help"):
            print_help()
            exit(0)
        elif opt in ("-i", "--infile"):
            infile = arg
        elif opt in ("-o", "--outfile"):
            outfile = arg
    if (len(infile) == 0) or (len(outfile) == 0):
        print_help()
        exit(-1)
    create_board_interface(parse_stack_file(infile), outfile)

if __name__ == "__main__":
    main(argv[1:])
