#!/bin/bash
# Version is 1.0.1
# Thanks to Gontran for his tip about the error. 
curdir=`pwd`/
clear
echo "============================================"
echo "========   Welcome to PHOTO-GEN    ========="
echo "============================================"
echo "= This script will create a photo album    ="
echo "= From a directory full of jpgs.           ="
echo "============================================"
echo "=                                          ="
echo "= Created by Jeremy Shawley                ="
echo "= For more information, please visit:      ="
echo "= http://shawley.myip.org                  =" 
echo "= or write me:                             ="
echo "= jshawley@shawley.myip.org                ="
echo "=                                          =" 
echo "============================================"
echo ""
echo ""
echo ""
echo "============================================"
echo "= Lets get started. Press ctl-c to quit    ="
echo "============================================"
echo ""
# set XWIN so bash doesn't bitch at last string comaprison
XWIN='N'

echo "What is the full path to the directory of your pictures ?"
echo "[ $curdir ] Press Enter for Default"
read DIRPATH 

if [ "$DIRPATH" = "" ]
	then
	DIRPATH=$curdir
fi

echo ""
echo "Is this path correct ($DIRPATH) ? (y or n)"
read YESORNO 

if [ $YESORNO = "y" ] || [ $YESORNO = "Y" ]
	then
	if [ ! -d $DIRPATH ]
		then
		echo "Sorry, directory does not exist - Exiting"
		echo "Please run photogen again with the correct path"
		echo ""
		exit 0
	fi
 	echo ""
	echo "============================================"
	echo "Creating thumbnails from this path :"
	echo "$DIRPATH"
	echo "============================================"
	echo ""
else
	exit
fi

echo ""
echo "============================================"
echo "Please give your gallary a name :"
echo "============================================"
read galname
echo ""
echo "============================================"
echo "Would you like to put captions on your images ?"
echo "(y or n)"
echo "============================================"
# read CAPT
CAPT=n
echo ""
	if [ $CAPT = "y" ] || [ $CAPT = "Y" ]
	then
	echo "============================================"
	echo "Are you using Xwindows ?"
	echo "(y or n)"
	echo "============================================"
	read XWIN 
	echo ""
	fi
echo "============================================"
echo "Would you like you like to change your "
echo "High res pictures to 72 dpi also ?"
echo "(y or n)"
echo "============================================"
HIRES=n
#read HIRES
echo "Creating Thumbnails"

if [ ! -d $DIRPATH/lowres ]
	then
	mkdir $DIRPATH/lowres
	echo "============================================"
	echo "Created Directory lowres to store thumbnails"
	echo "============================================"
fi

if [ ! -e $DIRPATH/index.html ]
	then
	touch $DIRPATH/index.html
else
	echo "==============================================="
	echo "WARNING: index.html is there, removing old file"
	echo "==============================================="
	rm $DIRPATH/index.html
	touch $DIRPATH/index.html
	echo ""
fi

echo "==============================================="
echo "Building New index.html template"
echo "==============================================="
echo ""
echo "==============================================="
echo "What color would you like the background ?"
echo "==============================================="
echo "1. Steel Grey <default>"
echo "2. Blue"
echo "3. Green"
echo "4. Yellow"
echo "5. Pink"
echo "" 
echo "Please choose 1 - 5"
read BGCOLOR

if [ $BGCOLOR = "1" ]
	then
	BGCOLOR=#778899
elif [ $BGCOLOR = "2" ]
	then
	BGCOLOR=lightblue
elif [ $BGCOLOR = "3" ]
        then
        BGCOLOR=lightgreen
elif [ $BGCOLOR = "4" ]
        then
        BGCOLOR=lightyellow
elif [ $BGCOLOR = "5" ]
        then
        BGCOLOR=pink
else
        BGCOLOR=#778899
fi
echo "<HTML><HEAD><TITLE> $galname </TITLE><BODY bgcolor=$BGCOLOR>" >> $DIRPATH/index.html
echo "<H1> $galname </H1>" >> $DIRPATH/index.html
echo "<table border=0 cellpadding=20 cellspacing=20><tr>" >> $DIRPATH/index.html
colcount=0

echo ""
echo "==============================================="
echo "Now Generating Thumb Nails"
echo "==============================================="
echo ""

for image in $(ls $DIRPATH/*.[jJgG][pPiI][gGfF] ); do

	base=`basename $image`

	echo "Lowresing image : $base"

	if [ $colcount -eq  5 ] 
		then
	echo "</tr><tr>" >> $DIRPATH/index.html
		colcount=1
	else
		colcount=$(expr $colcount + 1)
	fi

	if [ ! -e $DIRPATH/lowres/low-$base ]
		then
		 	if [ "$HIRES" = "y" ] || [ "$HIRES" = "Y" ]
			then
			echo "-- Converting original to 72 dpi --"
			convert -density 72x72 $DIRPATH/$base $DIRPATH/$base
			fi
		convert -density 72x72 -geometry 100x100 $DIRPATH/$base $DIRPATH/lowres/low-$base
	else
		echo "== Image low-$base is there - Skipping Lowres Generation =="
	fi

	if [ $CAPT = "y" ] || [ $CAPT = "Y" ]
		then
		if [ $XWIN = "y" ] || [ $XWIN = "Y" ]
			then
			display $DIRPATH/lowres/low-$base &
		fi
		echo "Please Enter Caption for Image"
		read imagecap
	else
		imagecap=$base
	fi

	echo "<td align=center bgcolor=white><a href=$base><img border=0 src=lowres/low-$base alt=\"\"></a></td>" >> $DIRPATH/index.html
done
	echo "</tr></table>" >> $DIRPATH/index.html
	echo "<center><font size=2 ><a href=\"javascript:window.history.back()\">go back</a></center>" >>  $DIRPATH/index.html
echo ""
if  [ $XWIN = "y" ] || [ $XWIN = "Y" ] 
	then
	echo "Would you like to close the thumbnails on your screen ? (y or n)"
	read KILL_DISPLAY
	if [ $KILL_DISPLAY = "y" ] || [$KILL_DISPLAY = "Y" ]
		then
		/usr/bin/killall display > /dev/null 2>&1
	fi
fi
echo "===================================================================="
echo "Photo Album Has Been Generated"
echo "Please go to :"
echo "file : $DIRPATH/index.html"
echo "with your web browser to view"
echo "===================================================================="
echo ""
echo ""
echo ""
# killall display
exit 0

