Fix the image height code
[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"
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 declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
156         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
157 else
158         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
159 fi
160
161 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
162         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
163 else
164         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
165 fi
166
167 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
168         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
169 else
170         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
171 fi
172
173 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
174         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
175 else
176         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
177 fi
178
179 FINDIMAGESOPTIONS=""
180
181 for imageext in $IMAGEEXTENSIONS; do
182         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -maxdepth 1 -iname '*.$imageext' -print0'
183 done
184
185 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
186
187 function usage() {
188 cat <<END
189 Usage:
190   $0 [--help|--version|<path to images>]
191
192     --help
193         displays this help screen
194     --version
195         displays the version and exits
196         
197 END
198 }
199
200 function version() {
201 cat <<END
202 Version: $VERSION
203 END
204 }
205
206 if [[ -z $1 ]]; then
207         echo "No path given"
208         usage
209         exit 1
210 fi
211
212 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
213         usage
214         exit 0
215 fi
216
217 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
218         version
219         exit 0
220 fi
221
222 if [[ ! -d $1 ]]; then
223         echo "$1 is not a directory"
224         usage
225         exit 2
226 fi
227
228 cd "$1"
229
230 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
231         echo "$1 does not contain any images. Quitting."
232         exit 4
233 fi
234
235 if [ ! -d icons ]; then
236         mkdir icons
237 else
238         echo "$1 already contains an icons folder, stopping"
239         exit 3
240 fi
241
242 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace echo {} | $WCCOMMAND);
243 currentimage=0
244
245 $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
246
247 echo done: $totalimages/$totalimages images
248
249 $BPGALLERY_HEAD_FUNCTION > index.html
250
251 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} |$SORTCOMMAND -g | while read filename; do filename=${filename#./}; caption=$($GREPCOMMAND -E "^$filename        " captions.txt); caption=${caption#*    }; $BPGALLERY_THUMBSLINE_FUNCTION; done >> index.html
252
253 $BPGALLERY_TAIL_FUNCTION >> index.html
254
255 cd icons
256
257 MAXHEIGHT=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 $IDENTIFYCOMMAND -format "%h\n" | $GREPCOMMAND -v "^$" | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
258
259 cd ..
260
261 # add a bit to the maxheight for the size of the caption
262 MAXHEIGHT=$((MAXHEIGHT+75))
263
264 $BPGALLERY_STYLESHEET_FUNCTION > style.css
265