26a130b9f47e4ff63887ea6a07b52ab47c847d77
[busybox-power] / debian / scripts / uninstall-binary.sh
1 #!/bin/sh
2 # A script to restore /bin/busybox and delete created symlinks as defined in $INSTALLDIR/installed-symlinks
3 #
4 # Symbolic links are only removed if they are
5 # a) created by the installer script ("install-binary.sh")
6 # b) not replaced by a binary (i.e. they are still a symbolic link)
7 # c) pointing to a busybox binary
8
9 # By Dennis Groenen <tj.groenen@gmail.com>
10 # GPLv3 licensed
11
12 # Version 0.4 08-02-2011 (MM-DD-YYYY)
13 # 0.1: Initial release
14 # 0.2: Minor clean-ups and be quieter
15 # 0.3: Add support for multiple environments
16 #      Make use of functions in this script
17 #      Implement additional checks
18 # 0.4: Add support for undoing symlinks to busybox_root
19 #      Update email address
20
21 INSTALLDIR="/opt/busybox-power"
22 EXECPWR="$INSTALLDIR/busybox.power"
23 VERBOSE="0"
24
25 # Print extra information in verbose mode
26 if test $VERBOSE == 1; then 
27   echo "busybox-power: verbose mode" \ 
28   echo "  binary: $EXECPWR" \ 
29   echo "  version string: `$EXECPWR | $EXECPWR head -n 1`"
30 fi
31
32 # Detect environment
33 CHECK_ENV() {
34     if test -d /scratchbox
35       then
36         ENVIRONMENT="SDK"
37       else
38         PROD=$(cat /proc/component_version | grep product | cut -d" " -f 6)
39         case $PROD in
40           RX-51)
41             ENVIRONMENT="N900"
42           ;;
43           *)
44             # Unsupported, use the least strict environment (SDK)
45             ENVIRONMENT="SDK"
46           ;;
47         esac
48     fi
49
50     if test $VERBOSE == 1; then echo "  environment: $ENVIRONMENT"; fi
51 }
52
53 # Environment-independent checks before continuing
54 GENERIC_CHECKS() {
55     #if test -n "`pgrep dpkg`" -o "`pgrep apt`"
56     if ! lsof /var/lib/dpkg/lock >> /dev/null; then 
57       echo "error: you're running me as a stand-alone application"
58       echo "  do not do this, I will be called automatically upon"
59       echo "  deinstallation of busybox-power"
60       exit 1
61     fi
62
63     if test ! -e $INSTALLDIR/busybox-power.symlinks; then
64       echo -e "Error: cannot find the list of symlinks to be removed. No symlinks will be removed at all!\n" >> /tmp/busybox-power-error
65     fi
66 }
67
68 # Additional checks for the N900
69 E_N900_CHECKS() {
70     if test "`id -u`" -ne 0; then
71       echo "error: you're not running me as root, aborting"
72       echo "  also, DO NOT run me as a stand-alone application"
73       echo "  I will be called automatically upon deinstallation"
74       echo "  of busybox-power"
75       exit 1
76     fi
77
78     if test ! -e $INSTALLDIR/busybox.original; then
79       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
80     fi
81 }
82
83 # N900-specific code executed prior to uninstalling the enhanced binary
84 E_N900_PRERM() {
85     if test -e $INSTALLDIR/busybox.power.md5
86       then
87         INSTBINARY_MD5=`md5sum /bin/busybox | awk '{ print $1 }'`
88         ORIGBINARY_MD5=`cat $INSTALLDIR/busybox.power.md5`
89         if test ! "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
90           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
91         fi
92     fi
93
94     if test -e $INSTALLDIR/busybox.original.md5
95       then
96         INSTBINARY_MD5=`cat $INSTALLDIR/busybox.original.md5`
97         ORIGBINARY_MD5=`md5sum $INSTALLDIR/busybox.original | awk '{ print $1 }'`
98         if test ! "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
99           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
100         fi
101       else
102         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
103     fi
104 }
105
106 # Display encountered errors
107 DISPLAY_ERRORS() {
108       case $ENVIRONMENT in
109         SDK)
110           echo -e "\n\n-----------Attention!-----------"
111           cat /tmp/busybox-power-error
112           rm /tmp/busybox-power-error
113           echo "-> Please press [enter] to ignore the above errors/warnings. Hit [ctrl-c] to break"
114           read 
115         ;;
116         N900)
117           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
118           maemo-confirm-text "Attention!" /tmp/busybox-power-error
119           res=$?
120           rm /tmp/busybox-power-error
121           if test ! $res == 0; then exit 1; fi
122         ;;
123       esac
124
125       touch $INSTALLDIR/busybox-power.symlinks
126 }
127
128 # Uninstallation of the enhanced binary on the N900
129 E_N900_UNINST() {
130     if test -e $INSTALLDIR/busybox.original 
131       then
132         cp -f $INSTALLDIR/busybox.original /bin/busybox
133         if test -e /bin/busybox; then rm $INSTALLDIR/busybox.original; fi
134     fi 
135 }
136
137 # Uninstallation of the enhanced binary in Maemo's SDK
138 E_SDK_UNINST() {
139     if test -e $INSTALLDIR/busybox.original
140       then
141         cp -f $INSTALLDIR/busybox.original /bin/busybox
142         if test -e /bin/busybox; then rm $INSTALLDIR/busybox.original; fi
143       else
144         rm /bin/busybox
145     fi
146 }
147
148 # Remove all symlinks that busybox-power has made
149 UNSYMLINK() {
150     # Load list of installed symlinks
151     source $INSTALLDIR/busybox-power.symlinks
152
153     # Walk through all possible destinations
154     for DESTDIR in $DESTINATIONS
155       do 
156         # Enable us to see all entries in $DESTIONATION as variables
157         eval "APPLICATIONS=\$$DESTDIR"
158         # Set destination dirrectory accordingly
159         case $DESTDIR in
160           *DEST_BIN)
161             DIR="/bin"
162           ;;
163           *DEST_SBIN)
164             DIR="/sbin"
165           ;;
166           *DEST_USRBIN)
167             DIR="/usr/bin"
168           ;;
169           *DEST_USRSBIN)
170             DIR="/usr/sbin"
171           ;;
172         esac
173
174       # Determine whether we've symlinked to busybox or busybox_root
175       if test `echo $DESTDIR | cut -c1-1` == "S"
176         then SUID=1; else SUID=0; fi
177
178       if test $VERBOSE == 1; then echo -e "\nRemoving symlinks in $DIR (SUID=$SUID)"; fi
179       # Walk through all applications from the current destination
180       for APP in $APPLICATIONS
181         do
182           # The following code is executed for every application in the current destination
183           if test -h $DIR/$APP # Check if the app is a symbolic link
184             then
185               if test -n "`ls -l $DIR/$APP | grep busybox`" # Check if the symbolic link points to busybox
186                 then
187                   if test $SUID == 0
188                     then
189                       if test $VERBOSE == 1; then echo "Removing link: $DIR/$APP"; fi
190                       rm $DIR/$APP
191                     else
192                       if test -n "`echo $RESYM | grep $APP`"
193                         then
194                           if test $VERBOSE == 1; then echo "Restoring link: /bin/busybox -> $DIR/$APP"; fi
195                           rm $DIR/$APP
196                           ln -s /bin/busybox $DIR/$APP
197                         else
198                           if test $VERBOSE == 1; then echo "Removing link: $DIR/$APP"; fi
199                           rm $DIR/$APP
200                       fi
201                   fi
202               fi
203           fi
204       done
205     done
206 }
207
208 # Action to be performed after restoring original busybox
209 POST_UNINST() {
210     OLDFILES="busybox-power.symlinks
211       busybox.power.md5
212       busybox.original.md5"
213
214     for file in $OLDFILES
215       do
216         if test -e $INSTALLDIR/$file; then
217           rm $INSTALLDIR/$file
218         fi
219       done
220 }
221
222 ### Codepath ###
223 CHECK_ENV
224 GENERIC_CHECKS
225 case $ENVIRONMENT in
226   SDK)
227     # Check for errors before restoring BusyBox
228     if test -e /tmp/busybox-power-error
229       then DISPLAY_ERRORS; fi
230     E_SDK_UNINST
231   ;;
232   N900)
233     E_N900_CHECKS
234     E_N900_PRERM
235     # Check for errors before restoring BusyBox
236     if test -e /tmp/busybox-power-error
237       then DISPLAY_ERRORS; fi
238     E_N900_UNINST
239   ;;
240 esac
241 UNSYMLINK
242 POST_UNINST
243