]> git.sommitrealweird.co.uk Git - bpgallery.git/blob - bpgallery.sh
c26f826e26ecd545eed7135cf902caf954d3511a
[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"
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 declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
218         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
219 else
220         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
221 fi
222
223 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
224         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
225 else
226         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
227 fi
228
229 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
230         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
231 else
232         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
233 fi
234
235 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
236         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
237 else
238         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
239 fi
240
241 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
242         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
243 else
244         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
245 fi
246
247 FINDIMAGESOPTIONS=""
248
249 for imageext in $IMAGEEXTENSIONS; do
250         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
251 done
252
253 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
254 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
255
256 function usage() {
257 cat <<END
258 Usage:
259   $0 [--help|--version|<path to images>]
260
261     --help
262         displays this help screen
263     --version
264         displays the version and exits
265         
266 END
267 }
268
269 function version() {
270 cat <<END
271 Version: $VERSION
272 END
273 }
274
275 function generate_thumbs() {
276         $ECHOCOMMAND "Generating Thumbnails"
277         currentimage=0
278         totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} | $WCCOMMAND);
279         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)
280         
281         if [ $totalimages -eq $imagestoupdate ]; then
282                 $ECHOCOMMAND "Regenerating all thumbnails"
283         elif [ $imagestoupdate -eq 0 ]; then
284                 $ECHOCOMMAND "No Updated Thumbs, not regenerating"
285                 return
286         else
287                 $ECHOCOMMAND "Generating $imagestoupdate of $totalimages thumbnails"
288         fi
289         
290         $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
291         $ECHOCOMMAND done: $imagestoupdate/$imagestoupdate images
292         $ECHOCOMMAND "Completed generating thumbs for $totalimages images"
293 }
294
295 if [[ -z $1 ]]; then
296         $ECHOCOMMAND "No path given"
297         usage
298         exit 1
299 fi
300
301 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
302         usage
303         exit 0
304 fi
305
306 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
307         version
308         exit 0
309 fi
310
311 if [[ ! -d $1 ]]; then
312         $ECHOCOMMAND "$1 is not a directory"
313         usage
314         exit 2
315 fi
316
317 cd "$1"
318
319 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
320         $ECHOCOMMAND "$1 does not contain any images. Quitting."
321         exit 4
322 fi
323
324 if [ ! -d icons ]; then
325         mkdir icons
326 fi
327
328 generate_thumbs
329
330 if [ -r description.txt ] ; then
331         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
332 else
333         DESCRIPTION=""
334 fi
335
336
337 $BPGALLERY_HEAD_FUNCTION > index.html
338 $BPGALLERY_DESCRIPTION_FUNCTION >> index.html
339
340 $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
341
342 $BPGALLERY_TAIL_FUNCTION >> index.html
343
344 cd icons
345
346 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
347 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
348
349 cd ..
350
351 # add a bit to the maxheight for the size of the caption
352 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
353
354 $BPGALLERY_STYLESHEET_FUNCTION > style.css
355