Exit on failure of being able to write medium sized images
[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+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="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="icons/$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='${INDEXDOCUMENT}'>Thumbnails</a></li>
137 END
138         if [[ ! -z $previouspage ]]; then
139                 echo "<li><a href='${previouspage}'>Previous</a></li>"
140         else
141                 echo "<li>Previous</li>"
142         fi
143
144         if [[ ! -z $nextpage ]]; then
145                 echo "<li><a href='${nextpage}'>Next</a></li>"
146         else
147                 echo "<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 declare -rf bpgallery_escape_url
180
181 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
182         declare -r BPGALLERY_THEME
183 fi
184
185 if [[ ! -z ${TITLE} ]] ; then
186         declare -r TITLE
187 fi
188
189 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
190         . /usr/local/etc/bpgallery/config 2>/dev/null
191 fi
192
193 if [[ -e /etc/bpgallery/config ]] ; then
194         . /etc/bpgallery/config 2>/dev/null
195 fi
196
197 if [[ -e $HOME/.bpgallery.rc ]]; then
198         . $HOME/.bpgallery.rc 2>/dev/null
199 fi
200
201 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
202         . $1/.bpgallery.rc 2>/dev/null
203 fi
204
205 if [[ -z ${BPGALLERY_THEME} ]]; then
206         BPGALLERY_THEME=default
207 fi
208
209 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
210         [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
211         . ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} 2>/dev/null
212 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
213         . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
214 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
215         . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
216 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
217         . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
218 fi
219
220 if [[ -z $TITLE ]]; then 
221         TITLE="Photo Gallery"
222 fi
223
224 if [[ -z $CONVERTTOOL ]]; then
225         CONVERTTOOL=convert
226 fi
227
228 if [[ -z $FINDCOMMAND ]]; then
229         FINDCOMMAND=find
230 fi
231
232 if [[ -z $XARGSCOMMAND ]]; then
233         XARGSCOMMAND=xargs
234 fi
235
236 if [[ -z $ECHOCOMMAND ]]; then
237         ECHOCOMMAND=echo
238 fi
239
240 if [[ -z $SORTCOMMAND ]]; then
241         SORTCOMMAND=sort
242 fi
243
244 if [[ -z $IDENTIFYCOMMAND ]]; then
245         IDENTIFYCOMMAND=identify
246 fi
247
248 if [[ -z $HEADCOMMAND ]]; then
249         HEADCOMMAND=head
250 fi
251
252 if [[ -z $GREPCOMMAND ]]; then
253         GREPCOMMAND=grep
254 fi
255
256 if [[ -z $SEDCOMMAND ]]; then
257         SEDCOMMAND=sed
258 fi
259
260 if [[ -z $WIDTH ]]; then
261         WIDTH=100
262 fi
263
264 if [[ -z $IMAGEEXTENSIONS ]]; then
265         IMAGEEXTENSIONS="jpeg jpg gif png";
266 fi
267
268 if [[ -z $WCCOMMAND ]]; then
269         WCCOMMAND="wc -l"
270 fi
271
272 if [[ -z $CAPTIONHEIGHT ]]; then
273         CAPTIONHEIGHT=75
274 fi
275
276 if [[ -z $OUTPUTHTML ]]; then
277         OUTPUTHTML=0
278 fi
279
280 if [[ -z $GENERATEPAGESFORFULLSIZE ]]; then
281         GENERATEPAGESFORFULLSIZE=0
282 fi
283
284 if [[ -z $GENERATEPAGESFORMEDIUMSIZE ]]; then
285         GENERATEPAGESFORMEDIUMSIZE=1
286 fi
287
288 if [[ -z $MEDIUMWIDTH ]]; then
289         MEDIUMWIDTH=400
290 fi
291
292 if [[ -z $INDEXDOCUMENT ]]; then
293         INDEXDOCUMENT=index.html
294 fi
295
296 if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
297         if declare -F "bpgallery_${BPGALLERY_THEME}_page" > /dev/null ; then
298                 BPGALLERY_PAGE_FUNCTION="bpgallery_${BPGALLERY_THEME}_page"
299         else
300                 BPGALLERY_PAGE_FUNCTION="bpgallery_default_page"
301         fi
302 fi
303
304 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
305         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
306 else
307         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
308 fi
309
310 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
311         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
312 else
313         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
314 fi
315
316 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
317         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
318 else
319         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
320 fi
321
322 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
323         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
324 else
325         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
326 fi
327
328 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
329         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
330 else
331         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
332 fi
333
334 FINDIMAGESOPTIONS=""
335
336 for imageext in $IMAGEEXTENSIONS; do
337         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
338 done
339
340 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
341 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
342
343 function usage() {
344 cat <<END
345 Usage:
346   $0 [--help|--version|<path to images>]
347
348     --help
349         displays this help screen
350     --version
351         displays the version and exits
352         
353 END
354 }
355
356 function version() {
357 cat <<END
358 Version: $VERSION
359 END
360 }
361
362 function generate_resized_images() {
363         $ECHOCOMMAND "Generating $2"
364         currentimage=0
365         totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | $WCCOMMAND);
366         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)
367         
368         if [ $totalimages -eq $imagestoupdate ]; then
369                 $ECHOCOMMAND "Regenerating all $2"
370         elif [ $imagestoupdate -eq 0 ]; then
371                 $ECHOCOMMAND "No Updated $2, not regenerating"
372                 return
373         else
374                 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages $2"
375         fi
376         
377         $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
378         $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
379         $ECHOCOMMAND "Completed generating $2 for $totalimages images"
380 }
381
382 function generate_thumbs() {
383         if [ ! -d icons ]; then
384                 mkdir icons
385         fi
386
387         if [ ! -w icons ]; then
388                 $ECHOCOMMAND "Can't write to icons directory, exiting"
389                 exit 16
390         fi
391
392         generate_resized_images $WIDTH icons
393 }
394
395 function generate_medium() {
396         if [ ! -d medium ]; then
397                 mkdir medium
398         fi
399
400         if [ ! -w medium ]; then
401                 $ECHOCOMMAND "Can't write to medium directory, exiting"
402                 exit 32
403         fi
404         
405         generate_resized_images $MEDIUMWIDTH medium
406 }
407
408 function generate_pages() {
409         $ECHOCOMMAND "Generating Pages"
410         previouspage=""
411         currentpage=""
412         nextpage=""
413         previousimage=""
414         currentimage=""
415         nextimage=""
416         extra=""
417         extradir=""
418         addlinks=0
419         
420         if [ ! -z $1 ]; then
421                 extra=$1
422         fi
423
424         if [ ! -z $2 ]; then
425                 extradir=$2/
426                 addlinks=1
427         fi
428
429         if [ -z $1 ] && [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
430                 addlinks=2
431         fi
432
433         $FINDCOMMAND . $FINDIMAGESOPTIONS | \
434         $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
435         $SORTCOMMAND -g | \
436         while read imagefilename; do
437                 previousimage=$currentimage
438                 currentimage=$nextimage
439                 nextimage=${imagefilename#./}
440                 addlink=""
441
442                 if [[ $addlinks == 1 ]]; then
443                         if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
444                                 addlink=${currentimage}.html
445                         else
446                                 addlink=${currentimage}
447                         fi
448                 fi
449
450                 if [[ $addlinks == 2 ]]; then
451                         addlink=${currentimage}__medium.html
452                 fi
453                 
454                 previouspage=$currentpage
455                 currentpage=$nextpage
456                 nextpage=${imagefilename}${extra}.html
457                 filename=${extradir}${currentimage}
458                 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
459                 echo $nextimage
460         done | tail -n 2 | (
461                 read previouspage
462                 read currentpage
463                 addlink=""
464
465                 if [[ $addlinks == 1 ]]; then
466                         if [[ $GENERATEPAGESFORFULLSIZE ]]; then
467                                 addlink=${currentpage}.html
468                         else
469                                 addlink=${currentpage}
470                         fi
471                 fi
472
473                 if [[ $addlinks == 2 ]]; then
474                         addlink=${nextimage}__medium.html
475                 fi
476                 
477                 filename=${extradir}${currentpage}
478                 currentpage=${currentpage}${extra}.html
479                 previouspage=${previouspage}${extra}.html
480                 nextpage=""
481                 generate_general_page "$previouspage" "$currentpage" "" $addlink
482         )
483 }
484
485 function generate_medium_pages() {
486         generate_pages __medium medium
487 }
488
489 function generate_general_page() {
490
491         if [[ -z $1 ]]; then
492                 previouspage=""
493         fi
494
495         if [[ -z $2 ]]; then
496                 currentpage=""
497                 return
498         fi
499
500         if [[ -z $3 ]]; then
501                 nextpage=""
502         fi
503
504         if [[ ! -z $4 ]]; then
505                 linklocation=$4
506         fi
507
508         if [ -r captions.txt ]; then
509                 caption=$($GREPCOMMAND -E "^${filename}   " captions.txt); caption=${caption#*    }
510         else
511                 caption=""
512         fi
513
514         $BPGALLERY_PAGE_FUNCTION > "$currentpage"
515 }
516
517 if [[ $OUTPUTHTML != 0 ]]; then
518         $ECHOCOMMAND "<pre>"
519 fi
520
521 if [[ -z $1 ]]; then
522         $ECHOCOMMAND "No path given"
523         usage
524         exit 1
525 fi
526
527 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
528         usage
529         exit 0
530 fi
531
532 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
533         version
534         exit 0
535 fi
536
537 if [[ ! -d $1 ]]; then
538         $ECHOCOMMAND "$1 is not a directory"
539         usage
540         exit 2
541 fi
542
543 cd "$1"
544
545 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
546         $ECHOCOMMAND "$1 does not contain any images. Quitting."
547         exit 4
548 fi
549
550 if [ ! -w . ]; then
551         $ECHOCOMMAND "Can't write to images directory, exiting"
552         exit 8
553 fi
554
555 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
556         $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
557         exit 8
558 fi
559
560 if [ -e style.css ] && [ ! -w style.css ]; then
561         $ECHOCOMMAND "Can't write style.css, exiting"
562         exit 8
563 fi
564
565 generate_thumbs
566
567 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
568         generate_medium
569         generate_medium_pages
570 fi
571
572 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
573         generate_pages
574 fi
575
576 if [ -r description.txt ] ; then
577         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
578 else
579         DESCRIPTION=""
580 fi
581
582
583 $ECHOCOMMAND "Starting to generate page"
584
585 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
586 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
587
588 $ECHOCOMMAND "Adding Captions"
589
590 extra=""
591 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
592         extra=__medium
593 fi
594
595 $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 [ $GENERATEPAGESFORFULLSIZE != 0 ]; then link=$(bpgallery_escape_url "${filename}${extra}.html"); else link=$(bpgallery_escape_url "$filename"); fi; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> ${INDEXDOCUMENT}
596
597 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
598
599 $ECHOCOMMAND "Finished generating the page"
600 $ECHOCOMMAND "Generating stylesheet"
601 cd icons
602
603 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
604 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
605
606 cd ..
607
608 # add a bit to the maxheight for the size of the caption
609 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
610
611 $BPGALLERY_STYLESHEET_FUNCTION > style.css
612
613 $ECHOCOMMAND "All done"
614
615 if [[ $OUTPUTHTML != 0 ]]; then
616         $ECHOCOMMAND "</pre>"
617 fi