#!/bin/bash
if [ -z $1 ] ; then NIMI=`date +%d.%m.%Y`; else NIMI="$@" ; fi

echo 'Generating thumbnails to thumbs/'
if [ ! -d thumbs ] ; then mkdir thumbs ; fi
for s in *.jpg ; do echo "Processing: $s" ; convert -geometry 320x420 "$s" "thumbs/th_$s" ; done

prefiksi="Gallery "
echo 'Flooding beginning of index.html'
echo "<html><head><title>$prefiksi$NIMI</title>" > index.html
echo '<style type=\"text/css\"> table { margin-right:auto; margin-left:auto; text-align:center }</style>' >> index.html
echo '<base target=\"_blank\" /></head>' >> index.html
echo '<body bgcolor=\"#000000\" text=\"#FFFFFF\">' >> index.html
echo "<h2 align=\"center\">$prefiksi$NIMI</h2><p>" >> index.html
echo 'Generating tables...'
rivilla=3
x=0
for s in *.jpg
do
	if [ $x -eq $rivilla ] ; then echo '</tr></table>' >> index.html ; x=0 ; fi
	if [ $x -eq 0 ] ; then echo "<table><tr>" >> index.html ; fi
	if [ $x -lt $rivilla ]
	then
		floodbeg="<td><a href=\"$s\"><img src=\"thumbs/th_$s\"></a><br>$s<br>"
		filesize=`identify -format "%b" "$s"`
		resolution=`identify -format "%wx%h" "$s"`
		if [ `expr index $filesize b` -eq 0 ] #if the size is already in bytes do nothing
		then
			filesizekB=`expr $filesize / 1024`
			if [ $filesizekB -gt 1 ] ; then echo $floodbeg$filesizekB"kb "$resolution"</td>" >> index.html ; fi
		else
			echo $floodbeg$filesize' '$resolution >> index.html
		fi
		x=$(($x+1))
	fi
done
echo 'Finalizing index.html'
if [ $x -gt 0 ]
then
	while [ $x -lt $rivilla ] ; do echo '<td>&nbsp;</td>' >> index.html; x=$(($x+1)) ; done
	echo '</tr></table>' >> index.html
fi
echo '</p></body></html>' >> index.html