3 # bpgallery.sh - builds a simple 'gallery' from a collection of images
4 # Copyright (C) 2004 Brett Parker <iDunno@sommitrealweird.co.uk>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 function bpgallery_default_head() {
27 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
28 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
30 <title>${TITLE}</title>
31 <link rel="stylesheet" href="style.css" type="text/css" />
37 declare -rf bpgallery_default_head
39 function bpgallery_default_description() {
46 declare -rf bpgallery_default_description
48 function bpgallery_default_thumbsline() {
49 if [[ -z $caption ]]; then
55 <div class="thumbnail"><a href="$filename"><img src="icons/$filename" alt="$caption_alt" /></a><div class="caption">$caption</div></div>
59 declare -rf bpgallery_default_thumbsline
61 function bpgallery_default_tail() {
68 declare -rf bpgallery_default_tail
70 function bpgallery_default_stylesheet() {
75 font-family: sans-serif;
84 height: ${MAXHEIGHT}px;
103 declare -rf bpgallery_default_stylesheet
105 function bpgallery_escape_url() {
123 declare -rf bpgallery_escape_url
125 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
126 declare -r BPGALLERY_THEME
129 if [[ ! -z ${TITLE} ]] ; then
133 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
134 . /usr/local/etc/bpgallery/config 2>/dev/null
137 if [[ -e /etc/bpgallery/config ]] ; then
138 . /etc/bpgallery/config 2>/dev/null
141 if [[ -e $HOME/.bpgallery.rc ]]; then
142 . $HOME/.bpgallery.rc 2>/dev/null
145 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
146 . $1/.bpgallery.rc 2>/dev/null
149 if [[ -z ${BPGALLERY_THEME} ]]; then
150 BPGALLERY_THEME=default
153 if [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
154 . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
155 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
156 . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
157 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
158 . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
161 if [[ -z $TITLE ]]; then
162 TITLE="Photo Gallery"
165 if [[ -z $CONVERTTOOL ]]; then
169 if [[ -z $FINDCOMMAND ]]; then
173 if [[ -z $XARGSCOMMAND ]]; then
177 if [[ -z $ECHOCOMMAND ]]; then
181 if [[ -z $SORTCOMMAND ]]; then
185 if [[ -z $IDENTIFYCOMMAND ]]; then
186 IDENTIFYCOMMAND=identify
189 if [[ -z $HEADCOMMAND ]]; then
193 if [[ -z $GREPCOMMAND ]]; then
197 if [[ -z $SEDCOMMAND ]]; then
201 if [[ -z $WIDTH ]]; then
205 if [[ -z $IMAGEEXTENSIONS ]]; then
206 IMAGEEXTENSIONS="jpeg jpg gif png";
209 if [[ -z $WCCOMMAND ]]; then
213 if [[ -z $CAPTIONHEIGHT ]]; then
217 if [[ -z $OUTPUTHTML ]]; then
221 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
222 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
224 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
227 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
228 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
230 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
233 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
234 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
236 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
239 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
240 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
242 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
245 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
246 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
248 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
253 for imageext in $IMAGEEXTENSIONS; do
254 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
257 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
258 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
263 $0 [--help|--version|<path to images>]
266 displays this help screen
268 displays the version and exits
279 function generate_thumbs() {
280 $ECHOCOMMAND "Generating Thumbnails"
282 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | $WCCOMMAND);
283 imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | while read filename; do filename=${filename//\"/\\\"}; if [ ! -r "icons/$filename" ] || [ "$filename" -nt "icons/$filename" ] || [ $($IDENTIFYCOMMAND -format "%w" "icons/$filename") -ne $WIDTH ]; then $ECHOCOMMAND $filename; fi; done | $WCCOMMAND)
285 if [ $totalimages -eq $imagestoupdate ]; then
286 $ECHOCOMMAND "Regenerating all thumbnails"
287 elif [ $imagestoupdate -eq 0 ]; then
288 $ECHOCOMMAND "No Updated Thumbs, not regenerating"
291 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages thumbnails"
294 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | while read filename; do tempfilename=${filename//\"/\\\"/}; if [ ! -r "icons/$tempfilename" ] || [ "$tempfilename" -nt "icons/$tempfilename" ] || [ $($IDENTIFYCOMMAND -format "%w" "icons/$filename") -ne $WIDTH ]; then $ECHOCOMMAND -n $filename; $ECHOCOMMAND -n -e "\000"; fi; done | $XARGSCOMMAND -0 --verbose --max-procs=4 --replace $CONVERTTOOL -resize $WIDTH '{}' 'icons/{}' 2>&1 | while read throwout; do $ECHOCOMMAND done: $currentimage/$imagestoupdate images; currentimage=$((currentimage+1)); done
295 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
296 $ECHOCOMMAND "Completed generating thumbs for $totalimages images"
299 if [[ $OUTPUTHTML != 0 ]]; then
304 $ECHOCOMMAND "No path given"
309 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
314 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
319 if [[ ! -d $1 ]]; then
320 $ECHOCOMMAND "$1 is not a directory"
327 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
328 $ECHOCOMMAND "$1 does not contain any images. Quitting."
332 if [ ! -d icons ]; then
338 if [ -r description.txt ] ; then
339 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
345 $ECHOCOMMAND "Starting to generate page"
347 $BPGALLERY_HEAD_FUNCTION > index.html
348 $BPGALLERY_DESCRIPTION_FUNCTION >> index.html
350 $ECHOCOMMAND "Adding Captions"
352 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} |$SORTCOMMAND -g | while read filename; do filename=${filename#./}; if [ -r captions.txt ]; then caption=$($GREPCOMMAND -E "^$filename " captions.txt); caption=${caption#* }; else caption=""; fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> index.html
354 $BPGALLERY_TAIL_FUNCTION >> index.html
356 $ECHOCOMMAND "Finished generating the page"
357 $ECHOCOMMAND "Generating stylesheet"
360 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
361 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
365 # add a bit to the maxheight for the size of the caption
366 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
368 $BPGALLERY_STYLESHEET_FUNCTION > style.css
370 $ECHOCOMMAND "All done"
372 if [[ $OUTPUTHTML != 0 ]]; then
373 $ECHOCOMMAND "</pre>"