#!/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 $nextpic; my $nextindex;

my $i; my $j; my $k;	#loop index
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 august-photos.csv: $!";

#open data file and extract each line of data, plcing 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";
}

$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;}

if (substr($picclean,0,1) eq "0"){
substr($picclean,0,1,"");
}

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

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

$picbig=$picclean.".jpg";
$nextindex=++$picclean;
#print "$nextindex $picbig ";

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

#if ($nextindex<10) {
#$nextindex="0".$nextindex;}
#print " $nextindex ";

$nextpic=$nextindex.".jpg";

#reuse nextpic to serve as target for pause/resume button
if (-e "../august-photos/$nextpic") {
$nextpic="augustslide.cgi?&key=$nextindex";}
else{
$nextpic="newgal.cgi";}

#print "$nextpic";
#print " $picbig";

$heading="August 2013";
$altext="Photos from August 2013";

print q(
<!DOCTYPE HTML>
<html lang="en>
<head>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
);

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

print q(<STYLE>
@import url(../gallery.css);
</STYLE>);

print qq(
<meta charset="UTF-8">
</head>
<body>);

print qq(
<div id="card">
    <img src="../gal_back.jpg">

  <div id="header">
  August 2013
  </div>
  <div id="images"><img src="../august-photos/$picbig" alt="$altext">
  </div>
  <div id="description">
  $titles[$picclean] 
  </div>
  
<div id="button">
  <a href="../index.html" class="chapter"><img src="../button.gif"  border="0"/></a>
  </div>
<div id="pause_button">
  <a href="$nextpic" class="chapter"><img src="/resume.gif"  border="0"/></a>
  </div>
<div id="stop_button">
  <a href="august-gal.cgi" class="chapter"><img src="../stop_button-2.gif"  border="0"/></a>
  </div>
  <div id="copyright">
  All photographs &copy; R A O'Neill 2003 - 2010
  </div>
</div>


</body>
</html>);