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