07b0d228b7b0fb4aa8c4246ba65d28a41950b5cd
[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.2+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>
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: 12pt;
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 a {
68         border: 0px;
69 }
70
71 img {
72         border: 0px;
73 }
74 END
75 }
76
77 if [[ -e /usr/local/etc/bpgallery/config ]] ; then
78         . /usr/local/etc/bpgallery/config
79 fi
80
81 if [[ -e /etc/bpgallery/config ]] ; then
82         . /etc/bpgallery/config
83 fi
84
85 if [[ -e $HOME/.bpgallery.rc ]]; then
86         . $HOME/.bpgallery.rc
87 fi
88
89 if [[ -z ${BPGALLERY_THEME} ]]; then
90         BPGALLERY_THEME=default
91 fi
92
93 if [[ -e $HOME/.bpgallery.themes/${BPGALLERY_THEME} ]]; then
94         . $HOME/.bpgallery.themes/${BPGALLERY_THEME}
95 elif [[ -e /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
96         . /usr/local/etc/bpgallery/themes/${BPGALLERY_THEME}
97 elif [[ -e /etc/bpgallery/themes/${BPGALLERY_THEME} ]]; then
98         . /etc/bpgallery/themes/${BPGALLERY_THEME}
99 fi
100
101 if [[ -z $TITLE ]]; then 
102         TITLE="Photo Gallery"
103 fi
104
105 if [[ -z $CONVERTTOOL ]]; then
106         CONVERTTOOL=convert
107 fi
108
109 if [[ -z $FINDCOMMAND ]]; then
110         FINDCOMMAND=find
111 fi
112
113 if [[ -z $XARGSCOMMAND ]]; then
114         XARGSCOMMAND=xargs
115 fi
116
117 if [[ -z $ECHOCOMMAND ]]; then
118         ECHOCOMMAND=echo
119 fi
120
121 if [[ -z $SORTCOMMAND ]]; then
122         SORTCOMMAND=sort
123 fi
124
125 if [[ -z $IDENTIFYCOMMAND ]]; then
126         IDENTIFYCOMMAND=identify
127 fi
128
129 if [[ -z $HEADCOMMAND ]]; then
130         HEADCOMMAND=head
131 fi
132
133 if [[ -z $WIDTH ]]; then
134         WIDTH=100
135 fi
136
137 if [[ -z $IMAGEEXTENSIONS ]]; then
138         IMAGEEXTENSIONS="jpeg jpg gif png";
139 fi
140
141 if [[ -z $WCCOMMAND ]]; then
142         WCCOMMAND="wc -l"
143 fi
144
145 if declare -F "bpgallery_${BPGALLERY_THEME}_head" > /dev/null ; then
146         BPGALLERY_HEAD_FUNCTION="bpgallery_${BPGALLERY_THEME}_head"
147 else
148         BPGALLERY_HEAD_FUNCTION="bpgallery_default_head"
149 fi
150
151 if declare -F "bpgallery_${BPGALLERY_THEME}_thumbsline" > /dev/null ; then
152         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_${BPGALLERY_THEME}_thumbsline"
153 else
154         BPGALLERY_THUMBSLINE_FUNCTION="bpgallery_default_thumbsline"
155 fi
156
157 if declare -F "bpgallery_${BPGALLERY_THEME}_tail" > /dev/null ; then
158         BPGALLERY_TAIL_FUNCTION="bpgallery_${BPGALLERY_THEME}_tail"
159 else
160         BPGALLERY_TAIL_FUNCTION="bpgallery_default_tail"
161 fi
162
163 if declare -F "bpgallery_${BPGALLERY_THEME}_stylesheet" > /dev/null ; then
164         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_${BPGALLERY_THEME}_stylesheet"
165 else
166         BPGALLERY_STYLESHEET_FUNCTION="bpgallery_default_stylesheet"
167 fi
168
169 FINDIMAGESOPTIONS=""
170
171 for imageext in $IMAGEEXTENSIONS; do
172         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -maxdepth 1 -iname '*.$imageext' -print0'
173 done
174
175 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
176
177 function usage() {
178 cat <<END
179 Usage:
180   $0 [--help|--version|<path to images>]
181
182     --help
183         displays this help screen
184     --version
185         displays the version and exits
186         
187 END
188 }
189
190 function version() {
191 cat <<END
192 Version: $VERSION
193 END
194 }
195
196 if [[ -z $1 ]]; then
197         echo "No path given"
198         usage
199         exit 1
200 fi
201
202 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
203         usage
204         exit 0
205 fi
206
207 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
208         version
209         exit 0
210 fi
211
212 if [[ ! -d $1 ]]; then
213         echo "$1 is not a directory"
214         usage
215         exit 2
216 fi
217
218 cd "$1"
219
220 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
221         echo "$1 does not contain any images. Quitting."
222         exit 4
223 fi
224
225 if [ ! -d icons ]; then
226         mkdir icons
227 else
228         echo "$1 already contains an icons folder, stopping"
229         exit 3
230 fi
231
232 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace echo {} | $WCCOMMAND);
233 currentimage=0
234
235 $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
236
237 echo done: $totalimages/$totalimages images
238
239 $BPGALLERY_HEAD_FUNCTION > index.html
240
241 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} |$SORTCOMMAND -g | while read filename; do filename=${filename#./}; $BPGALLERY_THUMBSLINE_FUNCTION; done >> index.html
242
243 $BPGALLERY_TAIL_FUNCTION >> index.html
244
245 cd icons
246
247 for imageext in $IMAGEEXTENSIONS; do
248         if ( ls "*.$imageext" > /dev/null 2>/dev/null ); then
249                 TEMPMAX=$($IDENTIFYCOMMAND *.$imageext | grep "Geometry:" | sed -r 's#^.*Geometry:.*?[0-9]+x([0-9]+)\+.*$#\1#' | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
250                 if [[ $TEMPMAX -gt $MAXHEIGHT ]]; then
251                         MAXHEIGHT=$TEMPMAX
252                 fi
253         fi
254 done
255
256 cd ..
257
258
259 $BPGALLERY_STYLESHEET_FUNCTION > style.css
260