]> git.sommitrealweird.co.uk Git - bpgallery.git/blob - bpgallery.sh
ba71cc7edab3cefd248a2fedf758434f41b0ae22
[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.0+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 cat << END
50 <div class="thumbnail"><a href="$filename"><img src="icons/$filename" /></a><div class="caption">$caption</div></div>
51 END
52 }
53
54 declare -rf bpgallery_default_thumbsline
55
56 function bpgallery_default_tail() {
57 cat << END
58 </body>
59 </html>
60 END
61 }
62
63 declare -rf bpgallery_default_tail
64
65 function bpgallery_default_stylesheet() {
66 cat <<END
67 body {
68         background: white;
69         color: black;
70         font-family: sans-serif;
71         font-size: 10pt;
72 }
73
74 div.thumbnail {
75         float: left;
76         padding: 20px;
77         border: 0px;
78         width: ${WIDTH}px;
79         height: ${MAXHEIGHT}px;
80 }
81
82 div.caption {
83         width: 100%;
84         text-align: center;
85         font-weight: bold;
86 }
87
88 a {
89         border: 0px;
90 }
91
92 img {
93         border: 0px;
94 }
95 END
96 }
97
98 declare -rf bpgallery_default_stylesheet
99
100 function bpgallery_escape_url() {
101         temp=$1
102         temp=${temp//\%/%25}
103         temp=${temp//:/%3A}
104         temp=${temp//;/%3B}
105         temp=${temp// /%20}
106         temp=${temp//$/%24}
107         temp=${temp//&/%26}
108         temp=${temp//+/%2B}
109         temp=${temp//,/%2C}
110         temp=${temp//\//%2F}
111         temp=${temp//=/%3D}
112         temp=${temp//\?/%3F}
113         temp=${temp//@/%40}
114         temp=${temp//\"/%22}
115         echo $temp
116 }
117
118 declare -rf bpgallery_escape_url
119
120 if [[ ! -z ${BPGALLERY_THEME} ]] ; then
121         declare -r BPGALLERY_THEME
122 fi
123
124 if [[ ! -z ${TITLE} ]] ; then
125         declare -r TITLE
126 fi
127
128 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
129         . /usr/local/etc/bpgallery/config 2>/dev/null
130 fi
131
132 if [[ -e /etc/bpgallery/config ]] ; then
133         . /etc/bpgallery/config 2>/dev/null
134 fi
135
136 if [[ -e $HOME/.bpgallery.rc ]]; then
137         . $HOME/.bpgallery.rc 2>/dev/null
138 fi
139
140 if [[ -z ${BPGALLERY_THEME} ]]; then
141         BPGALLERY_THEME=default
142 fi
143
144 if [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
145         . $HOME/.bpgallery.themes/${BPGALLERY_THEME} 2>/dev/null
146 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
147         . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
148 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
149         . /etc/bpgallery/themes/${BPGALLERY_THEME} 2>/dev/null
150 fi
151
152 if [[ -z $TITLE ]]; then 
153         TITLE="Photo Gallery"
154 fi
155
156 if [[ -z $CONVERTTOOL ]]; then
157         CONVERTTOOL=convert
158 fi
159
160 if [[ -z $FINDCOMMAND ]]; then
161         FINDCOMMAND=find
162 fi
163
164 if [[ -z $XARGSCOMMAND ]]; then
165         XARGSCOMMAND=xargs
166 fi
167
168 if [[ -z $ECHOCOMMAND ]]; then
169         ECHOCOMMAND=echo
170 fi
171
172 if [[ -z $SORTCOMMAND ]]; then
173         SORTCOMMAND=sort
174 fi
175
176 if [[ -z $IDENTIFYCOMMAND ]]; then
177         IDENTIFYCOMMAND=identify
178 fi
179
180 if [[ -z $HEADCOMMAND ]]; then
181         HEADCOMMAND=head
182 fi
183
184 if [[ -z $GREPCOMMAND ]]; then
185         GREPCOMMAND=grep
186 fi
187
188 if [[ -z $SEDCOMMAND ]]; then
189         SEDCOMMAND=sed
190 fi
191
192 if [[ -z $WIDTH ]]; then
193         WIDTH=100
194 fi
195
196 if [[ -z $IMAGEEXTENSIONS ]]; then
197         IMAGEEXTENSIONS="jpeg jpg gif png";
198 fi
199
200 if [[ -z $WCCOMMAND ]]; then
201         WCCOMMAND="wc -l"
202 fi
203
204 if [[ -z $CAPTIONHEIGHT ]]; then
205         CAPTIONHEIGHT=75
206 fi
207
208 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
209         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
210 else
211         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
212 fi
213
214 if declare -F "bpgallery_${BPGALLERY_THEME}_description" > /dev/null ; then
215         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_${BPGALLERY_THEME}_description"
216 else
217         BPGALLERY_DESCRIPTION_FUNCTION="bpgallery_default_description"
218 fi
219
220 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
221         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
222 else
223         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
224 fi
225
226 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
227         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
228 else
229         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
230 fi
231
232 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
233         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
234 else
235         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
236 fi
237
238 FINDIMAGESOPTIONS=""
239
240 for imageext in $IMAGEEXTENSIONS; do
241         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -iname '*.$imageext' -print0'
242 done
243
244 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
245 FINDIMAGESOPTIONS='-maxdepth 1 '${FINDIMAGESOPTIONS}
246
247 function usage() {
248 cat <<END
249 Usage:
250   $0 [--help|--version|<path to images>]
251
252     --help
253         displays this help screen
254     --version
255         displays the version and exits
256         
257 END
258 }
259
260 function version() {
261 cat <<END
262 Version: $VERSION
263 END
264 }
265
266 if [[ -z $1 ]]; then
267         echo "No path given"
268         usage
269         exit 1
270 fi
271
272 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
273         usage
274         exit 0
275 fi
276
277 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
278         version
279         exit 0
280 fi
281
282 if [[ ! -d $1 ]]; then
283         echo "$1 is not a directory"
284         usage
285         exit 2
286 fi
287
288 cd "$1"
289
290 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
291         echo "$1 does not contain any images. Quitting."
292         exit 4
293 fi
294
295 if [ ! -d icons ]; then
296         mkdir icons
297
298         totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace echo {} | $WCCOMMAND);
299         currentimage=0
300         
301         $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
302         
303         echo done: $totalimages/$totalimages images
304 else
305         echo "$1 already contains an icons folder, updating."
306
307         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)
308
309         echo "images to update: $imagestoupdate"
310         
311         
312         currentimage=0
313
314         $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
315         echo done: $imagestoupdate/$imagestoupdate images
316 fi
317
318 if [ -r description.txt ] ; then
319         DESCRIPTION=$($SEDCOMMAND -e '1 { s/^/<p>/; }; /^$/ { s,$,</p><p>,; }; $ { s,$,</p>, };' description.txt)
320 else
321         DESCRIPTION=""
322 fi
323
324
325 $BPGALLERY_HEAD_FUNCTION > index.html
326 $BPGALLERY_DESCRIPTION_FUNCTION >> index.html
327
328 $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
329
330 $BPGALLERY_TAIL_FUNCTION >> index.html
331
332 cd icons
333
334 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
335 MAXWIDTH=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%w\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
336
337 cd ..
338
339 # add a bit to the maxheight for the size of the caption
340 MAXHEIGHT=$((MAXHEIGHT+$CAPTIONHEIGHT))
341
342 $BPGALLERY_STYLESHEET_FUNCTION > style.css
343