a088ebfed5a51c4fb0a97c44a1b00fed70858d33
[bpgallery.git] / bpgallery.sh
1 #!/usr/bin/env bash
2
3 # makegallery.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"
23
24 if [[ -z $TITLE ]]; then 
25         TITLE="Photo Gallery"
26 fi
27
28 if [[ -z $CONVERTTOOL ]]; then
29         CONVERTTOOL=convert
30 fi
31
32 if [[ -z $FINDCOMMAND ]]; then
33         FINDCOMMAND=find
34 fi
35
36 if [[ -z $XARGSCOMMAND ]]; then
37         XARGSCOMMAND=xargs
38 fi
39
40 if [[ -z $ECHOCOMMAND ]]; then
41         ECHOCOMMAND=echo
42 fi
43
44 if [[ -z $SORTCOMMAND ]]; then
45         SORTCOMMAND=sort
46 fi
47
48 if [[ -z $IDENTIFYCOMMAND ]]; then
49         IDENTIFYCOMMAND=identify
50 fi
51
52 if [[ -z $HEADCOMMAND ]]; then
53         HEADCOMMAND=head
54 fi
55
56 if [[ -z $WIDTH ]]; then
57         WIDTH=100
58 fi
59
60 if [[ -z $IMAGEEXTENSIONS ]]; then
61         IMAGEEXTENSIONS="jpeg jpg gif png";
62 fi
63
64 if [[ -z $WCCOMMAND ]]; then
65         WCCOMMAND="wc -l"
66 fi
67
68 FINDIMAGESOPTIONS=""
69
70 for imageext in $IMAGEEXTENSIONS; do
71         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -maxdepth 1 -iname '*.$imageext' -print0'
72 done
73
74 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
75
76 usage() {
77         cat <<END
78 Usage:
79   $0 [--help|--version|<path to images>]
80
81     --help
82         displays this help screen
83     --version
84         displays the version and exits
85         
86 END
87 }
88
89 version() {
90         cat <<END
91 Version: $VERSION
92 END
93 }
94
95 if [[ -z $1 ]]; then
96         echo "No path given"
97         usage
98         exit 1
99 fi
100
101 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
102         usage
103         exit 0
104 fi
105
106 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
107         version
108         exit 0
109 fi
110
111 if [[ ! -d $1 ]]; then
112         echo "$1 is not a directory"
113         usage
114         exit 2
115 fi
116
117 cd "$1"
118
119 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
120         echo "$1 does not contain any images. Quitting."
121         exit 4
122 fi
123
124 if [ ! -d icons ]; then
125         mkdir icons
126 else
127         echo "$1 already contains an icons folder, stopping"
128         exit 3
129 fi
130
131 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace echo {} | $WCCOMMAND);
132 currentimage=0
133
134 $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
135
136 echo done: $totalimages/$totalimages images
137
138 cat <<END > index.html
139 <?xml version="1.0"?>
140 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
141 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
142 <head>
143         <title>${TITLE}</title>
144         <link rel="stylesheet" href="style.css" type="text/css" />
145 </head>
146 <body>
147 END
148
149
150 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} |$SORTCOMMAND -g | sed -e 's#^./\(.*\)$#<div class="thumbnail"><a href="\1"><img src="icons/\1" /></a></div>#' >> index.html
151
152 cat <<END >> index.html
153 </body>
154 </html>
155 END
156
157
158 cd icons
159
160 for imageext in $IMAGEEXTENSIONS; do
161         if ( ls "*.$imageext" > /dev/null 2>/dev/null ); then
162                 TEMPMAX=$($IDENTIFYCOMMAND *.$imageext | grep "Geometry:" | sed -r 's#^.*Geometry:.*?[0-9]+x([0-9]+)\+.*$#\1#' | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
163                 if [[ $TEMPMAX -gt $MAXHEIGHT ]]; then
164                         MAXHEIGHT=$TEMPMAX
165                 fi
166         fi
167 done
168
169 cd ..
170
171
172 cat <<END > style.css
173 body {
174         background: white;
175         color: black;
176         font-family: sans-serif;
177         font-size: 12pt;
178 }
179
180 div.thumbnail {
181         float: left;
182         padding: 20px;
183         border: 0px;
184         width: ${WIDTH}px;
185         height: ${MAXHEIGHT}px;
186 }
187
188 a {
189         border: 0px;
190 }
191
192 img {
193         border: 0px;
194 }
195 END
196