#!/usr/bin/perl -s
    $mailprog = '/usr/sbin/sendmail -t ';
    $MyEmail = 'tseng@lins.fju.edu.tw';
    $CollectionURL = 'http://blue.lins.fju.edu.tw/~tseng/Collections/Col_data/';
    
    use CGI qw(:standard);
    print header;
    print start_html('DownLoad FJU Collections');
    if (param('CR')) {
    	$Collection = param('Collection');
	&ShowForm();
    } else {
    	$FirstName = param('FirstName');
    	$LastName = param('LastName');
    	$Title = param('Title');
    	$Affiliation = param('Affiliation');
    	$Email = param('Email');
    	$HomePage = param('HomePage');
    	$RequestList = param('RequestList');
    	$Comments = param('Comments');
    	$Collection = param('Collection');
    	foreach $n ($FirstName, $LastName, $Title, $Affiliation, $Collection, 
    			$Email, $HomePage, $RequestList, $Comments) {
    	    $n =~ s/^\s+|\s+$//g;
    	}
    	&MissField('first name') if ($FirstName eq '');
	&MissField('last name') if ($LastName eq '');
	&MissField('email') if ($Email eq '');
	&MissField('affiliation') if ($Affiliation eq '');
	$CollectionURL .= $Collection;
	&ShowCollectionURL();
    }
    print end_html;
    exit;

sub ShowForm {
	
    print "<center>", table(Tr(td( 
    h2('Copyright Notice'),
    table({-border=>5}, Tr(td(
"
To use this test collection, you agree to keep this collection from being 
unfair use (for example, profitable use or distribution of this collection).

"))),p, h2('Your Information'),
    start_form,
    table(Tr(
    [ td(["First Name: ",textfield('FirstName', '', 50, 50),br,]),
      td(["Last Name: ",textfield('LastName', '', 50, 50),br,]),
      td("Title: ") .
        td(radio_group(-name=>'Title',
		   -values=>['Mr.','Mrs.','Dr.','Prof.'],
		   -defaults=>['Dr.']),br,),
      td(["Affiliation: ", textfield('Affiliation', '', 80, 80),br,]),
      td(["Email: ", textfield('Email', '', 80, 80),br,]),
      td(["Home Page: ", textfield('HomePage', '', 80, 80),br,]),
      td("To be listed in the Request List: ") .
        td(radio_group(-name=>'RequestList',
		   -values=>['Yes','No'],
		   -defaults=>['Yes']),br,),
      td(["Other comments: ",  
        textarea(-name=>'Comments',-rows=>10,columns=>60,-default=>""),p,])
    ])),
    hidden('Collection'),
    submit,
    end_form, "</center>"
    )));
}

sub MissField {
    my($str) = @_;
    print h3("Please fill in your $str"), p, "\n";
    &ShowForm();
    print end_html();
    exit;
}

sub ShowConfirmation {

}

sub ShowCollectionURL {
    &MailOut();
    &InsertRequestList() if $RequestList eq 'Yes';
    print h3(
    "An email has been sent to '$Email' to show you how to download the collection.");
}


sub MailOut {
    $RequestInformation = <<END;
-----------------------------------------------------------------
First name: $FirstName
Last name: $LastName
Title: $Title
Email: $Email
Affiliation: $Affiliation
Home page: $HomePage
To be listed in the Request List: $RequestList
Requested collection: $Collection
Comments: 
  $Comments
-----------------------------------------------------------------
END

    open (M, "| $mailprog ") || die("Cannot open  $mailprog");
    print M <<MailHead;
From: "Yuen-Hsien Tseng" <$MyEmail>
Organization: Fu Jen Catholic University
To: $Email
Cc: $MyEmail
Subject: Download FJU collections from $FirstName $LastName

Dear $Title $FirstName $LastName,

The collection you requested can be downloaded at
$CollectionURL
with 
username: user
password: user

Your informatoin (for you to verify):
$RequestInformation

Kind regards,

Yuen-Hsien Tseng
Professor
Dept. of Library & Information Science
Fu Jen Catholic University
510, Chung Cheng Road, HsinChuang,
Taipei, Taiwan, R.O.C. 242
Home page: http://www.lins.fju.edu.tw/~tseng/

MailHead
    close(M);
}

sub InsertRequestList {
    my($Date, $file, $text, $str);
    my($sec, $min, $hour, $day, $mon, $year) = localtime(time); 
    $mon += 1; $year += 1900;
    $Date = "$year/$mon/$day";
#    $file = $Collection =~ /TC/?'Chinese_TC.html' : 'Chinese_OCR_IR.html';
    $file = 'RequestList.html';
    $str =<<END;

<li>$Title $FirstName $LastName, from $Affiliation, at $Email, on $Date

END
    open F, $file or die "Cannot read file:'$file'";
    local $/; undef $/; $text = <F>; # read all the content into $text
    close(F);
#    $text =~ s#<!--InsertHere-->#<li>$str#;
    $text =~ s#(\s<!--InsertHere-->\s)#$1<li>$Date<br><pre>\n$RequestInformation</pre>\n\n#;
    open F, ">$file" or die "Cannot write to file:'$file'";
    print F $text;
    close(F);
    
}
