#!/usr/bin/env python
# -*- coding: utf-8 -*-	
#
# This is a filter to convert Scol pkg code
# into something doxygen can understand.
# Copyright (C) 2010 Bastien Bourineau from Basti Grembowietz code
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# ------------------------------------------------------------------------- 
#

import getopt          # get command-line options
import os.path         # getting extension from file
import string          # string manipulation
import sys             # output and stuff
import re              # for regular expressions

## stream to write output to
outfile = sys.stdout

# dumps the given file
def dump(filename):
	f = open(filename)
	r = f.readlines()
	f.close()
	for s in r:
		sys.stdout.write("."), 
		sys.stdout.write(s)

def readstruct(matchobj):
	p = matchobj.group(2)
#	buff = re.sub(r'([a-zA-Z_0-9]*)\s*:\s*([a-zA-Z_0-9]+)\s*(![a-zA-Z_0-9])+,*', r'\1 \2;', p, 65535, re.DOTALL)
	buff = re.sub(r'([a-zA-Z_0-9]*)\s*:\s*([a-zA-Z_0-9\[\]\s]+)\s*,*', r'\1 (\2);', p, 65535, re.DOTALL)
	return 'struct ' + matchobj.group(1) + '{\n' + buff + '\n};\ntypedef struct ' + matchobj.group(3) + ' ' + matchobj.group(1) + ';'

# filters .pkg-files
def filterPKG(filename):
	global outfile ## get global variable
	f = open(filename)
	r = f.read()
	outfile.write("\n// -- processed by [filterPKG] --\n") 

	out = re.sub(r'fun\s+([a-zA-Z_0-9]*)\s*\((.*?)\)\s*[=]{1}\s*(.*?)[;]{2}', r'\1(\2){\n\3;\n}', r, 65535, re.DOTALL)
	out = re.sub(r'typeof\s+([a-zA-Z_0-9]*)\s*[=]{1}\s*([a-zA-Z_0-9]*)\s*[;]{2}', r'typeof \1=\2;', out, 65535, re.DOTALL)
	out = re.sub(r'typeof\s+([a-zA-Z_0-9]*)\s*[=]{1}\s*([a-zA-Z_0-9\[\]\s]+)\s*[;]{2}', r'typeof \1=\2;', out, 65535, re.DOTALL)
	out = re.sub(r'proto\s+([a-zA-Z_0-9]*)\s*[=]{1}\s*([a-zA-Z_0-9\[\]\s]+)\s*[;]{2}', r'proto \1=\2;', out, 65535, re.DOTALL)
	out = re.sub(r'var\s+([a-zA-Z_0-9]*)\s*[=]{1}\s*(.*?)\s*[;]{2}', r'var \1=\2;', out, 65535, re.DOTALL)
	
	out = re.sub(r'struct\s+([a-zA-Z_0-9]*)\s*[=]{1}\s*\[(.*?)\]\s*([a-zA-Z_0-9]*)\s*[;]{2}', readstruct, out, 65535, re.DOTALL)
	
# outfile.write("}\n") #write end func

#	outfile.write("}")  # for ending class
	outfile.write(out + "\n// -- [/filterPKG] --\n") 

## main filter-function ##
##
## this function decides whether the file is
## (*) a pkg file
##
## and calls the appropriate function

def filter(filename, out=sys.stdout):
	global outfile
	outfile = out

	try:
		root, ext = os.path.splitext(filename)
		if (ext.lower() ==".pkg"):
			## if it is a module call filterBAS
			filterPKG(filename)
		else:
			## if it is an unknown extension, just dump it
			dump(filename)

		sys.stderr.write("OK\n") 
	except IOError as e:
		sys.stderr.write(e[1]+"\n")

## main-entry ##
################

if len(sys.argv) != 2:
	print ("usage: ", sys.argv[0], " filename")
	sys.exit(1)

# Filter the specified file and print the result to stdout
filename = sys.argv[1] 
filter(filename)
sys.exit(0)