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