#! /bin/sh

PATH=/usr/local/bin:/usr/X11R6/bin:/usr/bin:/bin

# Get shell parameters
echo "Width: $1"
width="$1"; shift
echo "Height: $1"
height="$1"; shift
echo "Cachebase: $1"
cachebase="$1"; shift
echo "Album: $1"
album="$1"; shift
echo "Source: $1"
source="$1"; shift
dest=$cachebase/$album/`basename "$1"`; shift
echo "Dest: ${dest}"

# Do some sanity checks.  If anyone can find a way to get around all
# this, I'd love to hear it.  I really ought to rewrite this entire
# thing in C, where I don't have to worry about escaping problems.
if [ ! -r "$source" ]; then
    exit 1
fi

# Create the directory if it doesn't already exist
mkdir -p "$cachebase/$album"

# Convert the file
convert -antialias -quality 80  -resize "${width}x${height}" "${source}" "${dest}"
#convert -quality 80 -antialias -resize -filter Sinc "${width}x${height}" "${source}" "${dest}"
#echo convert -quality 80 -antialias -sample "${width}x${height}" "${source}" "${dest}" >> conv.log

# Take care of files that have >1 image inside them (ie. animated GIFs)
if [ -f "${dest}.0" ]; then
    mv "${dest}.0" "$dest"
    rm "${dest}".*
fi
