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