* Update version number ready for release
[bpgallery.git] / bpgallery.sh
1 #!/usr/bin/env bash
2
3 # bpgallery.sh - builds a simple 'gallery' from a collection of images
4 # Copyright (C) 2004 Brett Parker <iDunno@sommitrealweird.co.uk>
5 #
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.
10
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.
15
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
19
20 set -f
21
22 VERSION="1.1.2"
23
24 function bpgallery_default_head() {
25 cat <<END
26 <?xml version="1.0"?>
27 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
28 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
29 <head>
30         <title>${TITLE}</title>
31         <link rel="stylesheet" href="${BASEURL}style.css" type="text/css" />
32 </head>
33 <body>
34 END
35 }
36
37 declare -rf bpgallery_default_head
38
39 function bpgallery_default_description() {
40 cat <<END
41         <h1>${TITLE}</h1>
42         ${DESCRIPTION}
43 END
44 }
45
46 declare -rf bpgallery_default_description
47
48 function bpgallery_default_thumbsline() {
49 if [[ -z $caption ]]; then
50         caption_alt=Unknown
51 else
52         caption_alt=$caption
53 fi
54 cat << END
55 <div class="thumbnail"><a href="$link"><img src="${ICONSDIRECTORY}/$filename" alt="$caption_alt" /></a><div class="caption">$caption</div></div>
56 END
57 }
58
59 declare -rf bpgallery_default_thumbsline
60
61 function bpgallery_default_tail() {
62 cat << END
63 </body>
64 </html>
65 END
66 }
67
68 declare -rf bpgallery_default_tail
69
70 function bpgallery_default_stylesheet() {
71 cat <<END
72 body {
73         background: white;
74         color: black;
75         font-family: sans-serif;
76         font-size: 10pt;
77 }
78
79 div.thumbnail {
80         float: left;
81         padding: 20px;
82         border: 0px;
83         width: ${WIDTH}px;
84         height: ${MAXHEIGHT}px;
85 }
86
87 div.caption {
88         width: 100%;
89         text-align: center;
90         font-weight: bold;
91 }
92
93 div.navigation {
94         margin: 0;
95 }
96
97 div.navigation ul {
98         display: inline;
99         margin: 0;
100         padding: 0;
101         text-align: center;
102 }
103
104 div.navigation ul li {
105         display: inline;
106         margin: 2em;
107 }
108
109 a {
110         border: 0px;
111 }
112
113 img {
114         border: 0px;
115 }
116 END
117 }
118
119 declare -rf bpgallery_default_stylesheet
120
121 function bpgallery_default_page() {
122         $BPGALLERY_HEAD_FUNCTION
123
124         linkstart=""
125         linkend=""
126
127         if [[ ! -z $linklocation ]]; then
128                 linkstart="<a href='$linklocation'>"
129                 linkend="</a>"
130         fi
131         
132         cat <<END
133 <h1>${caption}</h1>
134 <div class="navigation">
135         <ul>
136                 <li><a href='${BASEURL}${INDEXDOCUMENT}'>Thumbnails</a></li>
137 END
138         if [[ ! -z $previouspage ]]; then
139                 $ECHOCOMMAND "<li><a href='${previouspage}'>Previous</a></li>"
140         else
141                 $ECHOCOMMAND "<li>Previous</li>"
142         fi
143
144         if [[ ! -z $nextpage ]]; then
145                 $ECHOCOMMAND "<li><a href='${nextpage}'>Next</a></li>"
146         else
147                 $ECHOCOMMAND "<li>Next</li>"
148         fi
149 cat <<END
150         </ul>
151 </div>
152 <div class="mainimage">
153         ${linkstart}<img src="${filename}" alt="${caption}" />${linkend}
154 </div>
155 END
156         $BPGALLERY_TAIL_FUNCTION
157 }
158
159 declare -rf bpgallery_default_page
160
161 function bpgallery_escape_url() {
162         temp=$1
163         temp=${temp//\%/%25}
164         temp=${temp//:/%3A}
165         temp=${temp//;/%3B}
166         temp=${temp// /%20}
167         temp=${temp//$/%24}
168         temp=${temp//&/%26}
169         temp=${temp//+/%2B}
170         temp=${temp//,/%2C}
171         temp=${temp//\//%2F}
172         temp=${temp//=/%3D}
173         temp=${temp//\?/%3F}
174         temp=${temp//@/%40}
175         temp=${temp//\"/%22}
176         $ECHOCOMMAND $temp
177 }
178
179 function bpgallery_cleanup_directory_name() {
180         temp=$1
181         temp=${temp//\%/_}
182         temp=${temp//:/_}
183         temp=${temp//;/_}
184         temp=${temp// /_}
185         temp=${temp//$/_}
186         temp=${temp//&/_}
187         temp=${temp//+/_}
188         temp=${temp//,/_}
189         temp=${temp//\//_}
190         temp=${temp//=/_}
191         temp=${temp//\?/_}
192         temp=${temp//@/_}
193         temp=${temp//\"/_}
194         $ECHOCOMMAND $temp
195 }
196
197 declare -rf bpgallery_escape_url
198
199 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
200         declare -r BPGALLERY_THEME
201 fi
202
203 if [[ ! -z ${TITLE} ]] ; then
204         declare -r TITLE
205 fi
206
207 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
208         . /usr/local/etc/bpgallery/config 2>/dev/null
209 fi
210
211 if [[ -e /etc/bpgallery/config ]] ; then
212         . /etc/bpgallery/config 2>/dev/null
213 fi
214
215 if [[ -e $HOME/.bpgallery.rc ]]; then
216         . $HOME/.bpgallery.rc 2>/dev/null
217 fi
218
219 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
220         . $1/.bpgallery.rc 2>/dev/null
221 fi
222
223 if [[ -z ${BPGALLERY_THEME} ]]; then
224         BPGALLERY_THEME=default
225 fi
226
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
236 fi
237
238 if [[ -z $TITLE ]]; then 
239         TITLE="Photo Gallery"
240 fi
241
242 if [[ -z $CONVERTTOOL ]]; then
243         hash convert
244         if [[ $? != 0 ]]; then
245                 echo "Can't find convert, exiting"
246                 exit 3
247         fi
248         CONVERTTOOL=$(hash -t convert)
249 else
250         if [[ ! -x $CONVERTTOOL ]]; then
251                 echo "Can't find convert at location $CONVERTTOOL, exiting"
252                 exit 3
253         fi
254 fi
255
256 if [[ -z $FINDCOMMAND ]]; then
257         hash find
258         if [[ $? != 0 ]]; then
259                 echo "Can't find find, exiting"
260                 exit 3
261         fi
262         FINDCOMMAND=$(hash -t find)
263 else
264         if [[ ! -x $FINDCOMMAND ]]; then
265                 echo "Can't find find at $FINDCOMMAND, exiting"
266                 exit 3
267         fi
268 fi
269
270 if [[ -z $XARGSCOMMAND ]]; then
271         hash xargs
272         if [[ $? != 0 ]]; then
273                 echo "Can't find xargs, exiting"
274                 exit 3
275         fi
276         XARGSCOMMAND=$(hash -t xargs)
277 else
278         if [[ ! -x $XARGSCOMMAND ]]; then
279                 echo "Can't find xargs at $XARGSCOMMAND, exiting"
280                 exit 3
281         fi
282 fi
283
284 if [[ -z $ECHOCOMMAND ]]; then
285         ECHOCOMMAND=echo
286 else
287         if [[ ! -x $ECHOCOMMAND ]]; then
288                 echo "Can't find echo at $ECHOCOMMAND, exiting"
289                 exit 3
290         fi
291 fi
292
293 if [[ -z $SORTCOMMAND ]]; then
294         hash sort
295         if [[ $? != 0 ]]; then
296                 echo "Can't find sort, exiting"
297                 exit 3
298         fi
299         SORTCOMMAND=$(hash -t sort)
300 else
301         if [[ ! -x $SORTCOMMAND ]]; then
302                 echo "Can't find sort, exiting"
303                 exit 3
304         fi
305 fi
306
307 if [[ -z $IDENTIFYCOMMAND ]]; then
308         hash identify
309         if [[ $? != 0 ]]; then
310                 echo "Can't find indentify, exiting"
311                 exit 3
312         fi
313         IDENTIFYCOMMAND=$(hash -t identify)
314 else
315         if [[ ! -x $IDENTIFYCOMMAND ]]; then
316                 echo "Can't find identify at $IDENTIFYCOMMAND, exiting"
317                 exit 3
318         fi
319 fi
320
321 if [[ -z $HEADCOMMAND ]]; then
322         hash head
323         if [[ $? != 0 ]]; then
324                 echo "Can't find head, exiting"
325                 exit 3
326         fi
327         HEADCOMMAND=$(hash -t head)
328 else
329         if [[ ! -x $HEADCOMMAND ]]; then
330                 echo "Can't find head at $HEADCOMMAND, exiting"
331                 exit 3
332         fi
333 fi
334
335 if [[ -z $GREPCOMMAND ]]; then
336         hash grep
337         if [[ $? != 0 ]]; then
338                 echo "Can't find grep, exiting"
339                 exit 3
340         fi
341         GREPCOMMAND=$(hash -t grep)
342 else
343         if [[ ! -x $GREPCOMMAND ]]; then
344                 echo "Can't find grep at $GREPCOMMAND, exiting"
345                 exit 3
346         fi
347 fi
348
349 if [[ -z $SEDCOMMAND ]]; then
350         hash sed
351         if [[ $? != 0 ]]; then
352                 echo "Can't find sed, exiting"
353                 exit 3
354         fi
355         SEDCOMMAND=$(hash -t sed)
356 else
357         if [[ ! -x $SEDCOMMAND ]]; then
358                 echo "Can't find sed at $SEDCOMMAND, exiting"
359                 exit 3
360         fi
361 fi
362
363 if [[ -z $WCCOMMAND ]]; then
364         hash wc
365         if [[ $? != 0 ]]; then
366                 echo "Can't find wc, exiting"
367                 exit 3
368         fi
369         WCCOMMAND=$(hash -t wc)
370         WCCOMMAND="$WCCOMMAND -l"
371 else
372         if [[ ! -x $WCCOMMAND ]]; then
373                 echo "Can't find wc at $WCCOMMAND, exiting"
374                 exit 3
375         fi
376         WCCOMMAND="$WCCOMMAND -l"
377 fi
378
379 if [[ -z $WIDTH ]]; then
380         WIDTH=100
381 fi
382
383 if [[ -z $IMAGEEXTENSIONS ]]; then
384         IMAGEEXTENSIONS="jpeg jpg gif png";
385 fi
386
387 if [[ -z $CAPTIONHEIGHT ]]; then
388         CAPTIONHEIGHT=75
389 fi
390
391 if [[ -z $OUTPUTHTML ]]; then
392         OUTPUTHTML=0
393 fi
394
395 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
396         GENERATEPAGESFORFULLSIZE=0
397 fi
398
399 if [[ -z $GENERATEPAGESFORMEDIUMSIZE ]]; then
400         GENERATEPAGESFORMEDIUMSIZE=1
401 fi
402
403 if [[ -z $PAGESDIRECTORY ]]; then
404         PAGESDIRECTORY=""
405 else
406         removetrailingslashes=${PAGESDIRECTORY%/}
407         temp=$removetrailingslashes
408         while [[ $removetrailingslashes != $temp ]]; do
409                 temp=$removetrailingslashes
410                 removetrailingslashes=${temp%/}
411         done
412         PAGESDIRECTORY=$(bpgallery_escape_url "${PAGESDIRECTORY}")
413 fi
414
415 if [[ -z $MEDIUMWIDTH ]]; then
416         MEDIUMWIDTH=400
417 fi
418
419 if [[ -z $INDEXDOCUMENT ]]; then
420         INDEXDOCUMENT=index.html
421 fi
422
423 if [[ -z $MEDIUMDIRECTORY ]]; then
424         MEDIUMDIRECTORY=medium
425 else
426         # Strip out anything that might be broken
427         MEDIUMDIRECTORY=$(bpgallery_cleanup_directory_name "$MEDIUMDIRECTORY")
428 fi
429
430 if [[ -z $ICONSDIRECTORY ]]; then
431         ICONSDIRECTORY=icons
432 else
433         # Strip out anything that might be broken
434         ICONSDIRECTORY=$(bpgallery_cleanup_directory_name "$ICONSDIRECTORY")
435 fi
436
437 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
438         BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
439 else
440         BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
441 fi
442
443 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
444         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
445 else
446         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
447 fi
448
449 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
450         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
451 else
452         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
453 fi
454
455 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
456         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
457 else
458         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
459 fi
460
461 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
462         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
463 else
464         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
465 fi
466
467 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
468         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
469 else
470         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
471 fi
472
473 FINDIMAGESOPTIONS=""
474
475 for imageext in $IMAGEEXTENSIONS; do
476         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
477 done
478
479 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
480 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
481
482 function usage() {
483 cat <<END
484 Usage:
485   $0 [--help|--version|<path to images>]
486
487     --help
488         displays this help screen
489     --version
490         displays the version and exits
491
492 This can also takes some environment variables, these will use defaults if not
493 set.
494
495         TITLE
496             the title of the gallery
497
498         WIDTH
499             the width to make the icons
500
501         MEDIUMWIDTH
502             set the width of images in the medium size pages
503
504         BPGALLERY_THEME
505             set the theme to use (described below)
506
507         BPGALLERY_THEME_DIR
508             set an extra location to look for themes
509
510         OUTPUTHTML
511             sets the script output to be wrapped in a <pre> block
512
513         GENERATEPAGESFORMEDIUMSIZE
514             generate medium sized images and pages
515
516         GENERATEPAGESFORFULLSIZE
517             decide wether to generate pages for the full size images or not
518
519         INDEXDOCUMENT
520             name of the index page (e.g. index.html)
521
522 Example:
523         TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
524 END
525 }
526
527 function version() {
528 cat <<END
529 Version: $VERSION
530 END
531 }
532
533 function generate_resized_images() {
534         $ECHOCOMMAND "Generating $2"
535         currentimage=0
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)
538         
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."
542                 exit 64
543         else
544                 # check if the directory exists and create it other wise
545                 if [ ! -d $2 ]; then
546                         mkdir $2
547                 fi
548
549                 if [ ! -w $2 ]; then
550                         $ECHOCOMMAND "Can't write to $2 directory, exiting"
551                         exit 16
552                 fi
553         fi
554
555         if [ $totalimages -eq $imagestoupdate ]; then
556                 $ECHOCOMMAND "Regenerating all $2"
557         elif [ $imagestoupdate -eq 0 ]; then
558                 $ECHOCOMMAND "No Updated $2, not regenerating"
559                 return
560         else
561                 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
562         fi
563         
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"
567 }
568
569 function generate_thumbs() {
570         if [[ ! -z $HEIGHT ]]; then
571                 generate_resized_images "${WIDTH}x${HEIGHT}" $ICONSDIRECTORY
572         else
573         generate_resized_images $WIDTH $ICONSDIRECTORY
574         fi
575 }
576
577 function generate_medium() {
578         if [[ ! -z $MEDIUMHEIGHT ]]; then
579                 generate_resized_images "${WIDTH}x${HEIGHT}" $MEDIUMDIRECTORY
580         else
581                 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
582         fi
583 }
584
585 function generate_pages() {
586         $ECHOCOMMAND "Generating Pages"
587         previouspage=""
588         currentpage=""
589         nextpage=""
590         previousimage=""
591         currentimage=""
592         nextimage=""
593         extra=""
594         extradir=""
595         addlinks=0
596         
597         if [ ! -z $1 ]; then
598                 extra=$1
599         fi
600
601         if [ ! -z $2 ]; then
602                 extradir=$2/
603                 addlinks=1
604         fi
605
606         if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
607                 addlinks=2
608         fi
609         
610         $FINDCOMMAND . $FINDIMAGESOPTIONS | \
611         $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
612         $SORTCOMMAND -g | \
613         while read imagefilename; do
614                 previousimage=$currentimage
615                 currentimage=$nextimage
616                 nextimage=${imagefilename#./}
617                 addlink=""
618                 
619                 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
620                         if [[ $addlinks == 1 ]]; then
621                                 addlink=${currentimage}.html
622                         else
623                                 addlink=${currentimage}${extra}.html
624                         fi
625                 else
626                         addlink=${currentimage}
627                 fi
628
629                 previouspage=$currentpage
630                 currentpage=$nextpage
631                 if [[ $addlinks == 1 ]]; then
632                         nextpage=${nextimage}${extra}.html
633                 else
634                         nextpage=${nextimage}.html
635                 fi
636
637                 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
638                         filename=../${extradir}${currentimage}
639                 else
640                         filename=${extradir}${currentimage}
641                 fi
642                 if [ "x$currentpage" != "x" ]; then
643                         generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
644                 fi
645                 $ECHOCOMMAND $nextimage
646         done | tail -n 2 | (
647                 read previouspage
648                 read currentpage
649                 addlink=""
650
651                 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
652                         if [[ $addlinks == 1 ]]; then
653                                 addlink=${currentpage}.html
654                         else
655                                 addlink=${currentpage}${extra}.html
656                         fi
657                 else
658                         addlink=${currentpage}
659                 fi
660                 
661                 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
662                         filename=../${extradir}${currentpage}
663                 else
664                         filename=${extradir}${currentpage}
665                 fi
666                 if [[ $addlinks == 1 ]]; then
667                         currentpage=${currentpage}${extra}.html
668                         previouspage=${previouspage}${extra}.html
669                 else
670                         currentpage=${currentpage}.html
671                         previouspage=${previouspage}.html
672                 fi
673                 nextpage=""
674                 generate_general_page "$previouspage" "$currentpage" "" $addlink
675         )
676 }
677
678 function generate_medium_pages() {
679         generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
680 }
681
682 function generate_general_page() {
683
684         if [[ -z $1 ]]; then
685                 previouspage=""
686         fi
687
688         if [[ -z $2 ]]; then
689                 currentpage=""
690                 return
691         fi
692
693         if [[ -z $3 ]]; then
694                 nextpage=""
695         fi
696
697         if [[ ! -z $4 ]]; then
698                 linklocation=$4
699         fi
700
701         if [ -r captions.txt ]; then
702                 imagefilename=${filename##*/}
703                 caption=$($GREPCOMMAND -E "^${imagefilename}    " captions.txt); caption=${caption#*    }
704         else
705                 caption=""
706         fi
707         
708         $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
709 }
710
711 if [[ $OUTPUTHTML != 0 ]]; then
712         $ECHOCOMMAND "<pre>"
713 fi
714
715 if [[ -z $1 ]]; then
716         $ECHOCOMMAND "No path given"
717         usage
718         exit 1
719 fi
720
721 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
722         usage
723         exit 0
724 fi
725
726 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
727         version
728         exit 0
729 fi
730
731 if [[ ! -d $1 ]]; then
732         $ECHOCOMMAND "$1 is not a directory"
733         usage
734         exit 2
735 fi
736
737 cd "$1"
738
739 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
740         $ECHOCOMMAND "$1 does not contain any images. Quitting."
741         exit 4
742 fi
743
744 if [ ! -w . ]; then
745         $ECHOCOMMAND "Can't write to images directory, exiting"
746         exit 8
747 fi
748
749 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
750         $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
751         exit 8
752 fi
753
754 if [ -e style.css ] && [ ! -w style.css ]; then
755         $ECHOCOMMAND "Can't write style.css, exiting"
756         exit 8
757 fi
758
759 generate_thumbs
760
761 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
762         if [ ! -d $PAGESDIRECTORY ]; then
763                 mkdir -p $PAGESDIRECTORY
764         fi
765
766         if [ ! -w $PAGESDIRECTORY ]; then
767                 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
768                 exit 16
769         fi
770         UNIXPAGESDIRECTORY=$PAGESDIRECTORY/     
771         PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
772
773         BASEURL="../"
774 else
775         BASEURL=""
776         UNIXPAGESDIRECTORY=""
777 fi
778
779 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
780         generate_medium
781         generate_medium_pages
782 fi
783
784 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
785         if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
786                 generate_pages __${MEDIUMDIRECTORY}
787         else
788                 generate_pages
789         fi
790 fi
791
792 if [ -r description.txt ] ; then
793         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
794 else
795         DESCRIPTION=""
796 fi
797
798 BASEURL=""
799
800 $ECHOCOMMAND "Starting to generate page"
801
802 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
803 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
804
805 $ECHOCOMMAND "Adding Captions"
806
807 extra=""
808 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
809         extra=__${MEDIUMDIRECTORY}
810 fi
811
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}
813
814 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
815
816 $ECHOCOMMAND "Finished generating the page"
817 $ECHOCOMMAND "Generating stylesheet"
818 cd $ICONSDIRECTORY
819
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)
822
823 cd ..
824
825 # add a bit to the maxheight for the size of the caption
826 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
827
828 $BPGALLERY_STYLESHEET_FUNCTION > style.css
829
830 $ECHOCOMMAND "All done"
831
832 if [[ $OUTPUTHTML != 0 ]]; then
833         $ECHOCOMMAND "</pre>"
834 fi