#!/usr/bin/perl -w
use Getopt::Long;


#
# Command-line options.
#
my $TODO    = 1; # Defaults to 'true'.
my $verbose = 0; # Defaults to 'flase'.
my $namespace ='';
my $debug = 0;
my $stdout = 0;
GetOptions ( 'TODO!'  => \$TODO, 'v|verbose' => \$verbose,
		'n|namespace=s' => \$namespace, 'd|debug' => \$debug,
		'stdout' =>  );

#
#  Obtain the author, class and namespace.
#
my ($author,$class);
while( $author = <> ) { last if $author =~ /\S/ }
while( $class  = <> ) { last if $class  =~ /\S/ }

my $class_definition = $class;
$class =~ s/\s*:.*//;
my @class_fields = split( / /, $class);
$class = pop @class_fields;


unless ($namespace ne '') {
	print "DEBUG: OS = '$^O'\n" if ($debug);
	print "DEBUG: OS recognized?... " if ($debug);
	if ($^O eq 'linux') {
		chomp($author);
		chomp($class);
		chomp($class_definition);

		print "yes\n" if ($debug);
		my @path = split("/", $ENV{PWD});
		$namespace = pop @path;


	} elsif ($^O eq 'MSWin32') {
		print "yes\n" if ($debug);
		my $path = `cd`;

		chomp($path);
		$path =~ s/[\r\f]+//g;
		$author =~ s/[\t\f]+//g;
		$class  =~ s/[\r\f]+//g;
		$class_definition =~ s/[\r\f]+//g;

		print "DEBUG: PATH = '$path'\n" if ($debug);
		my @path = split(/\\/, $path);
		$namespace = pop @path;

	} else {
		print "no\n" if ($debug);
		die "OS not recognized.  Expected 'linux' or 'windows'\n".
                    "The OS you are running is '$^O'\n".
		    "Contanct dcarrera\@math.toroto.edu for bug reprots.\n";
	}
}
print "DEBUG: Class Definition = $class_definition\n" if ($debug);
print "DEBUG: Class = '$class'\n" if ($debug);
print "DEBUG: NAMESPACE = '$namespace'\n" if ($debug);
print "DEBUG: VERBOSITY = $verbose\n" if ($debug);

if ($verbose) {
	open(OUT,"| tee $class.cs") or die "ERROR: Could not open pipe to 'tee'\n";
	select OUT;
	$| = 1;   # this sets output to the last selected handle to be unbuffered
} else {
	if ( open(OUT,">$class.cs") ) {
		select OUT
	} else {	
		warn "WARNING: Could not open $class.cs for writing\n".
		     "         sending output to STDOUT\n";
	}
}
#
#  Print the header.
#

print <<EOH
//
// $namespace.$class
//
// Author:
//   stubbed out by $author
//
// (C) 2002 Ximian, Inc
//

namespace $namespace
{
        public $class_definition
        {
EOH
;


#
#  Read all the subsequent lines into an array.
#  If a line ends in "\", It'll be interpreted as the text continuing
#  onto the next line.
$text = '';
$append='';
while(<>) {
	chomp;
	if (/\\$/) {
		chop;
		$text .= $_;
	} else {
		$text .= $_;
		push @lines, $text;
		$text = '';
	}
}


#
#  Print the properties, classes, etc.
#
$exception = "throw new NotImplementedException ();";
foreach $line (@lines) {
	$line =~ s/\s+/ /g; 	     # catenate spaces to one space.

	unless ($line =~ /\S/) { # skip blank lines
		print "\n";
		next;
	}
	if ($line =~ m[^\s*//]) {    # C# comments.
		print "\t\t$line\n";
		next;
	}
	if ($line =~ m[^=]) {	     # =public, =protected, etc
		if ($line =~ m[^=(.+)]) { $append = "$1 " }
		else		 	{ $append = '' }
		next;
	}

	#
	# If we get this far than we have real code.
	#
	if ( $line =~ s/{(.*)}\s*// ) {	
		$contents = $1;
		print "\t\t[MonoTODO]\n" if $TODO;
		print "\t\t$append$line\n\t\t{\n";
		if ($contents =~ /g/i) { 
			print "\t\t\tget { ", ($TODO? $exception:'')," }\n" 
		}
		if ($contents =~ /s/i) { 
			print "\t\t\tset { ", ($TODO? $exception:'')," }\n" 
		}
		if ($contents =~ /a/i) {
			print "\t\t\tadd { ", ($TODO? $exception:'')," }\n" 
		}
		if ($contents =~ /r/i) {
			print "\t\t\tremove { ", ($TODO? $exception:'')," }\n" 
		}
		print "\t\t}\n";
			
	} else {
		print "\t\t[MonoTODO]\n" if $TODO;
		print "\t\t$append$line\n".
			  "\t\t{\n".
			  "\t\t\t".($TODO?$exception:'')."\n".
			  "\t\t}\n";
	}
}

print "\t}\n}\n";
