]> git.sommitrealweird.co.uk Git - bpgallery.git/blob - bpgallery.sh
4cc74509a68b7d305ec3f50dcb03645e9f1268b7
[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         fi
403         
404         generate_resized_images $MEDIUMWIDTH medium
405 }
406
407 function generate_pages() {
408         $ECHOCOMMAND "Generating Pages"
409         previouspage=""
410         currentpage=""
411         nextpage=""
412         previousimage=""
413         currentimage=""
414         nextimage=""
415         extra=""
416         extradir=""
417         addlinks=0
418         
419         if [ ! -z $1 ]; then
420                 extra=$1
421         fi
422
423         if [ ! -z $2 ]; then
424                 extradir=$2/
425                 addlinks=1
426         fi
427
428         if [ -z $1 ] && [ -z $2 ] && [[ $GENERATEPAGESFORMEDIUMSIZE != 0 ]]; then
429                 addlinks=2
430         fi
431
432         $FINDCOMMAND . $FINDIMAGESOPTIONS | \
433         $XARGSCOMMAND -0 -I {} $ECHOCOMMAND {} | \
434         $SORTCOMMAND -g | \
435         while read imagefilename; do
436                 previousimage=$currentimage
437                 currentimage=$nextimage
438                 nextimage=${imagefilename#./}
439                 addlink=""
440
441                 if [[ $addlinks == 1 ]]; then
442                         if [[ $GENERATEPAGESFORFULLSIZE != 0 ]]; then
443                                 addlink=${currentimage}.html
444                         else
445                                 addlink=${currentimage}
446                         fi
447                 fi
448
449                 if [[ $addlinks == 2 ]]; then
450                         addlink=${currentimage}__medium.html
451                 fi
452                 
453                 previouspage=$currentpage
454                 currentpage=$nextpage
455                 nextpage=${imagefilename}${extra}.html
456                 filename=${extradir}${currentimage}
457                 generate_general_page "$previouspage" "$currentpage" "$nextpage" $addlink
458                 echo $nextimage
459         done | tail -n 2 | (
460                 read previouspage
461                 read currentpage
462                 addlink=""
463
464                 if [[ $addlinks == 1 ]]; then
465                         if [[ $GENERATEPAGESFORFULLSIZE ]]; then
466                                 addlink=${currentpage}.html
467                         else
468                                 addlink=${currentpage}
469                         fi
470                 fi
471
472                 if [[ $addlinks == 2 ]]; then
473                         addlink=${nextimage}__medium.html
474                 fi
475                 
476                 filename=${extradir}${currentpage}
477                 currentpage=${currentpage}${extra}.html
478                 previouspage=${previouspage}${extra}.html
479                 nextpage=""
480                 generate_general_page "$previouspage" "$currentpage" "" $addlink
481         )
482 }
483
484 function generate_medium_pages() {
485         generate_pages __medium medium
486 }
487
488 function generate_general_page() {
489
490         if [[ -z $1 ]]; then
491                 previouspage=""
492         fi
493
494         if [[ -z $2 ]]; then
495                 currentpage=""
496                 return
497         fi
498
499         if [[ -z $3 ]]; then
500                 nextpage=""
501         fi
502
503         if [[ ! -z $4 ]]; then
504                 linklocation=$4
505         fi
506
507         if [ -r captions.txt ]; then
508                 caption=$($GREPCOMMAND -E "^${filename}   " captions.txt); caption=${caption#*    }
509         else
510                 caption=""
511         fi
512
513         $BPGALLERY_PAGE_FUNCTION > "$currentpage"
514 }
515
516 if [[ $OUTPUTHTML != 0 ]]; then
517         $ECHOCOMMAND "<pre>"
518 fi
519
520 if [[ -z $1 ]]; then
521         $ECHOCOMMAND "No path given"
522         usage
523         exit 1
524 fi
525
526 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
527         usage
528         exit 0
529 fi
530
531 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
532         version
533         exit 0
534 fi
535
536 if [[ ! -d $1 ]]; then
537         $ECHOCOMMAND "$1 is not a directory"
538         usage
539         exit 2
540 fi
541
542 cd "$1"
543
544 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
545         $ECHOCOMMAND "$1 does not contain any images. Quitting."
546         exit 4
547 fi
548
549 if [ ! -w . ]; then
550         $ECHOCOMMAND "Can't write to images directory, exiting"
551         exit 8
552 fi
553
554 if [ -e ${INDEXDOCUMENT} ] && [ ! -w ${INDEXDOCUMENT} ]; then
555         $ECHOCOMMAND "Can't write ${INDEXDOCUMENT}, exiting"
556         exit 8
557 fi
558
559 if [ -e style.css ] && [ ! -w style.css ]; then
560         $ECHOCOMMAND "Can't write style.css, exiting"
561         exit 8
562 fi
563
564 generate_thumbs
565
566 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
567         generate_medium
568         generate_medium_pages
569 fi
570
571 if [ $GENERATEPAGESFORFULLSIZE != 0 ]; then
572         generate_pages
573 fi
574
575 if [ -r description.txt ] ; then
576         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
577 else
578         DESCRIPTION=""
579 fi
580
581
582 $ECHOCOMMAND "Starting to generate page"
583
584 $BPGALLERY_HEAD_FUNCTION > ${INDEXDOCUMENT}
585 $BPGALLERY_DESCRIPTION_FUNCTION >> ${INDEXDOCUMENT}
586
587 $ECHOCOMMAND "Adding Captions"
588
589 extra=""
590 if [ $GENERATEPAGESFORMEDIUMSIZE != 0 ]; then
591         extra=__medium
592 fi
593
594 $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}
595
596 $BPGALLERY_TAIL_FUNCTION >> ${INDEXDOCUMENT}
597
598 $ECHOCOMMAND "Finished generating the page"
599 $ECHOCOMMAND "Generating stylesheet"
600 cd icons
601
602 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
603 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
604
605 cd ..
606
607 # add a bit to the maxheight for the size of the caption
608 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
609
610 $BPGALLERY_STYLESHEET_FUNCTION > style.css
611
612 $ECHOCOMMAND "All done"
613
614 if [[ $OUTPUTHTML != 0 ]]; then
615         $ECHOCOMMAND "</pre>"
616 fi