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