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