use SHA1 hashes instead of MD5 hashes
[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
19 # Load shared functions
20 source $INSTALLDIR/functions
21
22 # Check whether we can load the list of created symlinks during installation
23 CHECK_SYMLINKSFILE() {
24     if test ! -e $INSTALLDIR/busybox-power.symlinks; then
25       echo -e "Error: cannot find the list of symlinks to be removed. No symlinks will be removed at all!\n" >> /tmp/busybox-power-error
26     fi
27 }
28
29 # Check the (integrity) of our BusyBox backup
30 CHECK_BACKUP() {
31     # Firstly, check whether the backup still exists
32     if test ! -e $INSTALLDIR/busybox.original; then
33       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
34       return
35     fi
36
37     # Secondly, check the integrity of the backup
38     if test -e $INSTALLDIR/busybox.original.sha1; then
39       INSTBINARY_SHA1=`cat $INSTALLDIR/busybox.original.sha1`
40       ORIGBINARY_SHA1=`sha1sum $INSTALLDIR/busybox.original | awk '{ print $1 }'`
41       if test ! "$INSTBINARY_SHA1" == "$ORIGBINARY_SHA1"; then
42         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
43       fi
44     else
45       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
46     fi
47 }
48
49 # Check whether /bin/busybox has been modified after bb-power's installation
50 CHECK_INSTALLEDBIN() {
51     if test -e $INSTALLDIR/busybox.power.sha1; then
52       INSTBINARY_SHA1=`sha1sum /bin/busybox | awk '{ print $1 }'`
53       ORIGBINARY_SHA1=`cat $INSTALLDIR/busybox.power.sha1`
54       if test ! "$INSTBINARY_SHA1" == "$ORIGBINARY_SHA1"; then
55         echo -e "Warning: /bin/busybox has been modified since installing busybox-power (invalid SHA1 checksum). This can be the result of a busybox upgrade, e.g. from CSSU. 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
56         MODIFIEDBIN="1"
57       fi
58     fi
59 }
60
61 # Display encountered errors
62 DISPLAY_ERRORS() {
63     case $ENVIRONMENT in
64       SDK)
65         echo -e "\n\n-----------Attention!-----------"
66         cat /tmp/busybox-power-error
67         rm /tmp/busybox-power-error
68         echo "-> Please press [enter] to ignore the above errors/warnings."
69         echo "   Hit [ctrl-c] to break"
70         read 
71         ;;
72       FREMANTLE)
73         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
74         echo "Please confirm the text on the screen of your device"
75         maemo-confirm-text "Attention!" /tmp/busybox-power-error
76         res=$?
77         rm /tmp/busybox-power-error
78         if test ! $res == 0; then exit 1; fi
79         ;;
80       esac
81 }
82
83 # Uninstallation of the enhanced binary
84 UNINSTALL() {
85     if test $MODIFIEDBIN == 1; then
86       # /bin/busybox has been modified since installing busybox-power
87       # Do not overwrite this modified version with our backup
88       mv $INSTALLDIR/busybox.original /opt/busybox.original
89       return
90     fi
91     if test -e $INSTALLDIR/busybox.original; then
92       cp -f $INSTALLDIR/busybox.original /bin/busybox
93       if test -e /bin/busybox; then
94         rm $INSTALLDIR/busybox.original; fi
95     else
96       if test "$ENVIRONMENT" == "SDK"; then
97         # There was no /bin/busybox to begin with..
98         rm /bin/busybox
99       fi
100     fi
101 }
102
103 # Remove all symlinks that the installation script has made
104 UNSYMLINK() {
105     # Load list of installed symlinks
106     touch $INSTALLDIR/busybox-power.symlinks
107     source $INSTALLDIR/busybox-power.symlinks
108
109     # Walk through all possible destinations
110     for DESTDIR in $DESTINATIONS; do 
111       # Enable us to see all entries in $DESTINATIONS as variables
112       eval "APPLICATIONS=\$$DESTDIR"
113       # Set destination directory accordingly
114       case $DESTDIR in
115         DEST_BIN)
116           DIR="/bin"
117           ;;
118         DEST_SBIN)
119           DIR="/sbin"
120           ;;
121         DEST_USRBIN)
122           DIR="/usr/bin"
123           ;;
124         DEST_USRSBIN)
125           DIR="/usr/sbin"
126           ;;
127       esac
128
129       ECHO_VERBOSE "\nRemoving symlinks in $DIR"
130       # Walk through all applications from the current destination
131       for APP in $APPLICATIONS; do
132         # The following code is executed for every application in the current destination
133         if test -h $DIR/$APP; then 
134           # Good, this app is still a symbolic link ..
135           if test -n "`ls -l $DIR/$APP | grep busybox`"; then
136             ECHO_VERBOSE "Removing link: $DIR/$APP"
137             rm $DIR/$APP
138           fi
139         fi
140       done
141     done
142 }
143
144 # Action to be performed after restoring original busybox
145 CLEANUP() {
146     OLDFILES="busybox-power.symlinks
147       busybox.power.sha1
148       busybox.original.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