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