add check for missing busybox.distrib.version in prerm
[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 CHECK_INSTALLEDBIN() {
75     if test "$INSTBINARY_SHA1" != "`sha1sum /bin/busybox | awk '{ print $1 }'`"; then
76       echo -e "Warning: /bin/busybox has been modified since installing" \
77         "busybox-power (invalid SHA1 checksum). Your current /bin/busybox" \
78         "won't be touched and the diversion of /bin/busybox to $DISTBIN will" \
79         "not be removed. \n" >> /tmp/busybox-power-error
80       MODIFIEDBIN="1"
81     fi
82 }
83
84 # Display encountered errors
85 DISPLAY_ERRORS() {
86     case $ENVIRONMENT in
87       SDK)
88         echo -e "\n\n-----------Attention!-----------"
89         cat /tmp/busybox-power-error
90         rm /tmp/busybox-power-error
91         echo "-> Please press [enter] to ignore the above errors/warnings."
92         echo "   Hit [ctrl-c] to break"
93         read 
94         ;;
95       FREMANTLE)
96         echo "Click \"I Agree\" to ignore the above errors/warnings. Ask for" \
97           "help if you don't know what to do." >> /tmp/busybox-power-error
98         echo "Please confirm the text on the screen of your device"
99         maemo-confirm-text "Attention!" /tmp/busybox-power-error
100         res=$?
101         rm /tmp/busybox-power-error
102         if test ! $res == 0; then exit 1; fi
103         ;;
104       esac
105 }
106
107 # Uninstallation of the enhanced binary
108 UNINSTALL() {
109     if test $MODIFIEDBIN == 1; then
110       # /bin/busybox has been modified since installing busybox-power
111       # Leave both the file and the diversion in place
112       return
113     elif test -e $DISTBIN; then
114       cp -af $DISTBIN /bin/busybox
115       if test -e /bin/busybox; then
116         rm $DISTBIN; fi
117     elif test "$ENVIRONMENT" == "SDK"; then
118       # There was no /bin/busybox to begin with..
119       rm /bin/busybox
120     fi
121
122     /usr/sbin/dpkg-divert --remove /bin/busybox
123 }
124
125 # Remove all symlinks that the installation script has made
126 UNSYMLINK() {
127     # Load list of installed symlinks
128     touch $INSTALLDIR/busybox-power.symlinks
129     source $INSTALLDIR/busybox-power.symlinks
130
131     # Walk through all possible destinations
132     for DESTDIR in $DESTINATIONS; do 
133       # Enable us to see all entries in $DESTINATIONS as variables
134       eval "APPLICATIONS=\$$DESTDIR"
135       # Set destination directory accordingly
136       case $DESTDIR in
137         DEST_BIN)
138           DIR="/bin"
139           ;;
140         DEST_SBIN)
141           DIR="/sbin"
142           ;;
143         DEST_USRBIN)
144           DIR="/usr/bin"
145           ;;
146         DEST_USRSBIN)
147           DIR="/usr/sbin"
148           ;;
149       esac
150
151       ECHO_VERBOSE "\nRemoving symlinks in $DIR"
152       # Walk through all applications from the current destination
153       for APP in $APPLICATIONS; do
154         # The following code is executed for every application in the current
155         # destination
156         if test -h $DIR/$APP; then 
157           # Good, this app is still a symbolic link ..
158           if test -n "`ls -l $DIR/$APP | grep busybox`"; then
159             ECHO_VERBOSE "Removing link: $DIR/$APP"
160             rm $DIR/$APP
161           fi
162         fi
163       done
164     done
165 }
166
167 # Action to be performed after restoring original busybox
168 CLEANUP() {
169     OLDFILES="busybox-power.symlinks
170       busybox.distrib.version
171       busybox.distrib.sha1"
172
173     for file in $OLDFILES; do
174       if test -e $INSTALLDIR/$file; then
175         rm $INSTALLDIR/$file
176       fi
177     done
178
179     # Clean up trap (from busybox-power <= 1.20.2power1)
180     # Should be removed by the old prerm, but may be left behind when that one
181     # fails and dpkg uses this one instead. Therefore, keep this cleanup
182     # included for a few busybox-power releases.
183     line="trap exit SIGHUP #by busybox-power"
184     if grep -F -q "$line" /etc/profile; then
185       sed "/$line/d" /etc/profile > /tmp/profile.sed
186       mv /tmp/profile.sed /etc/profile
187     fi
188 }
189
190 ### Codepath ###
191 ECHO_VERBOSE "busybox-power: verbose mode"
192 ECHO_VERBOSE "  binary: $EXECPWR"
193 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
194 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
195
196 CHECK_SYMLINKSFILE
197 if test "$ENVIRONMENT" != "SDK"; then
198   CHECK_ROOT
199 fi
200 CHECK_BACKUP
201 CHECK_INSTALLEDBIN
202 if test -e /tmp/busybox-power-error; then
203   # An error has occured during the checks
204   DISPLAY_ERRORS
205 fi
206 UNSYMLINK
207 UNINSTALL
208 CLEANUP
209