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