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