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