use the diversion target for backing up the original binary
[busybox-power] / debian / scripts / uninstall-binary.sh
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 VERBOSE="0"
18 MODIFIEDBIN="0"
19
20 INSTBINARY_SHA1=`sha1sum $EXECPWR | awk '{ print $1 }'`
21 if test -e $DISTBIN; then
22   ORIGBINARY_SHA1=`sha1sum $DISTBIN | awk '{ print $1 }'`; fi
23
24 # Load shared functions
25 source $INSTALLDIR/functions
26
27 # Check whether we can load the list of created symlinks during installation
28 CHECK_SYMLINKSFILE() {
29     if test ! -e $INSTALLDIR/busybox-power.symlinks; then
30       echo -e "Error: cannot find the list of symlinks to be removed. No symlinks will be removed at all!\n" >> /tmp/busybox-power-error
31     fi
32 }
33
34 # Check the (integrity) of our BusyBox backup
35 CHECK_BACKUP() {
36     # SDK doesn't ship with BusyBox by default, there might be no backup at all
37     if test ! -e $DISTBIN -a "$ENVIRONMENT" == "SDK" ; then
38       return; fi
39
40     # Firstly, check whether the backup still exists
41     if test ! -e $DISTBIN; then
42       echo -e "Error: original binary is missing! Continuing will only remove the symlinks made during installation, /bin/busybox stays untouched.\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         echo -e "Warning: the backed-up original binary has been modified since installing busybox-power (invalid SHA1 checksum). Do not continue unless you're sure $DISTBIN isn't corrupted.\n" >> /tmp/busybox-power-error
50       fi
51     else
52       echo -e "Warning: couldn't load the saved SHA1 checksum of the original binary; the integrity of the backup of the original binary can not be guaranteed.\n" >> /tmp/busybox-power-error
53     fi
54 }
55
56 # Check whether /bin/busybox has been modified after bb-power's installation
57 CHECK_INSTALLEDBIN() {
58     if test ! "$INSTBINARY_SHA1" == "`sha1sum /bin/busybox | awk '{ print $1 }'`"; then
59       echo -e "Warning: /bin/busybox has been modified since installing busybox-power (invalid SHA1 checksum). Your current /bin/busybox won't be touched and the diversion of /bin/busybox to $DISTBIN will not be removed. \n" >> /tmp/busybox-power-error
60       MODIFIEDBIN="1"
61     fi
62 }
63
64 # Display encountered errors
65 DISPLAY_ERRORS() {
66     case $ENVIRONMENT in
67       SDK)
68         echo -e "\n\n-----------Attention!-----------"
69         cat /tmp/busybox-power-error
70         rm /tmp/busybox-power-error
71         echo "-> Please press [enter] to ignore the above errors/warnings."
72         echo "   Hit [ctrl-c] to break"
73         read 
74         ;;
75       FREMANTLE)
76         echo "Click \"I Agree\" to ignore the above errors/warnings. Ask for help if you don't know what to do." >> /tmp/busybox-power-error
77         echo "Please confirm the text on the screen of your device"
78         maemo-confirm-text "Attention!" /tmp/busybox-power-error
79         res=$?
80         rm /tmp/busybox-power-error
81         if test ! $res == 0; then exit 1; fi
82         ;;
83       esac
84 }
85
86 # Uninstallation of the enhanced binary
87 UNINSTALL() {
88     if test $MODIFIEDBIN == 1; then
89       # /bin/busybox has been modified since installing busybox-power
90       # Leave both the file and the diversion in place
91       return
92     elif test -e $DISTBIN; then
93       cp -af $DISTBIN /bin/busybox
94       if test -e /bin/busybox; then
95         rm $DISTBIN; fi
96     elif test "$ENVIRONMENT" == "SDK"; then
97       # There was no /bin/busybox to begin with..
98       rm /bin/busybox
99     fi
100
101     /usr/sbin/dpkg-divert --remove /bin/busybox
102 }
103
104 # Remove all symlinks that the installation script has made
105 UNSYMLINK() {
106     # Load list of installed symlinks
107     touch $INSTALLDIR/busybox-power.symlinks
108     source $INSTALLDIR/busybox-power.symlinks
109
110     # Walk through all possible destinations
111     for DESTDIR in $DESTINATIONS; do 
112       # Enable us to see all entries in $DESTINATIONS as variables
113       eval "APPLICATIONS=\$$DESTDIR"
114       # Set destination directory accordingly
115       case $DESTDIR in
116         DEST_BIN)
117           DIR="/bin"
118           ;;
119         DEST_SBIN)
120           DIR="/sbin"
121           ;;
122         DEST_USRBIN)
123           DIR="/usr/bin"
124           ;;
125         DEST_USRSBIN)
126           DIR="/usr/sbin"
127           ;;
128       esac
129
130       ECHO_VERBOSE "\nRemoving symlinks in $DIR"
131       # Walk through all applications from the current destination
132       for APP in $APPLICATIONS; do
133         # The following code is executed for every application in the current destination
134         if test -h $DIR/$APP; then 
135           # Good, this app is still a symbolic link ..
136           if test -n "`ls -l $DIR/$APP | grep busybox`"; then
137             ECHO_VERBOSE "Removing link: $DIR/$APP"
138             rm $DIR/$APP
139           fi
140         fi
141       done
142     done
143 }
144
145 # Action to be performed after restoring original busybox
146 CLEANUP() {
147     OLDFILES="busybox-power.symlinks
148       busybox.distrib.sha1"
149
150     for file in $OLDFILES; do
151       if test -e $INSTALLDIR/$file; then
152         rm $INSTALLDIR/$file
153       fi
154     done
155 }
156
157 ### Codepath ###
158 ECHO_VERBOSE "busybox-power: verbose mode"
159 ECHO_VERBOSE "  binary: $EXECPWR"
160 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
161 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
162
163 CHECK_STANDALONE
164 CHECK_SYMLINKSFILE
165 if test "$ENVIRONMENT" != "SDK"; then
166   CHECK_ROOT
167   CHECK_BACKUP
168   CHECK_INSTALLEDBIN
169 fi
170 if test -e /tmp/busybox-power-error; then
171   # An error has occured during the checks
172   DISPLAY_ERRORS
173 fi
174 UNSYMLINK
175 UNINSTALL
176 CLEANUP
177