#!/bin/bash
# August 2013: switched to css

# This script takes a directory containing a collection of images and makes
# a web page.  

# A default captions file will be created if it doesn't exist.
# Suggestion: run the program once to create a captions file,
# then edit it and run the program again.
# Individual figure captions appear in the captions file between
# a line containing the filename and a line with "endcaption"

# The page title appears in the captions file between
# lines "title" and "endtitle".
# If the captions file has material between a "startintro"
# and "endintro" line, this will appear at the top of the
# index page below the title.

#defaults
homelink=index.html
htmlname=index.html
# default title if not found in captions file
title="Picture Index"
intro="some pictures"
thumbsize=300x300
thumbdir=thumbnails
smallsize=640x640
indexfile=index.html
# default colors
bgcolor="#3F7FFF"
text=black
link=gold
vlink=brown
alink=red

# if called with the argument "clean" clean out thumbnails and html files
if [ "$1" = clean ] 
then
  rm -rf $thumbdir
  rm -f *.html
#  rm captions
  exit
fi

# construct list of image files
list=""
for i in `ls -r`
do
 if file $i | grep image >>/dev/null
 then
  list="$list $i"
 fi
done
echo Image list: $list

# make the thumbnail directory
if [ ! -d $thumbdir ]
then
mkdir $thumbdir
fi

# if it doesn't already exist, create the captions file
if [ ! -f captions ]
then
  echo title >> captions
  echo $title >> captions
  echo endtitle >> captions
  echo startintro >> captions
  echo $intro >> captions
  echo endintro >> captions
fi

# change the homelink if the captions file says to
if grep "homelink=" captions
then
 export `grep homelink= captions`
 echo using $homelink as homelink directory
fi

title=`awk '/^title/,/endtitle/{ if ($0 !~ /title/ \
  && $0 !~ /endtitle/) print }' captions`

# update the captions file
echo homelink=$homelink > newcaptions

awk /^title/,/endtitle/ captions >> newcaptions
awk /^startintro/,/endintro/ captions >> newcaptions

if grep mystyle captions
then
 awk /^mystyle/,/endmystyle/ captions >> newcaptions
else
echo making style section
  echo mystyle >> newcaptions
# create style section
  cat >> newcaptions <<EOF
<style>
body 
{
background-color:$bgcolor;
text-align:center;
color:$text;
margin-left:auto;
margin-right:auto;
}
ul {text-align:left;}
table {
text-align:center;
margin-left:auto;
margin-right:auto;
}
a:link {color:$link;}
a:visited {color:$vlink;}
a:hover {color:$alink;}
</style>
EOF
  echo endmystyle >> newcaptions
fi

for filename in $list
do
# use file name for caption if none found
  if ! grep -q "^caption_$filename" captions
  then
   echo caption_$filename >> newcaptions
   echo $filename >> newcaptions
   echo endcaption >> newcaptions
  else
   awk /^caption_$filename/,/endcaption/ captions >> newcaptions
  fi
done
mv newcaptions captions

# start off the index file
cat > $indexfile <<EOF
<!DOCTYPE HTML">
<html>
<head>
EOF

if grep -q mystyle captions
then
  awk '/^mystyle/,/endmystyle/{ if ($0 !~ /mystyle/ \
  && $0 !~ /endmystyle/)  print }' captions >> $indexfile
fi

cat >> $indexfile <<EOF

<title> $title </title>
</head>
<body>
<h2> $title </h2>

EOF


# look for an intro section in the captions file
if grep -q startintro captions
then
  awk '/^startintro/,/endintro/{ if ($0 !~ /startintro/ \
  && $0 !~ /endintro/) print }' captions >> $indexfile
  echo \<p\> >> $indexfile
fi

# main loop over all the .jpg pictures in the directory
COLUMN=1
for filename in $list
do

# finish off previous picture
  if [ $last ]
  then
   next=`basename $filename .jpg`.html
   cat >> $htmlname <<EOF
 <a href="$next" >Next</a>
 -- <a href="$indexfile">Index</a>
 -- <a href=$homelink>Home</a>
</h2>
</body>
</html>
EOF
  fi

# new filenames
thumbname=$thumbdir/t_$filename
smallname=$thumbdir/s_$filename
htmlname=`basename $filename .jpg`.html
# make smaller image if it doesn't exist
if [ ! -f $smallname ]
then
  convert $filename -geometry $smallsize $smallname
fi
# make thumbnail if it doesn't exist
if [ ! -f $thumbname ]
then
  echo making $thumbname
  convert $smallname -geometry $thumbsize $thumbname
fi

# add link to the index file
# a left column picture starts a new table
COLUMN=$(( 1 - $COLUMN ))
if (( $COLUMN == 0 ))
then
cat >> $indexfile <<EOF

<table cellpadding="10" width="100%">
<tr><td width="50%">
EOF
else # second column
# echo >>$indexfile
echo \</td\>\<td\> >> $indexfile
echo >>$indexfile
fi

# start the subtable with the image
cat >> $indexfile <<EOF

<table width="240"><tr><td>
<a href="$htmlname">
<img src="$thumbname" alt=""></a>
</td></tr>
<tr><td>
EOF

# put in the caption and close the subtable
awk '/^'caption_$filename'/,/endcaption/{ if ($0 !~ /'^caption_$filename'/ \
&& $0 !~ /endcaption/) print }' captions >> $indexfile
echo \</td\>\</tr\>\</table\> >>$indexfile

if (( $COLUMN == 1 )) # finish the row
then
cat >> $indexfile <<EOF

</td></tr></table>
EOF
fi

# make the individual picture web page
cat > $htmlname <<EOF
<!DOCTYPE HTML">
<html>
<head>
EOF

if grep -q mystyle captions
then
  awk '/^mystyle/,/endmystyle/{ if ($0 !~ /mystyle/ \
  && $0 !~ /endmystyle/)  print }' captions >> $htmlname
fi

cat >> $htmlname <<EOF
<title>$filename</title>
</head>
<body>
<a href="$filename"><img src="$smallname"></a>
<p>
<h2>
EOF

# put in the caption
if [ -f captions ]
then
awk '/'^caption_$filename'/,/endcaption/{ if ($0 !~ /'^caption_$filename'/ \
&& $0 !~ /endcaption/) print }' captions >> $htmlname
fi
echo \<p\> >> $htmlname

# link to previous image
if [ $last ]
then
echo >> $htmlname \<a href=\"$last\"\>Previous\</a\> --
fi

last=$htmlname
# end of main loop
done

# finish off last image page
cat >> $htmlname <<EOF
<a href="$indexfile">Index</a> -- <a href=$homelink>Home</a>
</h2>
</body>
</html>
EOF

# end the table if last row has only one picture
if (( $COLUMN == 0 ))
then
cat >> $indexfile <<EOF

</td></tr></table>

EOF
fi

# finish index page
cat >> $indexfile <<EOF
<h2><a href=$homelink>Home</a></h2>
</body>
</html>
EOF

