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 generate_resized_images $WIDTH $ICONSDIRECTORY
573 function generate_medium() {
574 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
577 function generate_pages() {
578 $ECHOCOMMAND "Generating Pages"
598 if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
602 $FINDCOMMAND . $FINDIMAGESOPTIONS | \
603 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
605 while read imagefilename; do
606 previousimage=$currentimage
607 currentimage=$nextimage
608 nextimage=${imagefilename#./}
611 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
612 if [[ $addlinks == 1 ]]; then
613 addlink=${currentimage}.html
615 addlink=${currentimage}${extra}.html
618 addlink=${currentimage}
621 previouspage=$currentpage
622 currentpage=$nextpage
623 if [[ $addlinks == 1 ]]; then
624 nextpage=${nextimage}${extra}.html
626 nextpage=${nextimage}.html
629 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
630 filename=../${extradir}${currentimage}
632 filename=${extradir}${currentimage}
634 if [ "x$currentpage" != "x" ]; then
635 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
637 $ECHOCOMMAND $nextimage
643 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
644 if [[ $addlinks == 1 ]]; then
645 addlink=${currentpage}.html
647 addlink=${currentpage}${extra}.html
650 addlink=${currentpage}
653 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
654 filename=../${extradir}${currentpage}
656 filename=${extradir}${currentpage}
658 if [[ $addlinks == 1 ]]; then
659 currentpage=${currentpage}${extra}.html
660 previouspage=${previouspage}${extra}.html
662 currentpage=${currentpage}.html
663 previouspage=${previouspage}.html
666 generate_general_page "$previouspage" "$currentpage" "" $addlink
670 function generate_medium_pages() {
671 generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
674 function generate_general_page() {
689 if [[ ! -z $4 ]]; then
693 if [ -r captions.txt ]; then
694 imagefilename=${filename##*/}
695 caption=$($GREPCOMMAND -E "^${imagefilename} " captions.txt); caption=${caption#* }
700 $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
703 if [[ $OUTPUTHTML != 0 ]]; then
708 $ECHOCOMMAND "No path given"
713 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
718 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
723 if [[ ! -d $1 ]]; then
724 $ECHOCOMMAND "$1 is not a directory"
731 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
732 $ECHOCOMMAND "$1 does not contain any images. Quitting."
737 $ECHOCOMMAND "Can't write to images directory, exiting"
741 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
742 $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
746 if [ -e style.css ] && [ ! -w style.css ]; then
747 $ECHOCOMMAND "Can't write style.css, exiting"
753 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
754 if [ ! -d $PAGESDIRECTORY ]; then
755 mkdir -p $PAGESDIRECTORY
758 if [ ! -w $PAGESDIRECTORY ]; then
759 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
762 UNIXPAGESDIRECTORY=$PAGESDIRECTORY/
763 PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
768 UNIXPAGESDIRECTORY=""
771 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
773 generate_medium_pages
776 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
777 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
778 generate_pages __${MEDIUMDIRECTORY}
784 if [ -r description.txt ] ; then
785 DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
792 $ECHOCOMMAND "Starting to generate page"
794 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
795 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
797 $ECHOCOMMAND "Adding Captions"
800 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
801 extra=__${MEDIUMDIRECTORY}
804 $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}
806 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
808 $ECHOCOMMAND "Finished generating the page"
809 $ECHOCOMMAND "Generating stylesheet"
812 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
813 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
817 # add a bit to the maxheight for the size of the caption
818 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
820 $BPGALLERY_STYLESHEET_FUNCTION > style.css
822 $ECHOCOMMAND "All done"
824 if [[ $OUTPUTHTML != 0 ]]; then
825 $ECHOCOMMAND "</pre>"