divert busybox binary to /opt/busybox-power/
[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="$INSTALLDIR/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. Do not continue" \
54               "unless you're sure that $DISTBIN is not corrupted.\n" \
55               >> /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. We could not" \
60           "determine whether this is due to a BusyBox upgrade (e.g. by an" \
61           "(C)SSU update) or not. Do not continue unless either the latter is" \
62           "the case, or you're via other ways sure that $DISTBIN is not" \
63           "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 does not match busybox-power's binary" \
89           "This can be the result of a BusyBox upgrade (e.g. by a MeeGo" \
90           "update), an unsuccessful busybox-power installation, or a manual" \
91           "replacement of it. Your current /bin/busybox won't be touched. Our" \
92           "backup of the original BusyBox binary will be left at $DISTBIN.\n" \
93           >> /tmp/busybox-power-error
94       else
95         echo -e "Warning: /bin/busybox does not match busybox-power's binary." \
96           "Your current /bin/busybox won't be touched and the diversion of" \
97           "/bin/busybox to $DISTBIN will not be removed.\n" \
98           >> /tmp/busybox-power-error
99       fi
100     fi
101 }
102
103 # Display encountered errors
104 DISPLAY_ERRORS() {
105     case $ENVIRONMENT in
106       SDK)
107         echo -e "\n\n-----------Attention!-----------"
108         cat /tmp/busybox-power-error
109         rm /tmp/busybox-power-error
110         echo "-> Please press [enter] to ignore the above errors/warnings."
111         echo "   Hit [ctrl-c] to break"
112         read 
113         ;;
114       DIABLO|FREMANTLE)
115         echo "Click \"I Agree\" to ignore the above errors/warnings. Ask for" \
116           "help if you don't know what to do." >> /tmp/busybox-power-error
117         echo "Please confirm the text on the screen of your device"
118         maemo-confirm-text "Attention!" /tmp/busybox-power-error
119         res=$?
120         rm /tmp/busybox-power-error
121         if test ! $res == 0; then exit 1; fi
122         ;;
123       HARMATTAN)
124         echo "Click \"I Agree\" to ignore the above errors/warnings. Ask for" \
125           "help if you don't know what to do." >> /tmp/busybox-power-error
126         echo "Please confirm the text on the screen of your device"
127         # We're too privileged to call meego-confirm-text...
128         # Our source is com.nokia.maemo, whilst m-c-t is from an unknown source
129         /usr/bin/aegis-exec meego-confirm-text "Attention!" \
130           /tmp/busybox-power-error > /dev/null 2>&1
131         res=$?
132         rm /tmp/busybox-power-error
133         if test ! $res == 0; then exit 1; fi
134         ;;
135       esac
136 }
137
138 # Rollback procedure for Harmattan based devices
139 ROLLBACK_HARMATTAN() {
140     echo -e "\nWarning: an error has occured! Rolling back..."
141     $EXECPWR cp -af $TMPBINBAK /bin/busybox
142     $EXECPWR cp -a $TMPHASHBAK /var/lib/aegis/refhashlist
143     echo "End of roll-back"
144     exit 1
145 }
146
147 # Uninstallation of the enhanced binary
148 UNINSTALL() {
149     if test $MODIFIEDBIN == 1; then
150       # /bin/busybox has been modified since installing busybox-power
151       # Leave both the file and the diversion in place
152       return
153     fi
154
155     case $ENVIRONMENT in
156       SDK|DIABLO|FREMANTLE)
157         if test -e $DISTBIN; then
158           cp -af $DISTBIN /bin/busybox
159           if test -e /bin/busybox; then
160             rm $DISTBIN; fi
161         elif test "$ENVIRONMENT" == "SDK"; then
162           # There was no /bin/busybox to begin with..
163           rm /bin/busybox
164         fi
165
166         /usr/sbin/dpkg-divert --remove /bin/busybox
167         ;;
168       HARMATTAN)
169         if ! test -e $DISTBIN; then return; fi
170
171         TMPHASHBAK=`$EXECPWR mktemp`
172         TMPBINBAK=`$EXECPWR mktemp`
173         ORIGINCHECK=`GETORIGINCHECK_STATUS`
174
175         # Useful information for Harmattan-based devices
176         ECHO_VERBOSE "refhashlist backup: $TMPHASHBAK"
177         ECHO_VERBOSE "busybox backup: $TMPBINBAK"
178         ECHO_VERBOSE "instbinary: $INSTBINARY_SHA1"
179         ECHO_VERBOSE "origbinary: $ORIGBINARY_SHA1"
180         ECHO_VERBOSE "device mode: $DEVICEMODE"
181         ECHO_VERBOSE "origincheck: $ORIGINCHECK"
182
183         if test $ORIGINCHECK -eq 1; then
184           SETORIGINCHECK_STATUS 0; fi
185
186         $EXECPWR cp -a /bin/busybox $TMPBINBAK || exit 1
187         $EXECPWR cp -a /var/lib/aegis/refhashlist $TMPHASHBAK || exit 1
188         $EXECPWR cp -af $DISTBIN /bin/busybox || ROLLBACK_HARMATTAN
189         $EXECPWR sed -i "s/$INSTBINARY_SHA1/$ORIGBINARY_SHA1/" /var/lib/aegis/refhashlist || ROLLBACK_HARMATTAN
190         /usr/bin/accli -c tcb-sign -F /var/lib/aegis/refhashlist < /var/lib/aegis/refhashlist || ROLLBACK_HARMATTAN
191         /usr/sbin/validator-init
192
193         if test $ORIGINCHECK -eq 1; then
194           SETORIGINCHECK_STATUS 1; fi
195
196         rm $TMPBINBAK
197         rm $TMPHASHBAK
198         rm $DISTBIN
199         ;;
200     esac
201 }
202
203 # Remove all symlinks that the installation script has made
204 UNSYMLINK() {
205     # Load list of installed symlinks
206     touch $INSTALLDIR/busybox-power.symlinks
207     source $INSTALLDIR/busybox-power.symlinks
208
209     # Walk through all possible destinations
210     for DESTDIR in $DESTINATIONS; do 
211       # Enable us to see all entries in $DESTINATIONS as variables
212       eval "APPLICATIONS=\$$DESTDIR"
213       # Set destination directory accordingly
214       case $DESTDIR in
215         DEST_BIN)
216           DIR="/bin"
217           ;;
218         DEST_SBIN)
219           DIR="/sbin"
220           ;;
221         DEST_USRBIN)
222           DIR="/usr/bin"
223           ;;
224         DEST_USRSBIN)
225           DIR="/usr/sbin"
226           ;;
227       esac
228
229       ECHO_VERBOSE "\nRemoving symlinks in $DIR"
230       # Walk through all applications from the current destination
231       for APP in $APPLICATIONS; do
232         # The following code is executed for every application in the current
233         # destination
234         if test -h $DIR/$APP; then 
235           # Good, this app is still a symbolic link ..
236           if test -n "`ls -l $DIR/$APP | grep busybox`"; then
237             ECHO_VERBOSE "Removing link: $DIR/$APP"
238             rm $DIR/$APP
239           fi
240         fi
241       done
242     done
243 }
244
245 # Action to be performed after restoring original busybox
246 CLEANUP() {
247     OLDFILES="busybox-power.symlinks
248       busybox.distrib.version
249       busybox.distrib.sha1"
250
251     for file in $OLDFILES; do
252       if test -e $INSTALLDIR/$file; then
253         rm $INSTALLDIR/$file
254       fi
255     done
256
257     # Clean up trap (from busybox-power <= 1.20.2power1)
258     # Should be removed by the old prerm, but may be left behind when that one
259     # fails and dpkg uses this one instead. Therefore, keep this cleanup
260     # included for a few busybox-power releases.
261     line="trap exit SIGHUP #by busybox-power"
262     if grep -F -q "$line" /etc/profile; then
263       sed "/$line/d" /etc/profile > /tmp/profile.sed
264       mv /tmp/profile.sed /etc/profile
265     fi
266 }
267
268 ### Codepath ###
269 ECHO_VERBOSE "busybox-power: verbose mode"
270 ECHO_VERBOSE "  binary: $EXECPWR"
271 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
272 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
273
274 CHECK_SYMLINKSFILE
275 if test "$ENVIRONMENT" != "SDK"; then
276   CHECK_ROOT
277 fi
278 CHECK_BACKUP
279 CHECK_INSTALLEDBIN
280 if test -e /tmp/busybox-power-error; then
281   # An error has occured during the checks
282   DISPLAY_ERRORS
283 fi
284 UNSYMLINK
285 UNINSTALL
286 CLEANUP
287