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