#!/bin/bash
# Created by Ben Okopnik on Tue Sep 18 20:18:18 EDT 2007

[ -z "$1" ] && { printf "Usage: ${0##*/} <image>\n"; exit; }
[ -f "$1" ] || { printf "$1 does not exist.\n"; exit; }
[ -z "`/usr/bin/file \"$1\"|grep 'image'`" ] && { printf "$1 is not an image file.\n"; exit; }

################### User config ###########################
lf_dir="/var/www/lf"
################### User config ###########################

nameonly=${1##*/}

cd $lf_dir/galleries
galleries=$(echo $(/bin/ls -d *| grep -v '\.conf'))
cd - > /dev/null
echo "Which gallery would you like to add this image to?"

g=`echo $galleries|/bin/sed 's/ /\|/g'`
while [ -z `echo "$choice"|/bin/egrep "$g"` ]
do
	echo "(Choices: $galleries)"
	read choice
done

echo "Enter a one-line title for this image:"
read title
echo "Enter a one-line description for this image:"
read desc

echo "Adding $nameonly to the $choice gallery:"

[ -d "$lf_dir/galleries/$choice/xlarge" ] || /bin/mkdir -p "$lf_dir/galleries/$choice/xlarge"
cp "$1" "$lf_dir/galleries/$choice/xlarge/"

for n in 800x600:large 200x150:small 75x50:preview
do
	size=`echo $n|/bin/sed 's/:.*//'`
	dir=`echo $n|/bin/sed 's/.*://'`
	[ -d "$lf_dir/galleries/$choice/$dir" ] || /bin/mkdir -p "$lf_dir/galleries/$choice/$dir"
	echo "Converting ($dir image)..."
	/usr/bin/convert "$1" -geometry $size "$lf_dir/galleries/$choice/$dir/$nameonly"
done

conf="$lf_dir/galleries/$choice/config"

if ! /bin/grep "^$nameonly}" $conf
then
	echo -n "$nameonly" >> $conf
	if [ -n "$title" ]
	then
		echo -n "} $title" >> $conf
		if [ -n "$desc" ]
		then
			echo -n "} $desc" >> $conf
		fi
	fi
	echo >> $conf
fi


