#!/usr/bin/perl
sub error{
    print STDERR $_[0];
    exit 0;
}

sub getstart{
#    print "getstart:$_[0]!$_[1]!\n";
    if ($_[0] =~ /^$_[1]\s+\b(\w\w\w \w\w\w\s{1,2}\d{1,2} \d\d:\d\d:\d\d \d\d\d\d)/){
	$auxdate = $1;
        open(PIPEUDATE, "date --date=\'$auxdate\' +%s |");
        read(PIPEUDATE, $udate, 20);
#      print "$_[1]:$auxdate:$udate:$_[2]\n";
        $_[3]=$auxdate;
	return 0 unless ($udate < $_[2]);
#        print "Date ok\n";
	return 1;
    }
    print "Couldn't find date\n";
    return 0;
}

sub getsubject{
#  print "Subject:!$_[0]!\n";
  while(<>){
      return 1 if (/^Subject:\s+$_[0]/);
      return 0 if (/^Subject:\s+/);
  }
  return 0;
}

sub skipmsg{
   while(<>){
       last if /$_[0]/;
   }
}

sub skipblankline{
    $_= <STDIN> || &error("unexpected eof\n");
    while (/^\s*\n$/){
         $_= <STDIN> || &error("unexpected eof\n");
     }
    $_[0] = $_;
}


sub skipmsgheader{
    while(<>){
	last if /^%\s*\w+/;
    }
}

sub parseid{

    $_[0] =~ y/A-Z/a-z/;
    

    $_[2] =~ y/A-Z/a-z/;

    $_[1] = ($_[0] =~ /^%\s*\b([\w|\xc0-\xff])[\w|\xc0-\xff]*\s+.*\s*\b([\w|\xc0-\xff]+)\s*:/ ?  
                       "$1.$2" : "");
 
    if (!$_[1]){
	$_[1] = ($_[0] =~ /^%\s*\b([\w|\xc0-\xff]+)\s*:/ ?  
                       "$1" : "");
        if (!$_[1]) {return 0};
    }

    $_[3] = ($_[2] =~ /^%\s*\b(\w)\w*\s+.*\s*\b(\w+)\s*:/ ?  
                       "$1.$2" : "");
    if (!$_[3]){
	$_[3] = ($_[2] =~ /^%\s*\b(\w+)\s*:/ ?  
                       "$1" : "");
    }

    $_[6] =~ y/A-Z/a-z/;
    if ($_[6] =~ /^%\s*cweb\s*\n$/){
	$_[7] = '.w';
        $_[5] = '';
    } elsif ($_[6] =~ /^%\s*noweb\s*\n$/){
	$_[7] = '.nw';
        $_[4] =~ y/A-Z/a-z/;
        if ($_[4] =~ /^%\s*(\w*)/){
	    $_[5] = ".$1";
	} else { return 0;}
    } else {
#	print "No web\n";
        $_[7] = '';
        $_[4] =~ y/A-Z/a-z/;
#	print "id->4:$_[4]\n6:$_[6]\n";
        if ($_[4] =~ /^%\s*(\w+)/){
	    $_[5] = ".$1";
	} else {return 0;}
    }
    return 1;
}


if ($#ARGV<5 || $#ARGV>6){
    print STDERR "Run as: \n";
    print STDERR '  extract.pl <start> <subj> <date> <dir> <cw> <num> [<web>]',"\n";
    print STDERR "   where: <start> string marking begin of new message\n";
    print STDERR "          <subj> is the subject for the msg\n";
    print STDERR "          <date> is the submission deadline (Unix time) \n";
    print STDERR "          <dir> is directory name for output\n";
    print STDERR "          <cw> identifies  coursework type \n";
    print STDERR "          <num> is the index inside the given <cw>\n";
    print STDERR "          <web> is any string, indicating literate programming\n";
    print STDERR "\n\nIt will extract from the standard input the couseworks
identified as <cw><num>, and create a directory for each coursework found, 
inside the directory <dir>\n\n";
    exit(-1)
    } else{

	$msgstart = shift @ARGV;
        $subject = shift @ARGV;
        $date = shift @ARGV;
	$dir = shift @ARGV;
	if (!(-d $dir)){
            print STDERR "dir to open 0:$dir\n";
	    mkdir($dir,0770);
	}
	$cw = shift @ARGV;
	$n = shift @ARGV;
	$dir = "$dir/$cw$n";
	if (!(-d $dir)){
            print STDERR "dir to open 1:$dir\n";
	    mkdir($dir,0770);
	}
	if ($#ARGV==0){
            shift @ARGV;
	    $web="1";
	}
        else{
	    $web="0";
	}
    }

$_ = <>;
$firstline=$_;
while(<>){
#    print "\n\nNEW STUDENT\n\n";
   
    if (!getstart($firstline,$msgstart,$date,$msgdate)){
	&skipmsg($msgstart);
        $firstline=$_;
#        print "date?\n";
        next;
    }

    if (!getsubject($subject)){
#        print STDEER "No subject\n";
	&skipmsg($msgstart);
        $firstline=$_;
        next;
    }


    &skipmsgheader;

    $line0 = $_;

    &skipblankline($line1);


    if ($line1 =~ /^%\s*$cw\d+\s*\n$/){
	$line2 = $line1;
	$line1 = "";
    }
    else {
	&skipblankline($line2);
    }
#    print "$line1\n$line2\n";
    &skipblankline($line3);

    &skipblankline($line4);

    if ($web == 0){
	$line4 = "";
    }
  
    print "0:$line0\n1:$line1\n2:$line2\n3:$line3\n4:$line4\n";


    if (!($line2 =~ /^%\s*$cw$n\s*\n$/)){
	&skipmsg($msgstart);
        $firstline=$_;
#        print STDERR "Error 2:$line0$line2\n";
	next;
    }
    if (!parseid($line0,$name0,$line1,$name1,$line3,$name3,$line4,$name4)){
	&skipmsg($msgstart);
        $firstline=$_;
#        print STDERR "parse error: $msgdate\n";
#	print STDERR "parse err:0:$line0 1:$line1 2:$line2 3:$line3 4:$line4\n";
#	print STDERR "parse err:0:$name0\n1:$name1\n3:$name3\n4:$name4\n";
#        print STDERR "\n\n";
	next;
    } else{
#	print("Ok:$name0\n\n");
    }

#    print "0:$name0\n1:$name1\n3:$name3\n4:$name4\n";

    if (!(-d "$dir/$name0")){
	mkdir("$dir/$name0",0770);
    }
    
    $name = "$dir/$name0/the$name3$name4";
    
    if (-e $name){
	unlink $name;
#	print STDERR "<$name$> removed";
    }
    if (!open (HDL0,">$name")) {
#	print STDERR "I can't open $name\n0:$line0\n1:$line1\n2:$line2\n";
	&skipmsg($msgstart);
        $firstline=$_;
    }

    if (!$web){
	print HDL0 '/*-------------',"\n";
	print HDL0 "% $firstline";
	print HDL0 $line0, $line1, $line2, $line3, $line4;
	print HDL0 '--------------*/',"\n";
    } else {
        print HDL0 "% $firstline";
	print HDL0 $line0, $line1, $line2, $line3, $line4;
    }

    while(<>){
	if (/^$msgstart/){
	    $firstline=$_;
            last;
	}
	print HDL0 $_;
    }
    close HDL0;
}







