#!/usr/bin/perl

# htoswig.
# Converts a header file to SWIG format as follows :
#     - appends a module name
#     - Converts #include to %{, %}
#     - Runs the header through the C preprocessor
#     - produces a .i file
#

if (scalar(@ARGV) != 2) {
	print ("Usage : htoswig header module\n");
	exit();
}

$infile = $ARGV[0];
$module = $ARGV[1];

print "Input = $infile\n";
print "Module = $module\n";

# Munge the header file by replacing a few key things

open(HEADER,$infile);
open(MODULE,">$module.swig.h");

print MODULE "%module $module\n";

while (<HEADER>) {
	if (/#include/) {
	    print MODULE "%ifndef SWIG\n";
	    print MODULE "$_";
	    print MODULE "%endif\n";
	} else {
	    print MODULE "$_";
	}
    }
close(HEADER);
close(MODULE);

# Now run the module file through the C preprocessor

`cc -E $module.swig.h > $module.ii`;

`rm $module.swig.h`;

open(MUNGE,"$module.ii");
open(MODULE,">$module.i");

while(<MUNGE>) {
    if (/\A\s*\n/) {
#	print MODULE "$_";
    } else {
	print MODULE "$_";
    }
}
close(MUNGE);
close(MODULE);

`rm $module.ii`;
	

