#!/usr/bin/perl
use File::Copy;

#==========================================
#global constants

$WELCOME_MSG=
"\n
********************************************************************
*This script updates the Publications section of the LAMP web site.*
*                      Version 0.1 - July, 2001                    *
********************************************************************
\n\n";              



$TASK_MENU=
"
Below is the list of tasks currently supported:
====================================================================
|   1 - Add an item to the Publications section.                   |
|   2 - Add an item to the Technical Reports (TR) section.         |
|   x - Exit to the previous menu.                                 |
====================================================================
";

$FILENAME_PROMPT=
"
Please enter the file name of the [document] summary.
(Note: This file should be prepared according to the template provided at [location].)
Filename: ";

#=========================================

sub main {
    print $WELCOME_MSG;

    $more="y";
    while ($more =~/[yY]/) {
        chooseTask();
        print "\n\nWould you like to add another item? (y, n) ";
        chomp($more=<STDIN>);
    }

    print "Good bye.  Script now existing.\n";	
}

sub chooseTask {
    print $TASK_MENU;

    print "Please enter your selection: ";
    chomp($choice=<STDIN>);
    if ($choice==1) {
	addPub();
    }
    elsif ($choice==2) {
	addTr();
    }
    elsif ($choice =~/[xX]/) {
        #do nothing
    } 
    else {
        print "Invalid selection.  Please enter 1, 2 or x\n\n";
        chooseTask();
    }
}

sub addTr {
    print $FILENAME_PROMPT;
    chomp($file=<STDIN>);
    if (!(-r $file)) {
        print "Error: Cannot read the file \"$file\". Make sure you have set the permission correctly\n\n";
        addPub();
    }
    open (FILE, $file);

    my ($tr);
    $tr={
	author	=> "",
	title	=> "",
	publisher=>"",
        trnumber=> "",
	year	=> "",
        ps	=> "",
        pdf	=> "",
        abstract=> "",
    };

    chomp($_=<FILE>);
    ($tag, $tr->{author})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $tr->{title})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $tr->{publisher})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $tr->{trnumber})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $tr->{year})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $tr->{ps})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $tr->{pdf})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $tr->{abstract})=split(/:\s+/);
    while (<FILE>) {
        $tr->{abstract}=$tr->{abstract}.$_;
    }

    printSummary($tr);

    print "\nIs this information correct? (y, n)";
    chomp($answer=<STDIN>);
    if ($answer=~/[yY]/) {
        updateTr($tr);
    }
    else {
        addTr();
    }
    
}

sub addPub {
    print $FILENAME_PROMPT;
    chomp($file=<STDIN>);
    if (!(-r $file)) {
        print "Error: Cannot read the file \"$file\". Make sure you have set the permission correctly\n\n";
        addPub();
    }
    open (FILE, $file);

    my ($pub);
    $pub={
	author	=> "",
	title	=> "",
	publisher=>"",
        pages	=> "",
	year	=> 0,
	memo	=> "",
        ps	=> "",
        pdf	=> "",
        abstract=> "",
    };

    chomp($_=<FILE>);
    ($tag, $pub->{author})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $pub->{title})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $pub->{publisher})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $pub->{pages})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $pub->{year})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $pub->{memo})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $pub->{ps})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $pub->{pdf})=split(/:\s+/);

    chomp($_=<FILE>);
    ($tag, $pub->{abstract})=split(/:\s+/);
    while (<FILE>) {
        $pub->{abstract}=$pub->{abstract}.$_;
    }

    printSummary($pub);

    print "\nIs this information correct? (y, n)";
    chomp($answer=<STDIN>);
    if ($answer=~/[yY]/) {
        updatePub($pub);
    }
    else {
        addPub();
    }
}

sub printSummary {
    my($pub);
    $pub=$_[0];
    while ( ($k,$v) = each %$pub ) {
        print "$k:\t$v\n";
    }
}

sub updateTr {
    my($pub, $filename, $extention, $old, $new, $html, $yearline, $inserted);
    $tr=$_[0];

    $html=$tr->{author}.", ".$tr->{title};
    
    if ($tr->{publisher}) {
        $html=$html.", <i>".$tr->{publisher}."</i>";
    }
    if ($tr->{trnumber}) {
        $html=$html.", ".$tr->{trnumber};
    }
    if ($tr->{year}) {
        $html=$html.", ".$tr->{year};
    }

    $extention=1;
    $filename="TR".join(split($tr->{year}))."_".$extention;
    while (-e "../pubs/docs/$filename.ps" ||
           -e "../pubs/docs/$filename.pdf" ||
           -e "../pubs/docs/$filename.htm"
           ) {
        $extention++;
        $filename="TR".$tr->{year}."_".$extention;
    }

    if ($tr->{ps}) {
        copy("$tr->{ps}", "../pubs/docs/$filename.ps")	or die "Cannot read the PS file specified.  Please make sure you have entered the correct name, and the file has the appropraite permissions";
        $html=$html." <a href=\"docs/$filename.ps\">[PS]</a> ";
    }
    if ($tr->{pdf}) {
        copy("$tr->{pdf}", "../pubs/docs/$filename.pdf")	or die "Cannot read the PDF file specified.  Please make sure you have entered the correct name, and the file has the appropraite permissions";
        $html=$html." <a href=\"docs/$filename.pdf\">[PDF]</a> ";
    }
    if ($tr->{abstract}) {
        createAbstractPage($tr, $filename);
        $html=$html." <a href=\"docs/$filename.htm\">[Abstract]</a> ";        
    }
    $html='<font face="Verdana, Arial, Helvetica, sans-serif" size="1"><li>'.$html."</li></font><p>\n";
    $old="../pubs/tr.htm";
    $new="../pubs/trnew.htm";
    open(OLD, "$old")		or die "Cannot open OLD";
    open(NEW, ">$new")		or die "Cannot write to NEW";
    while (<OLD>) {
        if (/<!--end-->/) {
            print NEW $html;
        }
        print NEW $_;
    }
    close(OLD);
    close(NEW);
    rename($old, "$old.backup");
    rename($new, $old);
    print "\nThe information has been successfully updated.\n";   
}

