#!/usr/bin/perl 

$QOTDFILENAME  = "qotd.txt";
$QOTDIMAGEFILE = "qotdImages.txt";

print "Content-type: text/html\n\n";

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = localtime(time);

$dayNum = $year*365+$yday;

if (!open QFILE, "$QOTDFILENAME") {
	print "Can't open $QOTDFILENAME\n";
	exit(-1);
} 

if (!open IFILE, $QOTDIMAGEFILE) {
	print "Can't open $QOTDIMAGEFILE\n";
	exit(-1);
}

@lines = <QFILE>;
@images = <IFILE>;

$quote_file_text = join '', @lines;
$quote_file_text =~ s/\n/\n/g;

@quotes = split '\*\*\*', $quote_file_text;  
$num_lines = @quotes;
$num_images = @images;
$idx_yest_img = ($dayNum-1)%$num_images;
$idx_yest = ($dayNum-1)%$num_lines;

print "<html><body>\n";
print "<div align='center'><p>Yesterday's Awakening</p></div>\n";
print "<div align='center'>\n";
$img_src = $images[$idx_yest_img];
$img_src =~ s/[\t ].*$//g;
if ($images[$idx_yest_img] =~ /.*[ \t].*/) {
	$img_credit = $images[$idx_yest_img];
	$img_credit =~ s/^[^\t ]+[\t ]//g;
} else {
	$img_credit = "";
}
print "<img src=\"$img_src\"><BR> $quotes[$idx_yest]</div><BR>\n";
print "<div align='center'><em>$img_credit</em></div><br><br>\n";
print "<div align='center'><a href=\"daily-msg.pl\">Today's Awakening</a></div>\n";
print "</body></html>\n\n";
