#!/usr/bin/perl -w

########################################################################
## boilerplate - see Boilerplate
########

use strict;
use FileHandle;
use IPC::Open2;
use Fcntl ':flock';
use Time::Local;
use CGI ':standard';

my $abstractFile	= "abstract";
my $baseCGI		= "/signup/cgi";
my $baseDir		= "/staff/bdbryant/public_html";
my $baseSched		= "/signup/schedules";
my $baseURL		= "http://www.cse.unr.edu/~bdbryant";
my $calProg		= "/usr/bin/cal";
my $signupCGI		= "/show_schedule.cgi?startdate=";
my $speakerTmpFile	= "/tmp/spkrcgi.$$";
my $timestampFile	= ".timestamp";
my $notesFile	        = ".notes";
my $updateCGI		= "/~bdbryant/signup/cgi/update_schedule.cgi";

my @daysOfWeek		= qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
my @monthNames		= qw( January February March April May June July August September October November December );

my %fV;

eval `cat Common.pl`;


########################################################################
## local declarations
########

my $oldtimestamp;
my $notestring;
my $schedpath;
my $textchunk;
my %textchunks;

########################################################################
## program body
########

&htmlPreamble( "Signup Schedule" );
&foldFDs();
&getFormVars();

&readAbstract();
&printTitle();
&beginForm();
&printFormBodies();
&printFormButtons();
&endForm();

&htmlPostamble();
&cleanup();

########################################################################
## local subroutines
########

sub beginForm()
{
	print '<form method="POST" action="' . $updateCGI . '">';
	print '<input type="hidden" name="startdate" value="' . $fV{"startdate"} . '">';
	if ( $oldtimestamp )
	{
		print '<input type="hidden" name="oldtimestamp" value="' . $oldtimestamp . '">';
	}
}

sub endForm()
{
	print '</form>';
}

sub printFormBodies()
{
	opendir( DIR, "." );
	my @files = readdir( DIR );
	closedir( DIR );
	foreach my $file ( sort @files )
	{
		if ( $file =~ /^\d+$/ )
		{
			my $year	= substr( $file, 0, 4 );
			my $month	= substr( $file, 4, 2 );
			my $day		= substr( $file, 6, 2 );
			my $dname	= &dateToDay( $year, $month, $day );

			print
			    "<br>",
			    "<center>",
			    "<b>",
			    "For $dname, $monthNames[$month-1] $day, $year:",
			    "</b>",
			    "</center>";

			printFormBody( $file );
		}
	}
}

sub printFormBody()
{
	open( SCHEDULEFILE, $_[0] ) or return;
	my @text = <SCHEDULEFILE>;
	close( SCHEDULEFILE );

	print "<center>";

	print
	'<textarea rows="18" cols="56" name="' . $_[0] . '">',
	@text,
	"</textarea>",
	"</p>";

	print "</center>";
}

sub printFormButtons()
{
	print "<center>";

	print
	"<p>",
	'<input type="submit" name="submit" value="submit">',
	'&nbsp;',
	'<input type="reset" name="reset" value="reset">',
	"</p>";

	print "</center>";
}

sub printTitle()
{
	print "<center>";

	print
	"<p>",
	'<font color="#b15729" size="+2">',
	"<b>Signup Schedule for Bobby Bryant</b>",
	"<br>",
	"</font>",
	"</p>";
	print
	    "<p>",
	    '<font color="#b15729" size="+1">',
	    "Enter your name by the slot you want to reserve, ",
	    "then scroll to the bottom and click 'submit'.<br>",
	    "Sign up for consecutive slots if you need a longer session.",
	    "</font>",
	    "</p>";
	print
	    "<p>",
	    '<font color="#b15729" size="+1">',
	    "Do not add new times; write me if you cannot meet at any of these times.",
	    "</font>",
	    "</p>";

	if ( open( TIMESTAMP, "< $timestampFile" ) )
	{
		$oldtimestamp = <TIMESTAMP>;
		close( TIMESTAMP );

		my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime( $oldtimestamp );
		my $datestring = sprintf( "%s, %s %d, %d %2d:%02d:%02d",
				$daysOfWeek[$wday], $monthNames[$mon], $mday, $year+1900,
				$hour, $min, $sec );

		print
		    "<br><br>",
		    "Last update was $datestring";
	}
		
	if ( open( NOTES, "< $notesFile" ) )
	{
		$notestring = <NOTES>;
		close( NOTES );
		if ( $notestring )
{
		print
		"<p>",
		"<tr>",
		'<td valign="middle" align="center">',
		'<font face="Arial,Helvetica,sans-serif" size="3"><b>',
		"$notestring",
		"</b></font>",
		"</td>",
		"</tr>";
}
	}
		
	print "<p></center>";

}

sub readAbstract()
{
	&bail( "No startdate specified" ) unless $fV{"startdate"};
	$schedpath = $baseDir . $baseSched . "/" . $fV{"startdate"};
	chdir( $schedpath ) or &bail( "cd $schedpath: $!" );
}

sub saveTextChunk()
{
	if ( $textchunk )
	{
		(my $left, my $right) = split( /:/, $textchunk, 2 );
		if ( $left eq "Affiliation" )
		{
			$right =~ s/\n/<br>/g;
			$right =~ s/<br>$//;
		}
		else
		{
			$right =~ s/\n/ /g;
		}
		$right =~ s/\s+/ /g;
		$textchunks{$left} = $right;
	}
}





