Start theme support
[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"
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 [[ -z $TITLE ]]; then 
78         TITLE="Photo Gallery"
79 fi
80
81 if [[ -z $CONVERTTOOL ]]; then
82         CONVERTTOOL=convert
83 fi
84
85 if [[ -z $FINDCOMMAND ]]; then
86         FINDCOMMAND=find
87 fi
88
89 if [[ -z $XARGSCOMMAND ]]; then
90         XARGSCOMMAND=xargs
91 fi
92
93 if [[ -z $ECHOCOMMAND ]]; then
94         ECHOCOMMAND=echo
95 fi
96
97 if [[ -z $SORTCOMMAND ]]; then
98         SORTCOMMAND=sort
99 fi
100
101 if [[ -z $IDENTIFYCOMMAND ]]; then
102         IDENTIFYCOMMAND=identify
103 fi
104
105 if [[ -z $HEADCOMMAND ]]; then
106         HEADCOMMAND=head
107 fi
108
109 if [[ -z $WIDTH ]]; then
110         WIDTH=100
111 fi
112
113 if [[ -z $IMAGEEXTENSIONS ]]; then
114         IMAGEEXTENSIONS="jpeg jpg gif png";
115 fi
116
117 if [[ -z $WCCOMMAND ]]; then
118         WCCOMMAND="wc -l"
119 fi
120
121 FINDIMAGESOPTIONS=""
122
123 for imageext in $IMAGEEXTENSIONS; do
124         FINDIMAGESOPTIONS=$FINDIMAGESOPTIONS' -o -type f -maxdepth 1 -iname '*.$imageext' -print0'
125 done
126
127 FINDIMAGESOPTIONS=${FINDIMAGESOPTIONS## -o }
128
129 function usage() {
130 cat <<END
131 Usage:
132   $0 [--help|--version|<path to images>]
133
134     --help
135         displays this help screen
136     --version
137         displays the version and exits
138         
139 END
140 }
141
142 function version() {
143 cat <<END
144 Version: $VERSION
145 END
146 }
147
148 if [[ -z $1 ]]; then
149         echo "No path given"
150         usage
151         exit 1
152 fi
153
154 if [[ "$1" == "--help" || "$1" == "-h" ]]; then
155         usage
156         exit 0
157 fi
158
159 if [[ "$1" == "--version" || "$1" == "-v" ]]; then
160         version
161         exit 0
162 fi
163
164 if [[ ! -d $1 ]]; then
165         echo "$1 is not a directory"
166         usage
167         exit 2
168 fi
169
170 cd "$1"
171
172 if ( ! $FINDCOMMAND . $FINDIMAGESOPTIONS > /dev/null 2>/dev/null ); then
173         echo "$1 does not contain any images. Quitting."
174         exit 4
175 fi
176
177 if [ ! -d icons ]; then
178         mkdir icons
179 else
180         echo "$1 already contains an icons folder, stopping"
181         exit 3
182 fi
183
184 totalimages=$($FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace echo {} | $WCCOMMAND);
185 currentimage=0
186
187 $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
188
189 echo done: $totalimages/$totalimages images
190
191 bpgallery_default_head > index.html
192
193 $FINDCOMMAND . $FINDIMAGESOPTIONS | $XARGSCOMMAND -0 --replace $ECHOCOMMAND {} |$SORTCOMMAND -g | while read filename; do filename=${filename#./}; bpgallery_default_thumbsline $filename; done >> index.html
194
195 bpgallery_default_tail >> index.html
196
197 cd icons
198
199 for imageext in $IMAGEEXTENSIONS; do
200         if ( ls "*.$imageext" > /dev/null 2>/dev/null ); then
201                 TEMPMAX=$($IDENTIFYCOMMAND *.$imageext | grep "Geometry:" | sed -r 's#^.*Geometry:.*?[0-9]+x([0-9]+)\+.*$#\1#' | $SORTCOMMAND -g -r | $HEADCOMMAND -n 1)
202                 if [[ $TEMPMAX -gt $MAXHEIGHT ]]; then
203                         MAXHEIGHT=$TEMPMAX
204                 fi
205         fi
206 done
207
208 cd ..
209
210
211 bpgallery_default_stylesheet > style.css
212