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