Merge branch 'debian' into wheezy
[debian/openrocket] / update-openrocket
1 #!/bin/sh
2
3 # Copyright (C) 2006-2012 Bart Martens <bartm@knars.be>
4 # Copyright (C) 2013 Bdale Garbee <bdale@gag.com>
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 version 2 as
8 # published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 set -e
19
20 return_0() {
21         return 0
22 }
23
24 trap "return_0" 0
25
26 die_hard() {
27         echo "ERROR: $1" >&2
28         exit 1
29 }
30
31 [ `whoami` = "root" ] || die_hard "must be root"
32
33 show_usage() {
34         echo "Usage:"
35         echo "  update-openrocket --install"
36         echo "  update-openrocket --uninstall"
37         echo "  update-openrocket --status"
38         echo "Additional options:"
39         echo "  --verbose"
40         echo "  --quiet"
41         exit 1
42 }
43
44 getopt_temp=`getopt -o iusfvq --long install,uninstall,status,fast,verbose,quiet -n 'update-openrocket' -- "$@"` || show_usage
45 eval set -- "$getopt_temp" || show_usage
46
47 ACTION=none
48 fast=no
49 verbose=no
50 quiet=no
51
52 while [ true ]
53 do
54         case "$1" in
55                 -i|--install)
56                         ACTION="--install"
57                         shift
58                         ;;
59                 -u|--uninstall)
60                         ACTION="--uninstall"
61                         shift
62                         ;;
63                 -s|--status)
64                         ACTION="--status"
65                         shift
66                         ;;
67                 -f|--fast)
68                         fast=yes
69                         shift
70                         ;;
71                 -v|--verbose)
72                         verbose=yes
73                         shift
74                         ;;
75                 -q|--quiet)
76                         quiet=yes
77                         shift
78                         ;;
79                 --)
80                         shift
81                         break
82                         ;;
83                 *)
84                         echo "Internal error!"
85                         exit 1
86                         ;;
87         esac
88 done
89
90 [ "$ACTION" != "none" -a $# -eq 0 ] || show_usage
91 [ "$quiet" != "yes" ] || verbose=no
92
93 [ "$verbose" != "yes" ] || echo "options : $getopt_temp"
94
95 UNPACKDIR=`mktemp -d /tmp/openrocket.XXXXXXXXXX` || die_hard "mktemp failed"
96 echo "$UNPACKDIR" | grep -q "^/tmp/openrocket\." || die_hard "paranoia"
97 cd "$UNPACKDIR" || die_hard "cd failed"
98
99 [ "$verbose" != "yes" ] || echo "temporary directory: $UNPACKDIR"
100
101 do_cleanup() {
102         [ "$verbose" != "yes" ] || echo "cleaning up temporary directory $UNPACKDIR ..."
103         cd /
104         echo "$UNPACKDIR" | grep -q "^/tmp/openrocket\." || die_hard "paranoia"
105         rm -rf "$UNPACKDIR"
106 }
107
108 die_hard_with_a_cleanup() {
109         return_0
110         do_cleanup
111         die_hard "$1"
112 }
113
114 trap "die_hard_with_a_cleanup interrupted" INT
115
116 cachedir=/var/cache/openrocket
117
118 wgetquiet=' -q '
119 wgetfast='-t 3 -T 15 '
120 wgetalways=' -nd -P . '
121 wgetprogress=' -v --progress=dot:default '
122
123 get_installed_version() {
124
125         installed=`unzip -p /usr/lib/openrocket/*.jar build.properties | grep build.version= | awk -F\= '{ print $2 }'`
126 }
127
128 get_download_url() {
129         url=`wget -qO - http://openrocket.sourceforge.net | grep https | grep download | awk -F\" '{ print $4 }'`
130 }
131
132 get_upstream_version() {
133         get_download_url
134         upstream=`echo $url | awk -F/ '{ print $8 }'`
135 }
136
137 case "$ACTION" in
138
139         --install)
140                 [ "$verbose" != "yes" ] || echo "selected action = $ACTION"
141
142                 get_installed_version
143                 [ "$verbose" != "yes" ] || echo "installed version = $installed"
144
145                 get_upstream_version
146                 [ "$verbose" != "yes" ] || echo "upstream version = $upstream"
147
148                 if [ "$installed" != "" -a "$upstream" != "" -a "$installed" = "$upstream" ]
149                 then
150
151                         [ "$verbose" != "yes" ] || echo "upstream version $upstream is already installed"
152
153                 else
154
155                         wgetoptions="$wgetquiet $wgetalways"
156                         [ "$verbose" != "yes" ] || wgetoptions="$wgetalways $wgetprogress"
157                         [ "$fast" != "yes" ] || wgetoptions="$wgetoptions $wgetfast"
158                         [ "$verbose" != "yes" ] || echo "wgetoptions=$wgetoptions"
159
160                         [ "$verbose" != "yes" ] || echo "downloading $url..."
161
162                         HOME=/root \
163                         wget $wgetoptions $url \
164                                 || die_hard_with_a_cleanup "wget failed to download $downloadurl"
165
166                         targetdir=/usr/lib/openrocket
167
168                         [ "$verbose" != "yes" ] || echo "removing old .jar files from $targetdir ..."
169                         rm -f $targetdir/*.jar
170
171                         [ "$verbose" != "yes" ] || echo "moving download to $targetdir ..."
172                         mv -f download $targetdir/OpenRocket-$upstream.jar
173
174                         [ "$verbose" != "yes" ] || ( get_installed_version && echo "create /usr/bin/openrocket wrapper" )
175
176                         echo "#!/bin/sh" > /usr/bin/openrocket
177                         echo "exec java -jar /usr/lib/openrocket/OpenRocket-$upstream.jar \"\$@\"" >> /usr/bin/openrocket
178                         chmod +x /usr/bin/openrocket
179
180                         [ "$verbose" != "yes" ] || ( get_installed_version && echo "OpenRocket version: $installed" )
181
182                 fi # end if installed != upstream
183
184                 [ "$verbose" != "yes" ] || echo "end of action $ACTION"
185
186                 ;;
187
188         --uninstall)
189                 [ "$verbose" != "yes" ] || echo "selected action = $ACTION"
190
191                 [ "$verbose" != "yes" ] || echo "removing files ..."
192                 rm -f /usr/lib/openrocket/*.jar
193
194                 [ "$verbose" != "yes" ] || echo "end of action $ACTION"
195
196                 ;;
197
198         --status)
199                 [ "$verbose" != "yes" ] || echo "selected action = $ACTION"
200
201                 get_installed_version
202                 echo "OpenRocket version installed on this system  : $installed"
203                 get_upstream_version
204                 echo "OpenRocket version available on upstream site: $upstream"
205
206                 [ "$verbose" != "yes" ] || echo "end of action $ACTION"
207
208                 ;;
209
210         *)
211
212                 do_cleanup
213                 show_usage
214
215                 ;;
216
217 esac
218
219 do_cleanup
220
221 [ "$verbose" != "yes" ] || echo "end of update-openrocket"
222