#!/usr/bin/perl
use strict;
#use lib '/usr/lib/perl5/5.8.3';
use CGI ':standard';
$CGI::POST_MAX=4;


my $picnum;
my $picbig;
my $picclean;
my $heading;
my $altext;
my $lastindex; my $lastpic; my $returncookie;


my $i; my $j; my $k;	#loop index
my $nextpic; my $nextindex; my @time; my $day; my $month; my $year; my $now; my $then;
my $datestring; my $date; my $expirestr;

my $photofield;	#single line from csv file
my @arrayline;		#split into an array
my @photoarray;	#array created from each line of csv file, as an array
my $datafile;		#name of csv file
my $fh;			#file handle
my $len1;		#refers to length of @photoarray
my @titles;  my @indexes; #list of required captions
my $title; my $index;

$photofield = "1";	#ensure variable is defined
$datafile = "../august-photos.csv";

open $fh, "$datafile" or die "Can't open photos.csv: $!";

#open data file and extract each line of data, placing it in an array
$i = 0;

while (defined($photofield)) {
defined($photofield = readline($fh)) or last;
@photoarray = split(/,/,$photofield);
$indexes[$i]=$photoarray[0];
$titles[$i]=$photoarray[1];
$titles[$i]=~ s/\"//g;
$i++;
#print $photoarray[1]." ".$photoarray[3]." ".$photoarray[5]."\n";
}


#Initialize $picnum, then set it to any parameter passed.  Check parameter is in the form of two digits, and reject if not.
$picnum=1;
$picnum=param('key');
$picnum =~ /(^\d{2}$)/;
if ($1 eq "") {
print "content-type: text/html\n\n";
print '<p>Action not permitted</p>';
die;
} else{
$picclean=$1;}

#make sure $picclean fits format of photo titles, esp between 0 and 9
if (substr($picclean,0,1) eq "0"){
substr($picclean,0,1,"");
}

#print "content-type: text/html\n\n";
#print "$picclean";

$lastindex=$picclean;
if ($lastindex<=9){
$lastindex="0".$lastindex;}
$lastpic=$lastindex.".jpg";

if ($picclean<=9) {
    $picclean="0".$picclean;}

#set the title of the photo to be shown, and set up the title of the next one.
$picbig=$picclean.".jpg";
$nextindex=++$picclean;
$nextpic=$nextindex.".jpg";

#reset $picclean to use as index for photo titles
--$picclean;

#If the next one exists then prepare a string for the web page header to show that photo next.  If the photo is less than 80, then move to next season. If the photo shown is the last one, then the header takes us back to the Index page.
if (-e "../august-photos/$nextpic") {
	$nextpic="augustslide.cgi?&key=$nextindex";}

else {
	$nextpic="august-gal.cgi";}
	
if ($nextindex>=27) {
  $nextindex=00;
  }

$heading="August 2013";
$altext="Photos from August, 2013";
#set cookie to remember next image for returning viewers
#use time()
#get local time to set cookie expiration in 30 days

$now=time();
$then=$now+2764800; #seconds in 32 days
$datestring=scalar localtime($then);

$day=substr($datestring,0,3);
$date=substr($datestring,8,2);
$month=substr($datestring,4,3);
$year=substr($datestring,20,4);
$expirestr=$day."-".$date."-".$month."-".$year." 00:00:00 GMT";

print "Set-Cookie:nextimage=$nextindex; expires=$expirestr\n";
print "content-type: text/html\n\n";

#print "$expirestr";
#print "$lastpic ";
#print "$nextpic";
#print " $picbig ";
#print " $nextindex";
#print $picclean;

print q(
<!DOCTYPE HTML>
<html lang="en">
<head>);

print qq (
<title>$heading</title>);

print q(<STYLE TYPE="text/css" media="screen">
@import url(../gallery.css);
</STYLE>);

print qq(
<meta http-equiv="refresh" content="5;url=$nextpic">
<meta charset="UTF-8">
</head>
<body>

<div id="card">
    
  <div id="header">
  August 2013
  </div>
  <table id="images">
  <tbody id="image-body">
  <tr><td id="button-area">  	
  <div id="pause_button">
  <a href="august-pause.cgi?&key=$lastindex"><img src="../pause-white.gif"></a>
	</div>
  <div id="stop_button">
  <a href="august-gal.cgi"><img src="../stop-button-white.gif"></a>
  </div>
  <div id="button">
  <a href="../index.html"><img src="../button-white.gif"></a>
  </div>
  </td>
  <td>
  <div id="image-area">  
  <img src="../august-photos/$picbig" alt="$altext">
  </div>
  </td>
  </tr>
  <tr>
  <td>
  <div id="copyright">
  All photographs &copy; R A O'Neill 2003 - 2016
  </div>
  </td>
  <td>
  <div id="description">
  $titles[$picclean] 
  </div>
  </td>
  </tr>
  </tbody>
  </table>
  
</div>


</body>
</html>);