#! /bin/bash

# sphinx - Simple Picture Html INdeXer, by Daniele Raffo
# Released under the GPL License



if [ ! -z "$1" ] 
then 
    echo "

sphinx - Simple Picture Html INdeXer
Copyright (C) Daniele Raffo   v3.2.1 (18.01.2008) 

This script indexes picture files into a HTML album.

Usage: bash ./sphinx 

(The following instructions make reference to constants in sphinx.cfg,
the configuration file for sphinx.  I recommend that you modify just
the file sphinx.cfg, and leave this script unchanged -- unless you 
really want to.)  

This program generates two index pages with links to each picture:
a $INDEX_FILE with picture descriptions, and a $TH_INDEX_FILE
with thumbnails.  In both indexes, each entry links to a picture 
enframed in a black page, with links to the previous and next picture 
and back to the two index pages.  
For both indexes, it is possible to classify the images into 
sections.  Each entry in the indexes is the file's description, 
as parsed from the $DESCRIPTOR_FILE; this is a plaintext file which
you write to describe your album, formatted as follows: 

TITLE Section Alpha
image1.jpg Description Of First Image
image2.jpg Description Of Second Image
image3.gif Description Of Third Image
TITLE Section Beta
my_img.png Description Of Another Image
imageV.jpg Description of the Fifth Image
EOF

Hence the TITLE codeword identifies a section.  The descriptor file 
must end with the codeword EOF. 

The thumbnails are automatically generated by a call to ImageMagick. 
This program was designed to create a photographic album; to use it 
for other kinds of pictures, just replace the strings as appropriate.

You can download the latest version of this program from 
http://www.crans.org/~raffo/projects/sphinx
Report comments and bugs to <Daniele.Raffo@ca.infn.it>.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

ImageMagick is Copyright (C) 1999-2004 ImageMagick Studio LLC.

"
    exit 0
fi



if [ ! -e ./sphinx.cfg ]
then
    echo "Error: configuration file sphinx.cfg not found"
    exit 1
fi
source ./sphinx.cfg
if [ ! -e $DESCRIPTOR_FILE ]
then
    echo "Error: descriptor file $DESCRIPTOR_FILE not found"
    exit 1
fi
if [ ! -e $TH_DIR ]
then
    mkdir $TH_DIR
fi
set +o noclobber
rm -f $INDEX_FILE $TH_INDEX_FILE



# Create indexes' header

echo "
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>Photographs by $AUTHOR</title>
$HEAD_TAGS 
</head>
<body bgcolor=black text=white link=\"#999999\" alink=\"#999999\" vlink=\"#666666\">
<center><font size=+3>Photographs by $AUTHOR</font></center><br>
<blockquote>
" | tee $TH_INDEX_FILE > $INDEX_FILE



# Read the descriptor file, create thumbnails, complete indexes

line=""
n_pic=0
current_title="Top"
while [[ "$line" != EOF* ]]
do
    read line
    if [[ "$line" == EOF* ]]
    then
	break
    elif [[ $line == TITLE\ * ]]
    then
        # Title of a section
	current_title=${line#TITLE }
	echo "<p><br><blockquote><i><a name=\"$current_title\"><font size=+2>$current_title</font></a></i></blockquote>" | tee -a $TH_INDEX_FILE >> $INDEX_FILE
    else
        # Picture description
	file_list[$n_pic]="${line%% *}"
	descr_list[$n_pic]="${line#* }"
	title_belong[$n_pic]="$current_title"
	echo "<p><i><a href=\"${file_list[$n_pic]/%.*/.html}\"><font size=+1>${descr_list[$n_pic]}</font></a></i></p>" >> $INDEX_FILE
	echo "<span title=\"${descr_list[$n_pic]}\" class=\"description\"><a href=\"${file_list[$n_pic]/%.*/.html}\"><img src=\"$TH_DIR$TH_PREFIX${file_list[$n_pic]}\" border=0 vspace=5 hspace=5></a></span>" >> $TH_INDEX_FILE

	echo -n "$PIC_DIR${file_list[$n_pic]}: "
	if [ ! -e $PIC_DIR${file_list[$n_pic]} ]
        then
	    echo "(Warning) picture file does not exist"
	else
	    if [ -e $TH_DIR$TH_PREFIX${file_list[$n_pic]} ]
	    then
		echo "thumbnail already exists"
	    else
		echo "generating thumbnail"
		convert -antialias -geometry 120x60 $PIC_DIR${file_list[$n_pic]} $TH_DIR$TH_PREFIX${file_list[$n_pic]}
	    fi
	fi

	let n_pic++
    fi
done < $DESCRIPTOR_FILE

echo "
</blockquote>
<p><br><br><center><font size=-1><a href="$TH_INDEX_FILE">thumbnails</a></font></center>
" >> $INDEX_FILE

echo "
</blockquote>
<p><br><br><center><font size=-1><a href="$INDEX_FILE">no thumbnails</a></font></center>
" >> $TH_INDEX_FILE

echo "
<p><br><br><i>$MESSAGE</i></p>
<p><i>Album automatically created by <a href=\"http://www.crans.org/~raffo/projects/sphinx/\">sphinx</A> v$VERSION on $DATETIME.</i><br><br><br></p>
</blockquote>
</body>
</html>
" | tee -a $TH_INDEX_FILE >> $INDEX_FILE



# Create frames

let last_pic=$n_pic-1
for ((i=0; i < n_pic; i++)) 
do
    if [ "$i" -eq 0 ]
    then
	prev_link="<img src=\"$BLANK_ICON\" border=0 vspace=10>"
	next_link="<a href=\"${file_list[$i+1]/%.*/.html}\"><img src=\"$NEXT_ICON\" border=0 vspace=10></a>"
    elif [ "$i" -eq "$last_pic" ]
    then
	prev_link="<a href=\"${file_list[$i-1]/%.*/.html}\"><img src=\"$PREV_ICON\" border=0 vspace=10></a>"
	next_link="<img src=\"$BLANK_ICON\" border=0 vspace=10>"
    else 
	prev_link="<a href=\"${file_list[$i-1]/%.*/.html}\"><img src=\"$PREV_ICON\" border=0 vspace=10></a>"
	next_link="<a href=\"${file_list[$i+1]/%.*/.html}\"><img src=\"$NEXT_ICON\" border=0 vspace=10></a>"
    fi
    
    echo "
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>${title_belong[$i]}: ${descr_list[$i]} - Photograph by $AUTHOR</title>
$HEAD_TAGS 
</head>
<body bgcolor=black>
<center>
$prev_link
<a href=\"$INDEX_FILE#${title_belong[$i]}\"><img src=\"$INDEX_ICON\" border=0 vspace=10></a>
<a href=\"$TH_INDEX_FILE#${title_belong[$i]}\"><img src=\"$TH_INDEX_ICON\" border=0 vspace=10></a>
$next_link
<br><br><img src=\"$PIC_DIR${file_list[$i]}\" vspace=10><br><br>
$prev_link
<a href=\"$INDEX_FILE#${title_belong[$i]}\"><img src=\"$INDEX_ICON\" border=0 vspace=10></a>
<a href=\"$TH_INDEX_FILE#${title_belong[$i]}\"><img src=\"$TH_INDEX_ICON\" border=0 vspace=10></a>
$next_link
</center>
</body>
</html>
" > ${file_list[$i]/%.*/.html}

done

chmod 644 *.html $PIC_DIR* $TH_DIR*
chmod 711 $PIC_DIR $TH_DIR
exit 0

