+function check_dimensions() {
+ wantedwidth=$1
+ wantedheight=$2
+ dir=$3
+ filename=$4
+ if [ ! -e "$dir/$filename" ]; then
+ echo 0
+ return
+ fi
+ iconwidth=$($IDENTIFYCOMMAND -format "%wx%h" "$dir/$filename")
+ iconheight=${iconwidth//*x}
+ iconwidth=${iconwidth//x*}
+ # are the dimensions correctish?
+ imagesizeright=1
+ if [ ! -z $wantedheight ]; then
+ if [ $wantedwidth -ne $iconwidth ] && [ $wantedheight -ne $iconheight ]; then
+ imagesizeright=0
+ else
+ # make sure that both of the dimensions are smaller than they
+ # should be
+ if [ $iconwidth -gt $wantedwidth ] || [ $iconheight -gt $wantedheight ]; then
+ imagesizeright=0
+ fi
+ fi
+ else
+ if [ $wantedwidth -ne $iconwidth ]; then
+ imagesizeright=0
+ fi
+ fi
+ echo $imagesizeright
+}
+
+function generate_resized_images() {
+ $ECHOCOMMAND "Generating $2"
+ width=$1
+ height=${width//*x}
+ if [ $width != $height ]; then
+ width=${width//x*}
+ else
+ height=""
+ fi
+ currentimage=0
+ totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
+ imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS |
+ $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} |
+ while read filename; do
+ filename=${filename//\"/\\\"}
+ imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
+ if [ ! -r "$2/$filename" ] ||
+ [ "$filename" -nt "$2/$filename" ] ||
+ [ $imagesizeright -ne 1 ]; then
+ $ECHOCOMMAND $filename
+ fi
+ done |
+ $WCCOMMAND)
+
+ if [ $totalimages -eq 0 ]; then
+ # might as well exit at this point - there are no pictures
+ $ECHOCOMMAND "No images for the gallery - exiting."
+ exit 64
+ else
+ # check if the directory exists and create it other wise
+ if [ ! -d $2 ]; then
+ mkdir $2
+ fi
+
+ if [ ! -w $2 ]; then
+ $ECHOCOMMAND "Can't write to $2 directory, exiting"
+ exit 16
+ fi
+ fi
+
+ if [ $totalimages -eq $imagestoupdate ]; then
+ $ECHOCOMMAND "Regenerating all $2"
+ elif [ $imagestoupdate -eq 0 ]; then
+ $ECHOCOMMAND "No Updated $2, not regenerating"
+ return
+ else
+ $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
+ fi
+
+ $FINDCOMMAND . $FINDIMAGESOPTIONS | \
+ $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
+ while read filename; do
+ tempfilename=${filename//\"/\\\"/};
+ imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
+ if [ ! -r "$2/$tempfilename" ] \
+ || [ "$tempfilename" -nt "$2/$tempfilename" ] \
+ || [ $imagesizeright -ne 1 ]; then
+ $ECHOCOMMAND -n $filename
+ $ECHOCOMMAND -n -e "\000"
+ fi
+ done | \
+ $XARGSCOMMAND -0 --verbose --max-procs=4 -I {} $CONVERTTOOL -resize $1 '{}' $2/'{}' 2>&1 | \
+ while read throwout; do
+ $ECHOCOMMAND done: $currentimage/$imagestoupdate images
+ currentimage=$((currentimage+1))
+ done
+ $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
+ $ECHOCOMMAND "Completed generating $2 for $totalimages images"
+}
+
+function generate_thumbs() {
+ if [ ! -z $HEIGHT ]; then
+ generate_resized_images "${WIDTH}x${HEIGHT}" $ICONSDIRECTORY
+ else
+ generate_resized_images $WIDTH $ICONSDIRECTORY
+ fi
+}
+
+function generate_medium() {
+ if [ ! -z $MEDIUMHEIGHT ]; then
+ generate_resized_images "${MEDIUMWIDTH}x${MEDIUMHEIGHT}" $MEDIUMDIRECTORY
+ else
+ generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
+ fi
+}
+
+function generate_pages() {
+ $ECHOCOMMAND "Generating Pages"
+ previouspage=""
+ currentpage=""
+ nextpage=""
+ previousimage=""
+ currentimage=""
+ nextimage=""
+ extra=""
+ extradir=""
+ addlinks=0
+
+ if [ ! -z $1 ]; then
+ extra=$1
+ fi
+
+ if [ ! -z $2 ]; then
+ extradir=$2/
+ addlinks=1
+ fi
+
+ if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
+ addlinks=2
+ fi
+
+ $FINDCOMMAND . $FINDIMAGESOPTIONS | \
+ $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
+ $SORTCOMMAND -g | \
+ while read imagefilename; do
+ previousimage=$currentimage
+ currentimage=$nextimage
+ nextimage=${imagefilename#./}
+ addlink=""
+
+ if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
+ if [[ $addlinks == 1 ]]; then
+ addlink=${currentimage}.html
+ else
+ addlink=${currentimage}${extra}.html
+ fi
+ else
+ addlink=${currentimage}
+ fi
+
+ previouspage=$currentpage
+ currentpage=$nextpage
+ if [[ $addlinks == 1 ]]; then
+ nextpage=${nextimage}${extra}.html
+ else
+ nextpage=${nextimage}.html
+ fi
+
+ if [[ "x$PAGESDIRECTORY" != "x" ]]; then
+ filename=../${extradir}${currentimage}
+ else
+ filename=${extradir}${currentimage}
+ fi
+ if [ "x$currentpage" != "x" ]; then
+ generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
+ fi
+ $ECHOCOMMAND $nextimage
+ done | tail -n 2 | (
+ read previouspage
+ read currentpage
+ addlink=""
+
+ if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
+ if [[ $addlinks == 1 ]]; then
+ addlink=${currentpage}.html
+ else
+ addlink=${currentpage}${extra}.html
+ fi
+ else
+ addlink=${currentpage}
+ fi
+
+ if [[ "x$PAGESDIRECTORY" != "x" ]]; then
+ filename=../${extradir}${currentpage}
+ else
+ filename=${extradir}${currentpage}
+ fi
+ if [[ $addlinks == 1 ]]; then
+ currentpage=${currentpage}${extra}.html
+ previouspage=${previouspage}${extra}.html
+ else
+ currentpage=${currentpage}.html
+ previouspage=${previouspage}.html
+ fi
+ nextpage=""
+ generate_general_page "$previouspage" "$currentpage" "" $addlink
+ )
+}
+
+function generate_medium_pages() {
+ generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
+}
+
+function generate_general_page() {
+
+ if [[ -z $1 ]]; then
+ previouspage=""
+ fi
+
+ if [[ -z $2 ]]; then
+ currentpage=""
+ return
+ fi
+
+ if [[ -z $3 ]]; then
+ nextpage=""
+ fi
+
+ if [[ ! -z $4 ]]; then
+ linklocation=$4
+ fi
+
+ if [ -r captions.txt ]; then
+ imagefilename=${filename##*/}
+ caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
+ else
+ caption=""
+ fi
+
+ $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
+}
+
+if [[ $OUTPUTHTML != 0 ]]; then
+ $ECHOCOMMAND "<pre>"
+fi
+