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