#!/usr/bin/python

#
# eek!
#

import os
import sys
import re

include_regexp = re.compile('^#[ ]*include[ ]*[\"<](.*)[\">]')

def parse_filename(filename, include_paths):
	if filename[0] == '/':
		return filename
	for path in include_paths:
		p = path + filename
		if os.path.isfile(p):
			return p
	return ''

def parse_include(filename, include_paths, include_list):
	filenamefull = parse_filename(filename, include_paths)
	if not filenamefull:
		return
	include = open(filenamefull, "r")
	for line in include.readlines():
		include_line = include_regexp.match(line)
		if not include_line:
			continue
		include_next = include_line.group(1)
		key = filename + ' -> ' + include_next + ';'
		k = include_list.has_key(key)
		if not k:
			include_list[key] = 'true';
			print key
			parse_include(include_next, include_paths, include_list)
	include.close()

# comment before www ;-)
# yah.. i don't know python, so deal with it!
#
include_paths = [ sys.argv[2] ]
include_list = {}

parse_include(sys.argv[1], include_paths, include_list)
