]> git.sommitrealweird.co.uk Git - bpgallery.git/blob - bpgallery.sh
e3110fdcf2cec20b0faf7505174ad9171c73d315
[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-2008 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, but
12 # 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 along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 set -f
21
22 VERSION="1.1.4"
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 $MAXTHREADS ]]; then
381         # if there's more than 2G of memory, then do 4 threads, otherwise, just 1
382         __total_memory=$(free -t -g | sed -ne '2 { s#Mem:[[:space:]]*\([0-9]*\)[[:space:]]*.*$#\1#; p; }')
383         if [[ $__total_memory -ge 2 ]]; then
384                 MAXTHREADS=4
385         else
386                 MAXTHREADS=1
387         fi
388 fi
389
390 if [[ -z $WIDTH ]]; then
391         WIDTH=100
392 fi
393
394 if [[ -z $IMAGEEXTENSIONS ]]; then
395         IMAGEEXTENSIONS="jpeg jpg gif png";
396 fi
397
398 if [[ -z $CAPTIONHEIGHT ]]; then
399         CAPTIONHEIGHT=75
400 fi
401
402 if [[ -z $OUTPUTHTML ]]; then
403         OUTPUTHTML=0
404 fi
405
406 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
407         GENERATEPAGESFORFULLSIZE=0
408 fi
409
410 if [[ -z $GENERATEPAGESFORMEDIUMSIZE ]]; then
411         GENERATEPAGESFORMEDIUMSIZE=1
412 fi
413
414 if [[ -z $PAGESDIRECTORY ]]; then
415         PAGESDIRECTORY=""
416 else
417         removetrailingslashes=${PAGESDIRECTORY%/}
418         temp=$removetrailingslashes
419         while [[ $removetrailingslashes != $temp ]]; do
420                 temp=$removetrailingslashes
421                 removetrailingslashes=${temp%/}
422         done
423         PAGESDIRECTORY=$(bpgallery_escape_url "${PAGESDIRECTORY}")
424 fi
425
426 if [[ -z $MEDIUMWIDTH ]]; then
427         MEDIUMWIDTH=400
428 fi
429
430 if [[ -z $INDEXDOCUMENT ]]; then
431         INDEXDOCUMENT=index.html
432 fi
433
434 if [[ -z $MEDIUMDIRECTORY ]]; then
435         MEDIUMDIRECTORY=medium
436 else
437         # Strip out anything that might be broken
438         MEDIUMDIRECTORY=$(bpgallery_cleanup_directory_name "$MEDIUMDIRECTORY")
439 fi
440
441 if [[ -z $ICONSDIRECTORY ]]; then
442         ICONSDIRECTORY=icons
443 else
444         # Strip out anything that might be broken
445         ICONSDIRECTORY=$(bpgallery_cleanup_directory_name "$ICONSDIRECTORY")
446 fi
447
448 if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
449         BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
450 else
451         BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
452 fi
453
454 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
455         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
456 else
457         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
458 fi
459
460 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
461         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
462 else
463         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
464 fi
465
466 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
467         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
468 else
469         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
470 fi
471
472 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
473         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
474 else
475         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
476 fi
477
478 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
479         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
480 else
481         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
482 fi
483
484 FINDIMAGESOPTIONS=""
485
486 for imageext in $IMAGEEXTENSIONS; do
487         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
488 done
489
490 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
491 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
492
493 function usage() {
494 cat <<END
495 Usage:
496   $0 [--help|--version|<path to images>]
497
498     --help
499         displays this help screen
500     --version
501         displays the version and exits
502
503 This can also takes some environment variables, these will use defaults if not
504 set.
505
506         TITLE
507             the title of the gallery
508
509         WIDTH
510             the width to make the icons
511
512         HEIGHT
513             the height to make the icons (sets up a bounding box with WIDTH)
514
515         MEDIUMWIDTH
516             set the width of images in the medium size pages
517
518         MEDIUMHEIGHT
519             the height to make images in the medium size pages (sets up a
520             bounding box with MEDIUMWIDTH)
521
522         BPGALLERY_THEME
523             set the theme to use (described below)
524
525         BPGALLERY_THEME_DIR
526             set an extra location to look for themes
527
528         OUTPUTHTML
529             sets the script output to be wrapped in a <pre> block
530
531         GENERATEPAGESFORMEDIUMSIZE
532             generate medium sized images and pages
533
534         GENERATEPAGESFORFULLSIZE
535             decide wether to generate pages for the full size images or not
536
537         INDEXDOCUMENT
538             name of the index page (e.g. index.html)
539
540 Example:
541         TITLE="My Funky Gallery" WIDTH=200 INDEXDOCUMENT="welcome.html" GENERATEPAGESFORFULLSIZE=1 GENERATEPAGESFORMEDIUMSIZE=1 MEDIUMWIDTH=400 $0 /path/to/image/folder
542 END
543 }
544
545 function version() {
546 cat <<END
547 Version: $VERSION
548 END
549 }
550
551 function check_dimensions() {
552         wantedwidth=$1
553         wantedheight=$2
554         dir=$3
555         filename=$4
556         if [ ! -e "$dir/$filename" ]; then
557                 echo 0
558                 return
559         fi
560         iconwidth=$($IDENTIFYCOMMAND -format "%wx%h" "$dir/$filename")
561         iconheight=${iconwidth//*x}
562         iconwidth=${iconwidth//x*}
563         # are the dimensions correctish?
564         imagesizeright=1
565         if [ ! -z $wantedheight ]; then
566                 if [ $wantedwidth -ne $iconwidth ] && [ $wantedheight -ne $iconheight ]; then
567                         imagesizeright=0
568                 else
569                         # make sure that both of the dimensions are smaller than they
570                         # should be
571                         if [ $iconwidth -gt $wantedwidth ] || [ $iconheight -gt $wantedheight ]; then
572                                 imagesizeright=0
573                         fi
574                 fi
575         else
576                 if [ $wantedwidth -ne $iconwidth ]; then
577                         imagesizeright=0
578                 fi
579         fi
580         echo $imagesizeright
581 }
582
583 function generate_resized_images() {
584         $ECHOCOMMAND "Generating $2"
585         width=$1
586         height=${width//*x}
587         if [ $width != $height ]; then
588                 width=${width//x*}
589         else
590                 height=""
591         fi
592         currentimage=0
593         totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
594         imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS |
595                 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} |
596                 while read filename; do
597                         filename=${filename//\"/\\\"}
598                         imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
599                         if [ ! -r "$2/$filename" ] ||
600                                 [ "$filename" -nt "$2/$filename" ] ||
601                                 [ $imagesizeright -ne 1 ]; then
602                                 $ECHOCOMMAND $filename
603                         fi
604                 done |
605                 $WCCOMMAND)
606         
607         if [ $totalimages -eq 0 ]; then
608                 # might as well exit at this point - there are no pictures
609                 $ECHOCOMMAND "No images for the gallery - exiting."
610                 exit 64
611         else
612                 # check if the directory exists and create it other wise
613                 if [ ! -d $2 ]; then
614                         mkdir $2
615                 fi
616
617                 if [ ! -w $2 ]; then
618                         $ECHOCOMMAND "Can't write to $2 directory, exiting"
619                         exit 16
620                 fi
621         fi
622
623         if [ $totalimages -eq $imagestoupdate ]; then
624                 $ECHOCOMMAND "Regenerating all $2"
625         elif [ $imagestoupdate -eq 0 ]; then
626                 $ECHOCOMMAND "No Updated $2, not regenerating"
627                 return
628         else
629                 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
630         fi
631         
632         $FINDCOMMAND . $FINDIMAGESOPTIONS | \
633                 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
634                 while read filename; do 
635                         tempfilename=${filename//\"/\\\"/}; 
636                         imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
637                         if [ ! -r "$2/$tempfilename" ] \
638                                 || [ "$tempfilename" -nt "$2/$tempfilename" ] \
639                                 || [ $imagesizeright -ne 1 ]; then 
640                                         $ECHOCOMMAND -n $filename
641                                         $ECHOCOMMAND -n -e "\000"
642                         fi
643                 done | \
644                 $XARGSCOMMAND -0 --verbose --max-procs=$MAXTHREADS -I {} $CONVERTTOOL -resize $1 '{}' $2/'{}' 2>&1 | \
645                         while read throwout; do
646                                 $ECHOCOMMAND done: $currentimage/$imagestoupdate images
647                                 currentimage=$((currentimage+1))
648                         done
649         $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
650         $ECHOCOMMAND "Completed generating $2 for $totalimages images"
651 }
652
653 function generate_thumbs() {
654         if [ ! -z $HEIGHT ]; then
655                 generate_resized_images "${WIDTH}x${HEIGHT}" $ICONSDIRECTORY
656         else
657         generate_resized_images $WIDTH $ICONSDIRECTORY
658         fi
659 }
660
661 function generate_medium() {
662         if [ ! -z $MEDIUMHEIGHT ]; then
663                 generate_resized_images "${MEDIUMWIDTH}x${MEDIUMHEIGHT}" $MEDIUMDIRECTORY
664         else
665                 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
666         fi
667 }
668
669 function generate_pages() {
670         $ECHOCOMMAND "Generating Pages"
671         previouspage=""
672         currentpage=""
673         nextpage=""
674         previousimage=""
675         currentimage=""
676         nextimage=""
677         extra=""
678         extradir=""
679         addlinks=0
680         
681         if [ ! -z $1 ]; then
682                 extra=$1
683         fi
684
685         if [ ! -z $2 ]; then
686                 extradir=$2/
687                 addlinks=1
688         fi
689
690         if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
691                 addlinks=2
692         fi
693         
694         $FINDCOMMAND . $FINDIMAGESOPTIONS | \
695         $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
696         $SORTCOMMAND -g | \
697         while read imagefilename; do
698                 previousimage=$currentimage
699                 currentimage=$nextimage
700                 nextimage=${imagefilename#./}
701                 addlink=""
702                 
703                 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
704                         if [[ $addlinks == 1 ]]; then
705                                 addlink=${currentimage}.html
706                         else
707                                 addlink=${currentimage}${extra}.html
708                         fi
709                 else
710                         addlink=${currentimage}
711                 fi
712
713                 previouspage=$currentpage
714                 currentpage=$nextpage
715                 if [[ $addlinks == 1 ]]; then
716                         nextpage=${nextimage}${extra}.html
717                 else
718                         nextpage=${nextimage}.html
719                 fi
720
721                 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
722                         filename=../${extradir}${currentimage}
723                 else
724                         filename=${extradir}${currentimage}
725                 fi
726                 if [ "x$currentpage" != "x" ]; then
727                         generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
728                 fi
729                 $ECHOCOMMAND $nextimage
730         done | tail -n 2 | (
731                 read previouspage
732                 read currentpage
733                 addlink=""
734
735                 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
736                         if [[ $addlinks == 1 ]]; then
737                                 addlink=${currentpage}.html
738                         else
739                                 addlink=${currentpage}${extra}.html
740                         fi
741                 else
742                         addlink=${currentpage}
743                 fi
744                 
745                 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
746                         filename=../${extradir}${currentpage}
747                 else
748                         filename=${extradir}${currentpage}
749                 fi
750                 if [[ $addlinks == 1 ]]; then
751                         currentpage=${currentpage}${extra}.html
752                         previouspage=${previouspage}${extra}.html
753                 else
754                         currentpage=${currentpage}.html
755                         previouspage=${previouspage}.html
756                 fi
757                 nextpage=""
758                 generate_general_page "$previouspage" "$currentpage" "" $addlink
759         )
760 }
761
762 function generate_medium_pages() {
763         generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
764 }
765
766 function generate_general_page() {
767
768         if [[ -z $1 ]]; then
769                 previouspage=""
770         fi
771
772         if [[ -z $2 ]]; then
773                 currentpage=""
774                 return
775         fi
776
777         if [[ -z $3 ]]; then
778                 nextpage=""
779         fi
780
781         if [[ ! -z $4 ]]; then
782                 linklocation=$4
783         fi
784
785         if [ -r captions.txt ]; then
786                 imagefilename=${filename##*/}
787                 caption=$($GREPCOMMAND -E "^${imagefilename}    " captions.txt); caption=${caption#*    }
788         else
789                 caption=""
790         fi
791         
792         $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
793 }
794
795 if [[ $OUTPUTHTML != 0 ]]; then
796         $ECHOCOMMAND "<pre>"
797 fi
798
799 if [[ -z $1 ]]; then
800         $ECHOCOMMAND "No path given"
801         usage
802         exit 1
803 fi
804
805 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
806         usage
807         exit 0
808 fi
809
810 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
811         version
812         exit 0
813 fi
814
815 if [[ ! -d $1 ]]; then
816         $ECHOCOMMAND "$1 is not a directory"
817         usage
818         exit 2
819 fi
820
821 cd "$1"
822
823 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
824         $ECHOCOMMAND "$1 does not contain any images. Quitting."
825         exit 4
826 fi
827
828 if [ ! -w . ]; then
829         $ECHOCOMMAND "Can't write to images directory, exiting"
830         exit 8
831 fi
832
833 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
834         $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
835         exit 8
836 fi
837
838 if [ -e style.css ] && [ ! -w style.css ]; then
839         $ECHOCOMMAND "Can't write style.css, exiting"
840         exit 8
841 fi
842
843 generate_thumbs
844
845 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
846         if [ ! -d $PAGESDIRECTORY ]; then
847                 mkdir -p $PAGESDIRECTORY
848         fi
849
850         if [ ! -w $PAGESDIRECTORY ]; then
851                 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
852                 exit 16
853         fi
854         UNIXPAGESDIRECTORY=$PAGESDIRECTORY/     
855         PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
856
857         BASEURL="../"
858 else
859         BASEURL=""
860         UNIXPAGESDIRECTORY=""
861 fi
862
863 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
864         generate_medium
865         generate_medium_pages
866 fi
867
868 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
869         if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
870                 generate_pages __${MEDIUMDIRECTORY}
871         else
872                 generate_pages
873         fi
874 fi
875
876 if [ -r description.txt ] ; then
877         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
878 else
879         DESCRIPTION=""
880 fi
881
882 BASEURL=""
883
884 $ECHOCOMMAND "Starting to generate page"
885
886 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
887 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
888
889 $ECHOCOMMAND "Adding Captions"
890
891 extra=""
892 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
893         extra=__${MEDIUMDIRECTORY}
894 fi
895
896 $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}
897
898 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
899
900 $ECHOCOMMAND "Finished generating the page"
901 $ECHOCOMMAND "Generating stylesheet"
902 cd $ICONSDIRECTORY
903
904 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
905 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
906
907 cd ..
908
909 # add a bit to the maxheight for the size of the caption
910 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
911
912 $BPGALLERY_STYLESHEET_FUNCTION > style.css
913
914 $ECHOCOMMAND "All done"
915
916 if [[ $OUTPUTHTML != 0 ]]; then
917         $ECHOCOMMAND "</pre>"
918 fi