#!/bin/bash
# create_gallery shell script v. 1.1, 2009-04-17_20:49 GMT+1
# (C) Steffen Wendzel
# License: GPLv2, see http://www.gnu.org
#
# Requirements:
# - bash shell
# - netpbm
#
# Documentation:
# 1. Create a folder bilder_orig where you place in your original pictures you want to publish
# 2. For every file you want to add a comment (HTML title) to, create a file PIC_FILE.comment (PIC_FILE.title). E.g. your
#    picture is named bilder_orig/IMG_204.JPG then your comment file must be named bilder_orig/IMG_204.JPG.comment and a title containing file must be named bilder_orig/IMG_204.JPG.title.
# 3. Run ./create_gallery [gallery title] in the base folder that contains the bilder_orig folder
#    (You can add a short description text in the 2nd parameter ($2 == argv[2]) if you want to do so)
# 4. Upload all the *.html and *.jpg files (but not the bilder_orig folder)

if [ "$1" = "" ]; then
	echo "Need a gallery name as argv[1]"
	exit 1
fi

if [ "$2" = "" ]; then
	echo 'No description text given as argv[2] (not a problem)'
fi

echo '<html><head><title>'$1'</title></head><body>' > index.html
echo '<h1>'$1'</h1>' >> index.html
echo '<p>'$2'</p>' >> index.html

for file in `/bin/ls bilder_orig/*.[Jj][Pp][Gg]`; do
	file=`basename $file`
	echo ${file}...
	jpegtopnm bilder_orig/$file | pnmscale -width 640 -height 480 | pnmtojpeg --quality=90 > ${file}.big.jpg
	jpegtopnm ${file}.big.jpg | pnmscale -width 200 -height 150 | pnmtojpeg --quality=93 > ${file}.tiny.jpg
	echo '<html><head>' > ${file}.html
	# check for a <title> set by the user
	pic_title=''
	if [ -f bilder_orig/${file}.title ]; then
		pic_title=`cat bilder_orig/${file}.title`
		echo '<title>'$pic_title'</title>' >> ${file}.html
	else
		echo '<title>' $1 '/' $file '</title>' >> ${file}.html
	fi
	echo '</head><body>' >> ${file}.html
	if [ "$pic_title" != "" ]; then
		echo "<h1>"$pic_title"</h1>" >> ${file}.html
	fi
	echo '<img src="'$file'.big.jpg" />' >> ${file}.html
	# check for a comment set by the user
	if [ -f bilder_orig/${file}.comment ]; then
		echo '<br /><p>' >> ${file}.html
		cat bilder_orig/${file}.comment >> ${file}.html
		echo '</p>' >> ${file}.html
	fi
	echo '<br />[<a href="index.html">Back to index of "'$1'"</a>]<br />' >> ${file}.html
	echo '</body></html>' >> ${file}.html
	
	# add pic to the preview index page
	echo '<a href="'${file}.html'" alt="'$pic_title'" title="'$pic_title'"><img src="'${file}.tiny.jpg'" /></a>' >> index.html
done

echo '<p>Gallery created with <a href="http://www.wendzel.de/?sub=softw&ssub=creategallery">create_gallery</a>.</p>' >> index.html
echo '</body></html>' >> index.html



