#!/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_board_file(boardfile):
    f = open(boardfile, "r")
    rd = f.read().splitlines()
    ret = []
    for line in rd:
        if match("^.*struct driver.*$", line):
            line = sub("^.*struct driver", "", line).strip()
            line = sub(" .*$","",line).strip()
            if not line in ret:
                ret.append(line)
    return ret

def create_board_interface(device_list, outfile):
    f = open(outfile, "w")
    f.write("/* Board devices include file */\n\n")
    f.write("#ifndef BOARD_DEVICES_H\n")
    f.write("#define BOARD_DEVICES_H\n\n")
    for device in device_list:
        f.write("extern const struct driver " + device + ";\n")
    f.write("\n#endif /* BOARD_DEVICES_H */\n")

def main(argv):
    options, remainder = getopt(argv, "hb:o:", ["help", "boardfile=", "outfile="])
    boardfile = ""
    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 ("-b", "--boardfile"):
            boardfile = arg
        elif opt in ("-o", "--outfile"):
            outfile = arg
    if (len(boardfile) == 0) or (len(outfile) == 0):
        print_help()
        exit(-1)
    create_board_interface(parse_board_file(boardfile), outfile)

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