]> git.sommitrealweird.co.uk Git - bpgallery.git/blob - bpgallery.sh
* Check image exists in size matching code
[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.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         if [ ! -e "$dir/$filename" ]; then
547                 echo 0
548                 return
549         fi
550         iconwidth=$($IDENTIFYCOMMAND -format "%wx%h" "$dir/$filename")
551         iconheight=${iconwidth//*x}
552         iconwidth=${iconwidth//x*}
553         # are the dimensions correctish?
554         imagesizeright=1
555         if [ ! -z $wantedheight ]; then
556                 if [ $wantedwidth -ne $iconwidth ] && [ $wantedheight -ne $iconheight ]; then
557                         imagesizeright=0
558                 else
559                         # make sure that both of the dimensions are smaller than they
560                         # should be
561                         if [ $iconwidth -gt $wantedwidth ] || [ $iconheight -gt $wantedheight ]; then
562                                 imagesizeright=0
563                         fi
564                 fi
565         else
566                 if [ $wantedwidth -ne $iconwidth ]; then
567                         imagesizeright=0
568                 fi
569         fi
570         echo $imagesizeright
571 }
572
573 function generate_resized_images() {
574         $ECHOCOMMAND "Generating $2"
575         width=$1
576         height=${width//*x}
577         if [ $width != $height ]; then
578                 width=${width//x*}
579         else
580                 height=""
581         fi
582         currentimage=0
583         totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
584         imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS |
585                 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} |
586                 while read filename; do
587                         filename=${filename//\"/\\\"}
588                         imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
589                         if [ ! -r "$2/$filename" ] ||
590                                 [ "$filename" -nt "$2/$filename" ] ||
591                                 [ $imagesizeright -ne 1 ]; then
592                                 $ECHOCOMMAND $filename
593                         fi
594                 done |
595                 $WCCOMMAND)
596         
597         if [ $totalimages -eq 0 ]; then
598                 # might as well exit at this point - there are no pictures
599                 $ECHOCOMMAND "No images for the gallery - exiting."
600                 exit 64
601         else
602                 # check if the directory exists and create it other wise
603                 if [ ! -d $2 ]; then
604                         mkdir $2
605                 fi
606
607                 if [ ! -w $2 ]; then
608                         $ECHOCOMMAND "Can't write to $2 directory, exiting"
609                         exit 16
610                 fi
611         fi
612
613         if [ $totalimages -eq $imagestoupdate ]; then
614                 $ECHOCOMMAND "Regenerating all $2"
615         elif [ $imagestoupdate -eq 0 ]; then
616                 $ECHOCOMMAND "No Updated $2, not regenerating"
617                 return
618         else
619                 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
620         fi
621         
622         $FINDCOMMAND . $FINDIMAGESOPTIONS | \
623                 $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
624                 while read filename; do 
625                         tempfilename=${filename//\"/\\\"/}; 
626                         imagesizeright=$(check_dimensions "$width" "$height" "$2" "$filename")
627                         if [ ! -r "$2/$tempfilename" ] \
628                                 || [ "$tempfilename" -nt "$2/$tempfilename" ] \
629                                 || [ $imagesizeright -ne 1 ]; then 
630                                         $ECHOCOMMAND -n $filename
631                                         $ECHOCOMMAND -n -e "\000"
632                         fi
633                 done | \
634                 $XARGSCOMMAND -0 --verbose --max-procs=4 -I {} $CONVERTTOOL -resize $1 '{}' $2/'{}' 2>&1 | \
635                         while read throwout; do
636                                 $ECHOCOMMAND done: $currentimage/$imagestoupdate images
637                                 currentimage=$((currentimage+1))
638                         done
639         $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
640         $ECHOCOMMAND "Completed generating $2 for $totalimages images"
641 }
642
643 function generate_thumbs() {
644         if [ ! -z $HEIGHT ]; then
645                 generate_resized_images "${WIDTH}x${HEIGHT}" $ICONSDIRECTORY
646         else
647         generate_resized_images $WIDTH $ICONSDIRECTORY
648         fi
649 }
650
651 function generate_medium() {
652         if [ ! -z $MEDIUMHEIGHT ]; then
653                 generate_resized_images "${MEDIUMWIDTH}x${MEDIUMHEIGHT}" $MEDIUMDIRECTORY
654         else
655                 generate_resized_images $MEDIUMWIDTH $MEDIUMDIRECTORY
656         fi
657 }
658
659 function generate_pages() {
660         $ECHOCOMMAND "Generating Pages"
661         previouspage=""
662         currentpage=""
663         nextpage=""
664         previousimage=""
665         currentimage=""
666         nextimage=""
667         extra=""
668         extradir=""
669         addlinks=0
670         
671         if [ ! -z $1 ]; then
672                 extra=$1
673         fi
674
675         if [ ! -z $2 ]; then
676                 extradir=$2/
677                 addlinks=1
678         fi
679
680         if [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
681                 addlinks=2
682         fi
683         
684         $FINDCOMMAND . $FINDIMAGESOPTIONS | \
685         $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
686         $SORTCOMMAND -g | \
687         while read imagefilename; do
688                 previousimage=$currentimage
689                 currentimage=$nextimage
690                 nextimage=${imagefilename#./}
691                 addlink=""
692                 
693                 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
694                         if [[ $addlinks == 1 ]]; then
695                                 addlink=${currentimage}.html
696                         else
697                                 addlink=${currentimage}${extra}.html
698                         fi
699                 else
700                         addlink=${currentimage}
701                 fi
702
703                 previouspage=$currentpage
704                 currentpage=$nextpage
705                 if [[ $addlinks == 1 ]]; then
706                         nextpage=${nextimage}${extra}.html
707                 else
708                         nextpage=${nextimage}.html
709                 fi
710
711                 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
712                         filename=../${extradir}${currentimage}
713                 else
714                         filename=${extradir}${currentimage}
715                 fi
716                 if [ "x$currentpage" != "x" ]; then
717                         generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
718                 fi
719                 $ECHOCOMMAND $nextimage
720         done | tail -n 2 | (
721                 read previouspage
722                 read currentpage
723                 addlink=""
724
725                 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
726                         if [[ $addlinks == 1 ]]; then
727                                 addlink=${currentpage}.html
728                         else
729                                 addlink=${currentpage}${extra}.html
730                         fi
731                 else
732                         addlink=${currentpage}
733                 fi
734                 
735                 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
736                         filename=../${extradir}${currentpage}
737                 else
738                         filename=${extradir}${currentpage}
739                 fi
740                 if [[ $addlinks == 1 ]]; then
741                         currentpage=${currentpage}${extra}.html
742                         previouspage=${previouspage}${extra}.html
743                 else
744                         currentpage=${currentpage}.html
745                         previouspage=${previouspage}.html
746                 fi
747                 nextpage=""
748                 generate_general_page "$previouspage" "$currentpage" "" $addlink
749         )
750 }
751
752 function generate_medium_pages() {
753         generate_pages __${MEDIUMDIRECTORY} ${MEDIUMDIRECTORY}
754 }
755
756 function generate_general_page() {
757
758         if [[ -z $1 ]]; then
759                 previouspage=""
760         fi
761
762         if [[ -z $2 ]]; then
763                 currentpage=""
764                 return
765         fi
766
767         if [[ -z $3 ]]; then
768                 nextpage=""
769         fi
770
771         if [[ ! -z $4 ]]; then
772                 linklocation=$4
773         fi
774
775         if [ -r captions.txt ]; then
776                 imagefilename=${filename##*/}
777                 caption=$($GREPCOMMAND -E "^${imagefilename}    " captions.txt); caption=${caption#*    }
778         else
779                 caption=""
780         fi
781         
782         $BPGALLERY_PAGE_FUNCTION > "${UNIXPAGESDIRECTORY}$currentpage"
783 }
784
785 if [[ $OUTPUTHTML != 0 ]]; then
786         $ECHOCOMMAND "<pre>"
787 fi
788
789 if [[ -z $1 ]]; then
790         $ECHOCOMMAND "No path given"
791         usage
792         exit 1
793 fi
794
795 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
796         usage
797         exit 0
798 fi
799
800 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
801         version
802         exit 0
803 fi
804
805 if [[ ! -d $1 ]]; then
806         $ECHOCOMMAND "$1 is not a directory"
807         usage
808         exit 2
809 fi
810
811 cd "$1"
812
813 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
814         $ECHOCOMMAND "$1 does not contain any images. Quitting."
815         exit 4
816 fi
817
818 if [ ! -w . ]; then
819         $ECHOCOMMAND "Can't write to images directory, exiting"
820         exit 8
821 fi
822
823 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
824         $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
825         exit 8
826 fi
827
828 if [ -e style.css ] && [ ! -w style.css ]; then
829         $ECHOCOMMAND "Can't write style.css, exiting"
830         exit 8
831 fi
832
833 generate_thumbs
834
835 if [[ "x$PAGESDIRECTORY" != "x" ]]; then
836         if [ ! -d $PAGESDIRECTORY ]; then
837                 mkdir -p $PAGESDIRECTORY
838         fi
839
840         if [ ! -w $PAGESDIRECTORY ]; then
841                 $ECHOCOMMAND "Can't write to $PAGESDIRECTORY directory, exiting"
842                 exit 16
843         fi
844         UNIXPAGESDIRECTORY=$PAGESDIRECTORY/     
845         PAGESDIRECTORY=$(bpgallery_escape_url $PAGESDIRECTORY)/
846
847         BASEURL="../"
848 else
849         BASEURL=""
850         UNIXPAGESDIRECTORY=""
851 fi
852
853 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
854         generate_medium
855         generate_medium_pages
856 fi
857
858 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
859         if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
860                 generate_pages __${MEDIUMDIRECTORY}
861         else
862                 generate_pages
863         fi
864 fi
865
866 if [ -r description.txt ] ; then
867         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
868 else
869         DESCRIPTION=""
870 fi
871
872 BASEURL=""
873
874 $ECHOCOMMAND "Starting to generate page"
875
876 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
877 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
878
879 $ECHOCOMMAND "Adding Captions"
880
881 extra=""
882 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
883         extra=__${MEDIUMDIRECTORY}
884 fi
885
886 $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}
887
888 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
889
890 $ECHOCOMMAND "Finished generating the page"
891 $ECHOCOMMAND "Generating stylesheet"
892 cd $ICONSDIRECTORY
893
894 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
895 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
896
897 cd ..
898
899 # add a bit to the maxheight for the size of the caption
900 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
901
902 $BPGALLERY_STYLESHEET_FUNCTION > style.css
903
904 $ECHOCOMMAND "All done"
905
906 if [[ $OUTPUTHTML != 0 ]]; then
907         $ECHOCOMMAND "</pre>"
908 fi