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