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


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

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

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

unless ($namespace ne '') {
	my @pwd = split("/", $ENV{PWD});
	$namespace = pop @pwd;
}

if ($verbose) {
	open(OUT,"| tee $class.cs");
	select OUT;
	$| = 1;   # this sets output to the last selected handle to be unbuffered
	select STDOUT;
} else {
	open(OUT,">$class.cs");
}
#
#  Print the header.
#

print OUT <<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 OUT "\n";
		next;
	}
	if ($line =~ m[^\s*//]) {    # C# comments.
		print OUT "\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 OUT "\t\t[MonoTODO]\n" if $TODO;
		print OUT "\t\t$append$line\n\t\t{\n";
		if ($contents =~ /g/i) { 
			print OUT "\t\t\tget { ", ($TODO? $exception:'')," }\n" 
		}
		if ($contents =~ /s/i) { 
			print OUT "\t\t\tset { ", ($TODO? $exception:'')," }\n" 
		}
		print OUT "\t\t}\n";
			
	} else {
		print OUT "\t\t[MonoTODO]\n" if $TODO;
		print OUT "\t\t$append$line\n".
			  "\t\t{\n".
			  "\t\t\t".($TODO?$exception:'')."\n".
			  "\t\t}\n";
	}
}

print OUT "\t}\n}\n";
close(OUT);
