tweak hashing and backup logic
[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 VERBOSE="0"
17 MODIFIEDBIN="0"
18 DIVERTED="0"
19
20 INSTBINARY_SHA1=`sha1sum $EXECPWR | awk '{ print $1 }'`
21 if test -e $INSTALLDIR/busybox.original; then
22   ORIGBINARY_SHA1=`sha1sum $INSTALLDIR/busybox.original | 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 for a diversion of /bin/busybox
35 CHECK_DIVERT() {
36     if test -e /bin/busybox.distrib; then
37       DIVERTED="1"; fi
38 }
39
40 # Check the (integrity) of our BusyBox backup
41 CHECK_BACKUP() {
42     # SDK doesn't ship with BusyBox by default, there might be no backup at all
43     if test ! -e $INSTALLDIR/busybox.original -a "$ENVIRONMENT" == "SDK" ; then
44       return; fi
45
46     # Firstly, check whether the backup still exists
47     if test ! -e $INSTALLDIR/busybox.original; then
48       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
49       return
50     fi
51
52     # Secondly, check the integrity of the backup
53     if test -e $INSTALLDIR/busybox.original.sha1; then
54       if test ! "`cat $INSTALLDIR/busybox.original.sha1`" == "$ORIGBINARY_SHA1"; then
55         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 $INSTALLDIR/busybox.original isn't corrupted.\n" >> /tmp/busybox-power-error
56       fi
57     else
58       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
59     fi
60 }
61
62 # Check whether /bin/busybox has been modified after bb-power's installation
63 CHECK_INSTALLEDBIN() {
64     if test ! "$INSTBINARY_SHA1" == "`sha1sum /bin/busybox | awk '{ print $1 }'`"; then
65       echo -e "Warning: /bin/busybox has been modified since installing busybox-power (invalid SHA1 checksum). Your current /bin/busybox won't be touched, our backup of the original /bin/busybox will be copied to /opt/busybox.original. \n" >> /tmp/busybox-power-error
66       MODIFIEDBIN="1"
67     fi
68 }
69
70 # Display encountered errors
71 DISPLAY_ERRORS() {
72     case $ENVIRONMENT in
73       SDK)
74         echo -e "\n\n-----------Attention!-----------"
75         cat /tmp/busybox-power-error
76         rm /tmp/busybox-power-error
77         echo "-> Please press [enter] to ignore the above errors/warnings."
78         echo "   Hit [ctrl-c] to break"
79         read 
80         ;;
81       FREMANTLE)
82         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
83         echo "Please confirm the text on the screen of your device"
84         maemo-confirm-text "Attention!" /tmp/busybox-power-error
85         res=$?
86         rm /tmp/busybox-power-error
87         if test ! $res == 0; then exit 1; fi
88         ;;
89       esac
90 }
91
92 # Uninstallation of the enhanced binary
93 UNINSTALL() {
94     if test $DIVERTED == 1; then
95       # A package tried to install /bin/busybox since installing busybox-power
96       # This binary has priority over our own (old) backup
97       mv -f /bin/busybox.distrib /bin/busybox
98       rm $INSTALLDIR/busybox.original
99     elif test $MODIFIEDBIN == 1; then
100       # /bin/busybox has been modified since installing busybox-power
101       # Do not overwrite this modified version with our backup
102       mv -f $INSTALLDIR/busybox.original /opt/busybox.original
103     elif test -e $INSTALLDIR/busybox.original; then
104       cp -f $INSTALLDIR/busybox.original /bin/busybox
105       if test -e /bin/busybox; then
106         rm $INSTALLDIR/busybox.original; fi
107     elif test "$ENVIRONMENT" == "SDK"; then
108       # There was no /bin/busybox to begin with..
109       rm /bin/busybox
110     fi
111
112     /usr/sbin/dpkg-divert --remove /bin/busybox
113 }
114
115 # Remove all symlinks that the installation script has made
116 UNSYMLINK() {
117     # Load list of installed symlinks
118     touch $INSTALLDIR/busybox-power.symlinks
119     source $INSTALLDIR/busybox-power.symlinks
120
121     # Walk through all possible destinations
122     for DESTDIR in $DESTINATIONS; do 
123       # Enable us to see all entries in $DESTINATIONS as variables
124       eval "APPLICATIONS=\$$DESTDIR"
125       # Set destination directory accordingly
126       case $DESTDIR in
127         DEST_BIN)
128           DIR="/bin"
129           ;;
130         DEST_SBIN)
131           DIR="/sbin"
132           ;;
133         DEST_USRBIN)
134           DIR="/usr/bin"
135           ;;
136         DEST_USRSBIN)
137           DIR="/usr/sbin"
138           ;;
139       esac
140
141       ECHO_VERBOSE "\nRemoving symlinks in $DIR"
142       # Walk through all applications from the current destination
143       for APP in $APPLICATIONS; do
144         # The following code is executed for every application in the current destination
145         if test -h $DIR/$APP; then 
146           # Good, this app is still a symbolic link ..
147           if test -n "`ls -l $DIR/$APP | grep busybox`"; then
148             ECHO_VERBOSE "Removing link: $DIR/$APP"
149             rm $DIR/$APP
150           fi
151         fi
152       done
153     done
154 }
155
156 # Action to be performed after restoring original busybox
157 CLEANUP() {
158     OLDFILES="busybox-power.symlinks
159       busybox.original.sha1"
160
161     for file in $OLDFILES; do
162       if test -e $INSTALLDIR/$file; then
163         rm $INSTALLDIR/$file
164       fi
165     done
166 }
167
168 ### Codepath ###
169 ECHO_VERBOSE "busybox-power: verbose mode"
170 ECHO_VERBOSE "  binary: $EXECPWR"
171 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
172 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
173
174 CHECK_STANDALONE
175 CHECK_SYMLINKSFILE
176 CHECK_DIVERT
177 if test "$ENVIRONMENT" != "SDK"; then
178   CHECK_ROOT
179   CHECK_BACKUP
180   CHECK_INSTALLEDBIN
181 fi
182 if test -e /tmp/busybox-power-error; then
183   # An error has occured during the checks
184   DISPLAY_ERRORS
185 fi
186 UNSYMLINK
187 UNINSTALL
188 CLEANUP
189