]> git.sommitrealweird.co.uk Git - bpgallery.git/blob - bpgallery.sh
Add in a setable path for themes
[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.0.2+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="$filename"><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 a {
94         border: 0px;
95 }
96
97 img {
98         border: 0px;
99 }
100 END
101 }
102
103 declare -rf bpgallery_default_stylesheet
104
105 function bpgallery_escape_url() {
106         temp=$1
107         temp=${temp//\%/%25}
108         temp=${temp//:/%3A}
109         temp=${temp//;/%3B}
110         temp=${temp// /%20}
111         temp=${temp//$/%24}
112         temp=${temp//&/%26}
113         temp=${temp//+/%2B}
114         temp=${temp//,/%2C}
115         temp=${temp//\//%2F}
116         temp=${temp//=/%3D}
117         temp=${temp//\?/%3F}
118         temp=${temp//@/%40}
119         temp=${temp//\"/%22}
120         $ECHOCOMMAND $temp
121 }
122
123 declare -rf bpgallery_escape_url
124
125 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
126         declare -r BPGALLERY_THEME
127 fi
128
129 if [[ ! -z ${TITLE} ]] ; then
130         declare -r TITLE
131 fi
132
133 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
134         . /usr/local/etc/bpgallery/config 2>/dev/null
135 fi
136
137 if [[ -e /etc/bpgallery/config ]] ; then
138         . /etc/bpgallery/config 2>/dev/null
139 fi
140
141 if [[ -e $HOME/.bpgallery.rc ]]; then
142         . $HOME/.bpgallery.rc 2>/dev/null
143 fi
144
145 if [[ -e $1 && -d $1 && -e $1/.bpgallery.rc ]]; then
146         . $1/.bpgallery.rc 2>/dev/null
147 fi
148
149 if [[ -z ${BPGALLERY_THEME} ]]; then
150         BPGALLERY_THEME=default
151 fi
152
153 if [[ ! -z ${BPGALLERY_THEME_DIR} ]] && \
154         [[ -e ${BPGALLERY_THEME_DIR}/${BPGALLERY_THEME} ]]; then
155         . ${BPGALLERY_THEME_DIR/${BPGALLERY_THEME} 2>/dev/null
156 elif [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
157         . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
158 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
159         . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
160 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
161         . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
162 fi
163
164 if [[ -z $TITLE ]]; then 
165         TITLE="Photo Gallery"
166 fi
167
168 if [[ -z $CONVERTTOOL ]]; then
169         CONVERTTOOL=convert
170 fi
171
172 if [[ -z $FINDCOMMAND ]]; then
173         FINDCOMMAND=find
174 fi
175
176 if [[ -z $XARGSCOMMAND ]]; then
177         XARGSCOMMAND=xargs
178 fi
179
180 if [[ -z $ECHOCOMMAND ]]; then
181         ECHOCOMMAND=echo
182 fi
183
184 if [[ -z $SORTCOMMAND ]]; then
185         SORTCOMMAND=sort
186 fi
187
188 if [[ -z $IDENTIFYCOMMAND ]]; then
189         IDENTIFYCOMMAND=identify
190 fi
191
192 if [[ -z $HEADCOMMAND ]]; then
193         HEADCOMMAND=head
194 fi
195
196 if [[ -z $GREPCOMMAND ]]; then
197         GREPCOMMAND=grep
198 fi
199
200 if [[ -z $SEDCOMMAND ]]; then
201         SEDCOMMAND=sed
202 fi
203
204 if [[ -z $WIDTH ]]; then
205         WIDTH=100
206 fi
207
208 if [[ -z $IMAGEEXTENSIONS ]]; then
209         IMAGEEXTENSIONS="jpeg jpg gif png";
210 fi
211
212 if [[ -z $WCCOMMAND ]]; then
213         WCCOMMAND="wc -l"
214 fi
215
216 if [[ -z $CAPTIONHEIGHT ]]; then
217         CAPTIONHEIGHT=75
218 fi
219
220 if [[ -z $OUTPUTHTML ]]; then
221         OUTPUTHTML=0
222 fi
223
224 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
225         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
226 else
227         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
228 fi
229
230 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
231         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
232 else
233         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
234 fi
235
236 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
237         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
238 else
239         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
240 fi
241
242 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
243         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
244 else
245         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
246 fi
247
248 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
249         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
250 else
251         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
252 fi
253
254 FINDIMAGESOPTIONS=""
255
256 for imageext in $IMAGEEXTENSIONS; do
257         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
258 done
259
260 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
261 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
262
263 function usage() {
264 cat <<END
265 Usage:
266   $0 [--help|--version|<path to images>]
267
268     --help
269         displays this help screen
270     --version
271         displays the version and exits
272         
273 END
274 }
275
276 function version() {
277 cat <<END
278 Version: $VERSION
279 END
280 }
281
282 function generate_thumbs() {
283         $ECHOCOMMAND "Generating Thumbnails"
284         currentimage=0
285         totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | $WCCOMMAND);
286         imagestoupdate=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | while read filename; do filename=${filename//\"/\\\"}; if [ ! -r "icons/$filename" ] || [ "$filename" -nt "icons/$filename" ] || [ $($IDENTIFYCOMMAND -format "%w" "icons/$filename") -ne $WIDTH ]; then $ECHOCOMMAND $filename; fi; done | $WCCOMMAND)
287         
288         if [ $totalimages -eq $imagestoupdate ]; then
289                 $ECHOCOMMAND "Regenerating all thumbnails"
290         elif [ $imagestoupdate -eq 0 ]; then
291                 $ECHOCOMMAND "No Updated Thumbs, not regenerating"
292                 return
293         else
294                 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages thumbnails"
295         fi
296         
297         $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | while read filename; do tempfilename=${filename//\"/\\\"/}; if [ ! -r "icons/$tempfilename" ] || [ "$tempfilename" -nt "icons/$tempfilename" ] || [ $($IDENTIFYCOMMAND -format "%w" "icons/$filename") -ne $WIDTH ]; then $ECHOCOMMAND -n $filename; $ECHOCOMMAND -n -e "\000"; fi; done | $XARGSCOMMAND -0 --verbose --max-procs=4 --replace $CONVERTTOOL -resize $WIDTH '{}' 'icons/{}' 2>&1 | while read throwout; do $ECHOCOMMAND done: $currentimage/$imagestoupdate images; currentimage=$((currentimage+1)); done
298         $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
299         $ECHOCOMMAND "Completed generating thumbs for $totalimages images"
300 }
301
302 if [[ $OUTPUTHTML != 0 ]]; then
303         $ECHOCOMMAND "<pre>"
304 fi
305
306 if [[ -z $1 ]]; then
307         $ECHOCOMMAND "No path given"
308         usage
309         exit 1
310 fi
311
312 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
313         usage
314         exit 0
315 fi
316
317 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
318         version
319         exit 0
320 fi
321
322 if [[ ! -d $1 ]]; then
323         $ECHOCOMMAND "$1 is not a directory"
324         usage
325         exit 2
326 fi
327
328 cd "$1"
329
330 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
331         $ECHOCOMMAND "$1 does not contain any images. Quitting."
332         exit 4
333 fi
334
335 if [ ! -d icons ]; then
336         mkdir icons
337 fi
338
339 generate_thumbs
340
341 if [ -r description.txt ] ; then
342         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
343 else
344         DESCRIPTION=""
345 fi
346
347
348 $ECHOCOMMAND "Starting to generate page"
349
350 $BPGALLERY_HEAD_FUNCTION > index.html
351 $BPGALLERY_DESCRIPTION_FUNCTION >> index.html
352
353 $ECHOCOMMAND "Adding Captions"
354
355 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $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; filename=$(bpgallery_escape_url "$filename"); $BPGALLERY_THUMBSLINE_FUNCTION; done >> index.html
356
357 $BPGALLERY_TAIL_FUNCTION >> index.html
358
359 $ECHOCOMMAND "Finished generating the page"
360 $ECHOCOMMAND "Generating stylesheet"
361 cd icons
362
363 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
364 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
365
366 cd ..
367
368 # add a bit to the maxheight for the size of the caption
369 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
370
371 $BPGALLERY_STYLESHEET_FUNCTION > style.css
372
373 $ECHOCOMMAND "All done"
374
375 if [[ $OUTPUTHTML != 0 ]]; then
376         $ECHOCOMMAND "</pre>"
377 fi