add proper support for (un)installation in Harmattan (patched) Open Mode
[busybox-power] / debian / busybox-power.prerm
1 #!/bin/sh
2 # A script to restore /bin/busybox and delete the symlinks made during 
3 # installation.
4 #
5 # Symbolic links to applets are only removed if they are
6 # 1) created by the installer script ("install-binary.sh")
7 # 2) not replaced by a binary (i.e. they are still a symbolic link)
8 # 3) pointing to a busybox binary
9 #
10 # By Dennis Groenen <tj.groenen@gmail.com>
11 # GPLv3 licensed
12 #
13
14 INSTALLDIR="/opt/busybox-power"
15 EXECPWR="$INSTALLDIR/busybox.power"
16 DISTBIN="/bin/busybox.distrib"
17
18 VERBOSE="0"
19 MODIFIEDBIN="0"
20
21 INSTBINARY_SHA1=`sha1sum $EXECPWR | awk '{ print $1 }'`
22 if test -e $DISTBIN; then
23   ORIGBINARY_SHA1=`sha1sum $DISTBIN | awk '{ print $1 }'`; fi
24
25 # Load shared functions
26 source $INSTALLDIR/functions
27
28 # Check whether we can load the list of created symlinks during installation
29 CHECK_SYMLINKSFILE() {
30     if test ! -e $INSTALLDIR/busybox-power.symlinks; then
31       echo -e "Warning: cannot find the list of symlinks to be removed. No" \
32         "symlinks will be removed at all!\n" >> /tmp/busybox-power-error
33     fi
34 }
35
36 # Check the (integrity) of our BusyBox backup
37 CHECK_BACKUP() {
38     # Firstly, check whether the backup still exists
39     if test ! -e $DISTBIN; then
40       if test "$ENVIRONMENT" == "SDK"; then return; fi # SDK comes without BB
41       echo -e "Warning: the backup of the original BusyBox binary is missing!" \
42         "/bin/busybox will not be touched.\n" >> /tmp/busybox-power-error
43       return
44     fi
45
46     # Secondly, check the integrity of the backup
47     if test -e $INSTALLDIR/busybox.distrib.sha1; then
48       if test "`cat $INSTALLDIR/busybox.distrib.sha1`" != "$ORIGBINARY_SHA1"; then
49         if test -e $INSTALLDIR/busybox.distrib.version; then
50           if test "`cat $INSTALLDIR/busybox.distrib.version`" == "`GETBBVERSION`"; then
51             # The backup has been changed whilst busybox hasn't been upgraded
52             echo -e "Warning: the backup of the original BusyBox binary has" \
53               "been modified since installing busybox-power (invalid SHA1" \
54               "checksum). Do not continue unless you're sure that $DISTBIN" \
55               "is not corrupted.\n" >> /tmp/busybox-power-error
56           fi
57         else
58           echo -e "Warning: the backup of the original BusyBox binary has" \
59           "been modified since installing busybox-power (invalid SHA1" \
60           "checksum). We could not determine whether this is due to a BusyBox" \
61           "upgrade (e.g. by an (C)SSU update) or not. Do not continue unless" \
62           "either the latter is the case, or you're via other ways sure that" \
63           "$DISTBIN is not corrupted.\n" >> /tmp/busybox-power-error
64         fi
65       fi
66     else
67       echo -e "Warning: could not load the saved SHA1 checksum of the backup" \
68         "of the original BusyBox binary; the integrity of the backup of the" \
69         "original binary can not be guaranteed.\n" >> /tmp/busybox-power-error
70     fi
71 }
72
73 # Check whether /bin/busybox has been modified after bb-power's installation
74 # This always happens on Harmattan when it receives a BusyBox upgrade;
75 # dpkg-divert is disallowed on that platform
76 CHECK_INSTALLEDBIN() {
77     if test "$INSTBINARY_SHA1" != "`sha1sum /bin/busybox | awk '{ print $1 }'`"; then
78       MODIFIEDBIN="1"
79
80       if test "$ENVIRONMENT" == "HARMATTAN"; then
81         if test -e $INSTALLDIR/busybox.distrib.version; then
82           if test "`cat $INSTALLDIR/busybox.distrib.version`" != "`GETBBVERSION`"; then
83             # Harmattan received a BusyBox upgrade, do not disturb the user by
84             # warning that /bin/busybox has been modified
85             return
86           fi
87         fi
88         echo -e "Warning: /bin/busybox has been modified since installing" \
89           "busybox-power (invalid SHA1 checksum). This can be the result of a" \
90           "BusyBox upgrade (e.g. by a Meego update). Your current" \
91           "/bin/busybox won't be touched. Our backup of the original BusyBox" \
92           "binary will be left at $DISTBIN.\n" >> /tmp/busybox-power-error
93       else
94         echo -e "Warning: /bin/busybox has been modified since installing" \
95           "busybox-power (invalid SHA1 checksum). Your current /bin/busybox" \
96           "won't be touched and the diversion of /bin/busybox to $DISTBIN" \
97           "will not be removed.\n" >> /tmp/busybox-power-error
98       fi
99     fi
100 }
101
102 # Display encountered errors
103 DISPLAY_ERRORS() {
104     case $ENVIRONMENT in
105       SDK)
106         echo -e "\n\n-----------Attention!-----------"
107         cat /tmp/busybox-power-error
108         rm /tmp/busybox-power-error
109         echo "-> Please press [enter] to ignore the above errors/warnings."
110         echo "   Hit [ctrl-c] to break"
111         read 
112         ;;
113       FREMANTLE)
114         echo "Click \"I Agree\" to ignore the above errors/warnings. Ask for" \
115           "help if you don't know what to do." >> /tmp/busybox-power-error
116         echo "Please confirm the text on the screen of your device"
117         maemo-confirm-text "Attention!" /tmp/busybox-power-error
118         res=$?
119         rm /tmp/busybox-power-error
120         if test ! $res == 0; then exit 1; fi
121         ;;
122       HARMATTAN)
123         echo "Click \"I Agree\" to ignore the above errors/warnings. Ask for" \
124           "help if you don't know what to do." >> /tmp/busybox-power-error
125         echo "Please confirm the text on the screen of your device"
126         meego-confirm-text "Attention!" /tmp/busybox-power-error > /dev/null 2>&1
127         res=$?
128         rm /tmp/busybox-power-error
129         if test ! $res == 0; then exit 1; fi
130         ;;
131       esac
132 }
133
134 # Rollback procedure for Harmattan based devices
135 ROLLBACK_HARMATTAN() {
136     echo -e "\nWarning: an error has occured! Rolling back..."
137     $EXECPWR cp -af $TMPBINBAK /bin/busybox
138     $EXECPWR cp -a $TMPHASHBAK /var/lib/aegis/refhashlist
139     echo "End of roll-back"
140     exit 1
141 }
142
143 # Uninstallation of the enhanced binary
144 UNINSTALL() {
145     if test $MODIFIEDBIN == 1; then
146       # /bin/busybox has been modified since installing busybox-power
147       # Leave both the file and the diversion in place
148       return
149     fi
150
151     case $ENVIRONMENT in
152       SDK|FREMANTLE)
153         if test -e $DISTBIN; then
154           cp -af $DISTBIN /bin/busybox
155           if test -e /bin/busybox; then
156             rm $DISTBIN; fi
157         elif test "$ENVIRONMENT" == "SDK"; then
158           # There was no /bin/busybox to begin with..
159           rm /bin/busybox
160         fi
161
162         /usr/sbin/dpkg-divert --remove /bin/busybox
163         ;;
164       HARMATTAN)
165         if ! test -e $DISTBIN; then return; fi
166
167         TMPHASHBAK=`$EXECPWR mktemp`
168         TMPBINBAK=`$EXECPWR mktemp`
169         ORIGINCHECK=`GETORIGINCHECK_STATUS`
170
171         # Useful information for Harmattan-based devices
172         ECHO_VERBOSE "refhashlist backup: $TMPHASHBAK"
173         ECHO_VERBOSE "busybox backup: $TMPBINBAK"
174         ECHO_VERBOSE "instbinary: $INSTBINARY_SHA1"
175         ECHO_VERBOSE "origbinary: $ORIGBINARY_SHA1"
176         ECHO_VERBOSE "device mode: $DEVICEMODE"
177         ECHO_VERBOSE "origincheck: $ORIGINCHECK"
178
179         if test $ORIGINCHECK -eq 1; then
180           SETORIGINCHECK_STATUS 0; fi
181
182         $EXECPWR cp -a /bin/busybox $TMPBINBAK || exit 1
183         $EXECPWR cp -a /var/lib/aegis/refhashlist $TMPHASHBAK || exit 1
184         $EXECPWR cp -af $DISTBIN /bin/busybox || ROLLBACK_HARMATTAN
185         $EXECPWR sed -i "s/$INSTBINARY_SHA1/$ORIGBINARY_SHA1/" /var/lib/aegis/refhashlist || ROLLBACK_HARMATTAN
186         /usr/bin/accli -c tcb-sign -F /var/lib/aegis/refhashlist < /var/lib/aegis/refhashlist || ROLLBACK_HARMATTAN
187         /usr/sbin/validator-init
188
189         if test $ORIGINCHECK -eq 1; then
190           SETORIGINCHECK_STATUS 1; fi
191
192         rm $TMPBINBAK
193         rm $TMPHASHBAK
194         rm $DISTBIN
195         ;;
196     esac
197 }
198
199 # Remove all symlinks that the installation script has made
200 UNSYMLINK() {
201     # Load list of installed symlinks
202     touch $INSTALLDIR/busybox-power.symlinks
203     source $INSTALLDIR/busybox-power.symlinks
204
205     # Walk through all possible destinations
206     for DESTDIR in $DESTINATIONS; do 
207       # Enable us to see all entries in $DESTINATIONS as variables
208       eval "APPLICATIONS=\$$DESTDIR"
209       # Set destination directory accordingly
210       case $DESTDIR in
211         DEST_BIN)
212           DIR="/bin"
213           ;;
214         DEST_SBIN)
215           DIR="/sbin"
216           ;;
217         DEST_USRBIN)
218           DIR="/usr/bin"
219           ;;
220         DEST_USRSBIN)
221           DIR="/usr/sbin"
222           ;;
223       esac
224
225       ECHO_VERBOSE "\nRemoving symlinks in $DIR"
226       # Walk through all applications from the current destination
227       for APP in $APPLICATIONS; do
228         # The following code is executed for every application in the current
229         # destination
230         if test -h $DIR/$APP; then 
231           # Good, this app is still a symbolic link ..
232           if test -n "`ls -l $DIR/$APP | grep busybox`"; then
233             ECHO_VERBOSE "Removing link: $DIR/$APP"
234             rm $DIR/$APP
235           fi
236         fi
237       done
238     done
239 }
240
241 # Action to be performed after restoring original busybox
242 CLEANUP() {
243     OLDFILES="busybox-power.symlinks
244       busybox.distrib.version
245       busybox.distrib.sha1"
246
247     for file in $OLDFILES; do
248       if test -e $INSTALLDIR/$file; then
249         rm $INSTALLDIR/$file
250       fi
251     done
252
253     # Clean up trap (from busybox-power <= 1.20.2power1)
254     # Should be removed by the old prerm, but may be left behind when that one
255     # fails and dpkg uses this one instead. Therefore, keep this cleanup
256     # included for a few busybox-power releases.
257     line="trap exit SIGHUP #by busybox-power"
258     if grep -F -q "$line" /etc/profile; then
259       sed "/$line/d" /etc/profile > /tmp/profile.sed
260       mv /tmp/profile.sed /etc/profile
261     fi
262 }
263
264 ### Codepath ###
265 ECHO_VERBOSE "busybox-power: verbose mode"
266 ECHO_VERBOSE "  binary: $EXECPWR"
267 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
268 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
269
270 CHECK_SYMLINKSFILE
271 if test "$ENVIRONMENT" != "SDK"; then
272   CHECK_ROOT
273 fi
274 CHECK_BACKUP
275 CHECK_INSTALLEDBIN
276 if test -e /tmp/busybox-power-error; then
277   # An error has occured during the checks
278   DISPLAY_ERRORS
279 fi
280 UNSYMLINK
281 UNINSTALL
282 CLEANUP
283