sub updatePub {
    my($pub, $filename, $extention, $old, $new, $html, $yearline, $inserted);
    $pub=$_[0];

    $html=$pub->{author}.", ".$pub->{title}.", <i>".$pub->{publisher}."</i>";
    if ($pub->{pages}) {
        $html=$html.", ".$pub->{pages};
    }
    if ($pub->{year}) {
        $html=$html.", ".$pub->{year};
    }
    if ($pub->{memo}) {
        $html=$html.", ".$pub->{memo};
    }
    $extention=1;
    $filename="PUB".$pub->{year}."_".$extention;
    while (-e "../pubs/docs/$filename.ps" ||
           -e "../pubs/docs/$filename.pdf" ||
           -e "../pubs/docs/$filename.htm"
           ) {
        $extention++;
        $filename="PUB".$pub->{year}."_".$extention;
    }

    if ($pub->{ps}) {
        copy("$pub->{ps}", "../pubs/docs/$filename.ps")	or die "Cannot read the PS file specified.  Please make sure you have entered the correct name, and the file has the appropraite permissions";
        $html=$html." <a href=\"docs/$filename.ps\">[PS]</a> ";
    }
    if ($pub->{pdf}) {
        copy("$pub->{pdf}", "../pubs/docs/$filename.pdf")	or die "Cannot read the PDF file specified.  Please make sure you have entered the correct name, and the file has the appropraite permissions";
        $html=$html." <a href=\"docs/$filename.pdf\">[PDF]</a> ";
    }
    if ($pub->{abstract}) {
        createAbstractPage($pub, $filename);
        $html=$html." <a href=\"docs/$filename.htm\">[Abstract]</a> ";        
    }
    $html='<font face="Verdana, Arial, Helvetica, sans-serif" size="1"><li>'.$html."</li></font><p>\n";
    $old="../pubs/pubs.htm";
    $new="../pubs/pubnew.htm";
    open(OLD, "$old")		or die "Cannot open OLD";
    open(NEW, ">$new")		or die "Cannot write to NEW";
    while (<OLD>) {
        print NEW $_;
        if ($_=~/<!--$pub->{year}-->/) {
            $yearline=<OLD>;
            print NEW $yearline; #skips over the year number
            print NEW $html;
            $inserted="y";
        }
    }
    close(OLD);
    close(NEW);
    if ($inserted ne "y") {#the item being inserted is the first of this year
        updatePubSpecialCase($pub, $html);
    }
    rename($old, "$old.backup");
    rename($new, $old);
    print "\nThe information has been successfully updated.\n";   
}

sub createAbstractPage {
    my($pub, $filename);
    $pub=$_[0];
    $filename=$_[1];
    open (ABS, ">../pubs/docs/$filename.htm")		or die "cannot create the abstract page.";
    open (ABSTEMP, "../pubs/docs/abs_template.htm")			or die "cannot open the template file.";
    while (<ABSTEMP>) {
        print ABS $_;
        if (/<!--title-->/) {
            print ABS "\n<font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>".$pub->{title}."</b></font><br>";
        }
        elsif (/<!--author-->/) {
            print ABS "\n<font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\"><i>".$pub->{author}."</i></font><br>";
        }
        elsif (/<!--abstract-->/) {
            print ABS "\n<font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">".$pub->{abstract}."</font><br>";
            if ($pub->{ps} || $pub->{pdf}) {
                print ABS "<p><font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>Manuscript: </b></font>";
            }
            if ($pub->{ps}) {
                print ABS "<a href=\"$filename.ps\">[PS]</a> ";
            }
            if ($pub->{pdf}) {
                print ABS "<a href=\"$filename.pdf\">[PDF]</a> ";
            }
        }
    }
    close(ABS);
    close(ABSTEMP);
}


sub updatePubSpecialCase {#to handle the case where the publication entered is the first of its year.
    my($pub, $html, $written);
    $pub=$_[0];
    $html=$_[1];
    $html="\n<!--$pub->{year}-->\n<b><font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">$pub->{year}</font></b>\n".$html."\n";    
    $old="../pubs/pubs.htm";
    $new="../pubs/pubnew.htm";
    open(OLD, "$old");
    open(NEW, ">$new");
    while(<OLD>) {
        if (/<!--\d\d\d\d-->/) {
            @words=split(/-+/);
            $yearvalue=$words[1];
            if ($year>$yearvalue &&!$written) {
                print NEW $html;
                $written="y";
                print NEW $_;
            }
        }
        elsif (/<!--end-->/ && !$written) {
            print NEW $html;
            print NEW $_;
        }
        else {
            print NEW $_;
        }
    }
    close(OLD);
    close(NEW);
}    



main();