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