7e23d8e54b3694e8f7536b971993dd69cc0a8697
[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 # Last updated: 08-24-2012 (MM-DD-YYYY)
14
15
16 INSTALLDIR="/opt/busybox-power"
17 EXECPWR="$INSTALLDIR/busybox.power"
18 VERBOSE="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 the (integrity) of our BusyBox backup
31 CHECK_BACKUP() {
32     # Firstly, check whether the backup still exists
33     if test ! -e $INSTALLDIR/busybox.original; then
34       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
35       return
36     fi
37
38     # Secondly, check the integrity of the backup
39     if test -e $INSTALLDIR/busybox.original.md5; then
40       INSTBINARY_MD5=`cat $INSTALLDIR/busybox.original.md5`
41       ORIGBINARY_MD5=`md5sum $INSTALLDIR/busybox.original | awk '{ print $1 }'`
42       if test ! "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
43         echo -e "Warning: the backed-up original binary has been modified since installing busybox-power (invalid md5 checksum). Do not continue unless you're sure $INSTALLDIR/busybox.original isn't corrupted.\n" >> /tmp/busybox-power-error
44       fi
45     else
46       echo -e "Warning: couldn't load the saved md5 checksum of the original binary; the integrity of the backup of the original binary can not be guaranteed.\n" >> /tmp/busybox-power-error
47     fi
48 }
49
50 # Check whether /bin/busybox has been modified after bb-power's installation
51 CHECK_INSTALLEDBIN() {
52     if test -e $INSTALLDIR/busybox.power.md5; then
53       INSTBINARY_MD5=`md5sum /bin/busybox | awk '{ print $1 }'`
54       ORIGBINARY_MD5=`cat $INSTALLDIR/busybox.power.md5`
55       if test ! "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
56         echo -e "Warning: /bin/busybox has been modified since installing busybox-power (invalid md5 checksum). The original BusyBox binary at the time of installation will replace it if you continue.\n" >> /tmp/busybox-power-error
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 -e $INSTALLDIR/busybox.original; then
86       cp -f $INSTALLDIR/busybox.original /bin/busybox
87       if test -e /bin/busybox; then
88         rm $INSTALLDIR/busybox.original; fi
89     else
90       if test "$ENVIRONMENT" == "SDK"; then
91         # There was no /bin/busybox to begin with..
92         rm /bin/busybox
93       fi
94     fi
95 }
96
97 # Remove all symlinks that the installation script has made
98 UNSYMLINK() {
99     # Load list of installed symlinks
100     touch $INSTALLDIR/busybox-power.symlinks
101     source $INSTALLDIR/busybox-power.symlinks
102
103     # Walk through all possible destinations
104     for DESTDIR in $DESTINATIONS; do 
105       # Enable us to see all entries in $DESTINATIONS as variables
106       eval "APPLICATIONS=\$$DESTDIR"
107       # Set destination directory accordingly
108       case $DESTDIR in
109         DEST_BIN)
110           DIR="/bin"
111           ;;
112         DEST_SBIN)
113           DIR="/sbin"
114           ;;
115         DEST_USRBIN)
116           DIR="/usr/bin"
117           ;;
118         DEST_USRSBIN)
119           DIR="/usr/sbin"
120           ;;
121       esac
122
123       ECHO_VERBOSE "\nRemoving symlinks in $DIR"
124       # Walk through all applications from the current destination
125       for APP in $APPLICATIONS; do
126         # The following code is executed for every application in the current destination
127         if test -h $DIR/$APP; then 
128           # Good, this app is still a symbolic link ..
129           if test -n "`ls -l $DIR/$APP | grep busybox`"; then
130             ECHO_VERBOSE "Removing link: $DIR/$APP"
131             rm $DIR/$APP
132           fi
133         fi
134       done
135     done
136 }
137
138 # Action to be performed after restoring original busybox
139 CLEANUP() {
140     OLDFILES="busybox-power.symlinks
141       busybox.power.md5
142       busybox.original.md5"
143
144     for file in $OLDFILES; do
145       if test -e $INSTALLDIR/$file; then
146         rm $INSTALLDIR/$file
147       fi
148     done
149 }
150
151 ### Codepath ###
152 ECHO_VERBOSE "busybox-power: verbose mode"
153 ECHO_VERBOSE "  binary: $EXECPWR"
154 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
155 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
156
157 CHECK_STANDALONE
158 CHECK_SYMLINKSFILE
159 if test "$ENVIRONMENT" != "SDK"; then
160   CHECK_ROOT
161   CHECK_BACKUP
162   CHECK_INSTALLEDBIN
163 fi
164 if test -e /tmp/busybox-power-error; then
165   # An error has occured during the checks
166   DISPLAY_ERRORS
167 fi
168 UNSYMLINK
169 UNINSTALL
170 CLEANUP
171