Updated addon repository URL and improved debug output on download
[supertux.git] / tools / png_recompress.sh
1 #!/bin/bash
2 # $Id$
3
4 #  Copyright (C) 2007-2008 Arvid Norlander <anmaster AT tele2 DOT se>
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 3 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, see <http://www.gnu.org/licenses/>.
18 #
19 # This script recompresses .png files using optipng and
20 # advpng to get the smallest images. All recompression is
21 # lossless.
22 #
23 # This script needs at least bash3, bash2 will not work
24 #
25 # TODO:
26 #  * Make it work recursively on a directory.
27
28 # Check for new enough bash version
29 fail_old_bash() {
30         echo "Sorry your bash version is too old!"
31         echo "You need at least version 3.0 of bash."
32         echo "Please install a newer version:"
33         echo " * Either use your distro's packages."
34         echo " * Or see http://www.gnu.org/software/bash/"
35         exit 2
36 }
37
38 # Check bash version. We need at least 3.1
39 # Lets not use anything like =~ here because
40 # that may not work on old bash versions.
41 if [[ "${BASH_VERSINFO[0]}" -lt 3 ]]; then
42         fail_old_bash
43 fi
44
45 if [[ -z "$1" ]] || [[ "$1" == "--help" ]]; then
46         echo "Usage: $(basename $0) files..."
47         exit 1
48 fi
49
50 if ! type optipng > /dev/null 2>&1; then
51         echo "Can't find optipng!"
52         echo "This script depends on the optipng tool to be in PATH."
53         echo "Please install it or, if it is already installed add the"
54         echo "directory it is in to PATH and try again."
55         echo "Homepage of this tool is: http://optipng.sourceforge.net/"
56         exit 1
57 fi
58
59 if ! type advpng > /dev/null 2>&1; then
60         echo "Can't find advpng!"
61         echo "This script depends on the advpng tool to be in PATH."
62         echo "Please install it or, if it is already installed add the"
63         echo "directory it is in to PATH and try again."
64         echo "Homepage of this tool is: http://advancemame.sourceforge.net/comp-readme.html"
65         echo "Hint: For package name in your distro, try looking for \"advancecomp\"."
66         exit 1
67 fi
68
69 TMPPATH="$$.png-recompress"
70
71 echo -e "Please wait, this can take a \e[1mlong\e[0m time."
72
73 dooptipng() {
74         optipng -i 0 -o 7 "$@" | \
75                 awk '
76                         /^\*\* Processing:/ { print "\nFile:   " $3 }
77                         /^Input file size/ { print "Input:  " $5,$6 }
78                         /^Output file size/ { print "Output: " $5,$6,$7,$8,$9,$10,$11 }
79                         /is already optimized/ { print "Output: No change" }
80                 '
81 }
82
83 doadvpng() {
84         echo "         In          Out   %  Filename"
85         advpng -z -4 "$@"
86 }
87
88 echo -e "\n\n\n\e[1mPass 1: optipng\e[0m\n\n\n"
89 dooptipng "$@"
90
91 echo -e "\n\n\n\e[1mPass 2: advpng\e[0m\n\n\n"
92 doadvpng "$@"
93
94 echo -e "\n\n\n\e[1mPass 3: optipng again (as advpng often makes optipng more effective)\e[0m\n\n\n"
95 dooptipng "$@"