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