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