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="${BASEURL}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="$link"><img src="${ICONSDIRECTORY}/$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;
104 div.navigation ul li {
119 declare -rf bpgallery_default_stylesheet
121 function bpgallery_default_page() {
122 $BPGALLERY_HEAD_FUNCTION
127 if [[ ! -z $linklocation ]]; then
128 linkstart="<a href='$linklocation'>"
134 <div class="navigation">
136 <li><a href='${BASEURL}${INDEXDOCUMENT}'>Thumbnails</a></li>
138 if [[ ! -z $previouspage ]]; then
139 $ECHOCOMMAND "<li><a href='${previouspage}'>Previous</a></li>"
141 $ECHOCOMMAND "<li>Previous</li>"
144 if [[ ! -z $nextpage ]]; then
145 $ECHOCOMMAND "<li><a href='${nextpage}'>Next</a></li>"
147 $ECHOCOMMAND "<li>Next</li>"
152 <div class="mainimage">
153 ${linkstart}<img src="${filename}" alt="${caption}" />${linkend}
156 $BPGALLERY_TAIL_FUNCTION
159 declare -rf bpgallery_default_page
161 function bpgallery_escape_url() {
179 function bpgallery_cleanup_directory_name() {
197 declare -rf bpgallery_escape_url
199 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
200 declare -r BPGALLERY_THEME
203 if [[ ! -z ${TITLE} ]] ; then
207 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
208 . /usr/local/etc/bpgallery/config 2>/dev/null
211 if [[ -e /etc/bpgallery/config ]] ; then
212 . /etc/bpgallery/config 2>/dev/null
215 if [[ -e $HOME/.bpgallery.rc ]]; then
216 . $HOME/.bpgallery.rc 2>/dev/null
219 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
220 . $1/.bpgallery.rc 2>/dev/null
223 if [[ -z ${BPGALLERY_THEME} ]]; then
224 BPGALLERY_THEME=default
227 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
228 [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
229 . ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} 2>/dev/null
230 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
231 . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
232 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
233 . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
234 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
235 . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
238 if [[ -z $TITLE ]]; then
239 TITLE="Photo Gallery"
242 if [[ -z $CONVERTTOOL ]]; then
244 if [[ $? != 0 ]]; then
245 echo "Can't find convert, exiting"
248 CONVERTTOOL=$(hash -t convert)
250 if [[ ! -x $CONVERTTOOL ]]; then
251 echo "Can't find convert at location $CONVERTTOOL, exiting"
256 if [[ -z $FINDCOMMAND ]]; then
258 if [[ $? != 0 ]]; then
259 echo "Can't find find, exiting"
262 FINDCOMMAND=$(hash -t find)
264 if [[ ! -x $FINDCOMMAND ]]; then
265 echo "Can't find find at $FINDCOMMAND, exiting"
270 if [[ -z $XARGSCOMMAND ]]; then
272 if [[ $? != 0 ]]; then
273 echo "Can't find xargs, exiting"
276 XARGSCOMMAND=$(hash -t xargs)
278 if [[ ! -x $XARGSCOMMAND ]]; then
279 echo "Can't find xargs at $XARGSCOMMAND, exiting"
284 if [[ -z $ECHOCOMMAND ]]; then
287 if [[ ! -x $ECHOCOMMAND ]]; then
288 echo "Can't find echo at $ECHOCOMMAND, exiting"
293 if [[ -z $SORTCOMMAND ]]; then
295 if [[ $? != 0 ]]; then
296 echo "Can't find sort, exiting"
299 SORTCOMMAND=$(hash -t sort)
301 if [[ ! -x $SORTCOMMAND ]]; then
302 echo "Can't find sort, exiting"
307 if [[ -z $IDENTIFYCOMMAND ]]; then
309 if [[ $? != 0 ]]; then
310 echo "Can't find indentify, exiting"
313 IDENTIFYCOMMAND=$(hash -t identify)
315 if [[ ! -x $IDENTIFYCOMMAND ]]; then
316 echo "Can't find identify at $IDENTIFYCOMMAND, exiting"
321 if [[ -z $HEADCOMMAND ]]; then
323 if [[ $? != 0 ]]; then
324 echo "Can't find head, exiting"
327 HEADCOMMAND=$(hash -t head)
329 if [[ ! -x $HEADCOMMAND ]]; then
330 echo "Can't find head at $HEADCOMMAND, exiting"
335 if [[ -z $GREPCOMMAND ]]; then
337 if [[ $? != 0 ]]; then
338 echo "Can't find grep, exiting"
341 GREPCOMMAND=$(hash -t grep)
343 if [[ ! -x $GREPCOMMAND ]]; then
344 echo "Can't find grep at $GREPCOMMAND, exiting"
349 if [[ -z $SEDCOMMAND ]]; then
351 if [[ $? != 0 ]]; then
352 echo "Can't find sed, exiting"
355 SEDCOMMAND=$(hash -t sed)
357 if [[ ! -x $SEDCOMMAND ]]; then
358 echo "Can't find sed at $SEDCOMMAND, exiting"
363 if [[ -z $WCCOMMAND ]]; then
365 if [[ $? != 0 ]]; then
366 echo "Can't find wc, exiting"
369 WCCOMMAND=$(hash -t wc)
370 WCCOMMAND="$WCCOMMAND -l"
372 if [[ ! -x $WCCOMMAND ]]; then
373 echo "Can't find wc at $WCCOMMAND, exiting"
376 WCCOMMAND="$WCCOMMAND -l"
379 if [[ -z $WIDTH ]]; then
383 if [[ -z $IMAGEEXTENSIONS ]]; then
384 IMAGEEXTENSIONS="jpeg jpg gif png";
387 if [[ -z $CAPTIONHEIGHT ]]; then
391 if [[ -z $OUTPUTHTML ]]; then
395 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
396 GENERATEPAGESFORFULLSIZE=0
399 if [[ -z $GENERATEPAGESFORMEDIUMSIZE ]]; then
400 GENERATEPAGESFORMEDIUMSIZE=1
403 if [[ -z $PAGESDIRECTORY ]]; then
406 removetrailingslashes=${PAGESDIRECTORY%/}
407 temp=$removetrailingslashes
408 while [[ $removetrailingslashes != $temp ]]; do
409 temp=$removetrailingslashes
410 removetrailingslashes=${temp%/}
412 PAGESDIRECTORY=$(bpgallery_escape_url "${PAGESDIRECTORY}")
415 if [[ -z $MEDIUMWIDTH ]]; then
419 if [[ -z $INDEXDOCUMENT ]]; then
420 INDEXDOCUMENT=index.html
423 if [[ -z $MEDIUMDIRECTORY ]]; then
424 MEDIUMDIRECTORY=medium
426 # Strip out anything that might be broken
427 MEDIUMDIRECTORY=$(bpgallery_cleanup_directory_name "$MEDIUMDIRECTORY")
430 if [[ -z $ICONSDIRECTORY ]]; then
433 # Strip out anything that might be broken
434 ICONSDIRECTORY=$(bpgallery_cleanup_directory_name "$ICONSDIRECTORY")
437 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
438 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
440 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
443 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
444 BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
446 BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
449 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
450 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
452 BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
455 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
456 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
458 BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
461 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
462 BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
464 BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
467 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
468 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
470 BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
475 for imageext in $IMAGEEXTENSIONS; do
476 FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
479 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
480 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
485 $0 [--help|--version|<path to images>]
488 displays this help screen
490 displays the version and exits
492 This can also takes some environment variables, these will use defaults if not
496 the title of the gallery
499 the width to make the icons
502 set the width of images in the medium size pages
505 set the theme to use (described below)
508 set an extra location to look for themes
511 sets the script output to be wrapped in a <pre> block
513 GENERATEPAGESFORMEDIUMSIZE
514 generate medium sized images and pages
516 GENERATEPAGESFORFULLSIZE
517 decide wether to generate pages for the full size images or not
520 name of the index page (e.g. index.html)
523 TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
533 function generate_resized_images() {
534 $ECHOCOMMAND "Generating $2"
536 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
537 imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | while read filename; do filename=${filename//\"/\\\"}; if [ ! -r "$2/$filename" ] || [ "$filename" -nt "$2/$filename" ] || [ $($IDENTIFYCOMMAND -format "%w" "$2/$filename") -ne $1 ]; then $ECHOCOMMAND $filename; fi; done | $WCCOMMAND)
539 if [ $totalimages -eq 0 ]; then
540 # might as well exit at this point - there are no pictures
541 $ECHOCOMMAND "No images for the gallery - exiting."
544 # check if the directory exists and create it other wise
550 $ECHOCOMMAND "Can't write to $2 directory, exiting"
555 if [ $totalimages -eq $imagestoupdate ]; then
556 $ECHOCOMMAND "Regenerating all $2"
557 elif [ $imagestoupdate -eq 0 ]; then
558 $ECHOCOMMAND "No Updated $2, not regenerating"
561 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
564 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | while read filename; do tempfilename=${filename//\"/\\\"/}; if [ ! -r "$2/$tempfilename" ] || [ "$tempfilename" -nt "$2/$tempfilename" ] || [ $($IDENTIFYCOMMAND -format "%w" "$2/$filename") -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
565 $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
566 $ECHOCOMMAND "Completed generating $2 for $totalimages images"
569 function generate_thumbs() {
570 if [[ ! -z $HEIGHT ]]; then
571 generate_resized_images "${WIDTH}x${HEIGHT}" $ICONSDIRECTORY
573 generate_resized_images $WIDTH $ICONSDIRECTORY
577 function generate_medium() {
578 if [[ ! -z $MEDIUMHEIGHT ]]; then
579 generate_resized_images "${WIDTH}x${HEIGHT}" $MEDIUMDIRECTORY
581 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
585 function generate_pages() {
586 $ECHOCOMMAND "Generating Pages"
606 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
610 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
611 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
613 while read imagefilename; do
614 previousimage=$currentimage
615 currentimage=$nextimage
616 nextimage=${imagefilename#./}
619 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
620 if [[ $addlinks == 1 ]]; then
621 addlink=${currentimage}.html
623 addlink=${currentimage}${extra}.html
626 addlink=${currentimage}
629 previouspage=$currentpage
630 currentpage=$nextpage
631 if [[ $addlinks == 1 ]]; then
632 nextpage=${nextimage}${extra}.html
634 nextpage=${nextimage}.html
637 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
638 filename=../${extradir}${currentimage}
640 filename=${extradir}${currentimage}
642 if [ "x$currentpage" != "x" ]; then
643 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
645 $ECHOCOMMAND $nextimage
651 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
652 if [[ $addlinks == 1 ]]; then
653 addlink=${currentpage}.html
655 addlink=${currentpage}${extra}.html
658 addlink=${currentpage}
661 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
662 filename=../${extradir}${currentpage}
664 filename=${extradir}${currentpage}
666 if [[ $addlinks == 1 ]]; then
667 currentpage=${currentpage}${extra}.html
668 previouspage=${previouspage}${extra}.html
670 currentpage=${currentpage}.html
671 previouspage=${previouspage}.html
674 generate_general_page "$previouspage" "$currentpage" "" $addlink
678 function generate_medium_pages() {
679 generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
682 function generate_general_page() {
697 if [[ ! -z $4 ]]; then
701 if [ -r captions.txt ]; then
702 imagefilename=${filename##*/}
703 caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
708 $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
711 if [[ $OUTPUTHTML != 0 ]]; then
716 $ECHOCOMMAND "No path given"
721 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
726 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
731 if [[ ! -d $1 ]]; then
732 $ECHOCOMMAND "$1 is not a directory"
739 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
740 $ECHOCOMMAND "$1 does not contain any images. Quitting."
745 $ECHOCOMMAND "Can't write to images directory, exiting"
749 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
750 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
754 if [ -e style.css ] && [ ! -w style.css ]; then
755 $ECHOCOMMAND "Can't write style.css, exiting"
761 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
762 if [ ! -d $PAGESDIRECTORY ]; then
763 mkdir -p $PAGESDIRECTORY
766 if [ ! -w $PAGESDIRECTORY ]; then
767 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
770 UNIXPAGESDIRECTORY=$PAGESDIRECTORY/
771 PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
776 UNIXPAGESDIRECTORY=""
779 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
781 generate_medium_pages
784 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
785 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
786 generate_pages __${MEDIUMDIRECTORY}
792 if [ -r description.txt ] ; then
793 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
800 $ECHOCOMMAND "Starting to generate page"
802 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
803 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
805 $ECHOCOMMAND "Adding Captions"
808 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
809 extra=__${MEDIUMDIRECTORY}
812 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $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; if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then link=${PAGESDIRECTORY}$(bpgallery_escape_url "${filename}${extra}.html"); elif [ $GENERATEPAGESFORFULLSIZE != 0 ]; then link=${PAGESDIRECTORY}$(bpgallery_escape_url "${filename}.html"); else link=$(bpgallery_escape_url "$filename"); fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> ${INDEXDOCUMENT}
814 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
816 $ECHOCOMMAND "Finished generating the page"
817 $ECHOCOMMAND "Generating stylesheet"
820 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
821 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
825 # add a bit to the maxheight for the size of the caption
826 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
828 $BPGALLERY_STYLESHEET_FUNCTION > style.css
830 $ECHOCOMMAND "All done"
832 if [[ $OUTPUTHTML != 0 ]]; then
833 $ECHOCOMMAND "</pre>"