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