minor tweaks in coding style and warning messages
[busybox-power] / debian / scripts / uninstall-binary.sh
index c0eecc5..9a08957 100755 (executable)
 # By Dennis Groenen <tj.groenen@gmail.com>
 # GPLv3 licensed
 #
-# Last updated: 03-11-2012 (MM-DD-YYYY)
-# 
 
 INSTALLDIR="/opt/busybox-power"
 EXECPWR="$INSTALLDIR/busybox.power"
-VERBOSE="0"
+DISTBIN="/bin/busybox.distrib"
 
-ECHO_VERBOSE() {
-  if test $VERBOSE == 1; then 
-    echo -e "$1"; fi
-}
+VERBOSE="0"
+MODIFIEDBIN="0"
 
-# Detect environment
-CHECK_ENV() {
-    if test -d /scratchbox; then
-      ENVIRONMENT="SDK"
-    else
-      PROD=$(cat /proc/component_version | grep product | cut -d" " -f 6)
-      case $PROD in
-        RX-51)
-          ENVIRONMENT="N900"
-          ;;
-        *)
-          # Unsupported, use the least strict environment (SDK)
-          ENVIRONMENT="SDK"
-          ;;
-      esac
-    fi
-}
+INSTBINARY_SHA1=`sha1sum $EXECPWR | awk '{ print $1 }'`
+if test -e $DISTBIN; then
+  ORIGBINARY_SHA1=`sha1sum $DISTBIN | awk '{ print $1 }'`; fi
 
-# Environment-independent checks before continuing
-GENERIC_CHECKS() {
-    #if test -n "`pgrep dpkg`" -o "`pgrep apt`"
-    if ! lsof /var/lib/dpkg/lock >> /dev/null; then 
-      echo "error: you're running me as a stand-alone application"
-      echo "  do not do this, I will be called automatically upon"
-      echo "  deinstallation of busybox-power"
-      exit 1
-    fi
+# Load shared functions
+source $INSTALLDIR/functions
 
+# Check whether we can load the list of created symlinks during installation
+CHECK_SYMLINKSFILE() {
     if test ! -e $INSTALLDIR/busybox-power.symlinks; then
-      echo -e "Error: cannot find the list of symlinks to be removed. No symlinks will be removed at all!\n" >> /tmp/busybox-power-error
+      echo -e "Warning: cannot find the list of symlinks to be removed. No" \
+        "symlinks will be removed at all!\n" >> /tmp/busybox-power-error
     fi
 }
 
-# Additional checks for the N900
-E_N900_CHECKS() {
-    if test "`id -u`" -ne 0; then
-      echo "error: you're not running me as root, aborting"
-      echo "  also, DO NOT run me as a stand-alone application"
-      echo "  I will be called automatically upon deinstallation"
-      echo "  of busybox-power"
-      exit 1
+# Check the (integrity) of our BusyBox backup
+CHECK_BACKUP() {
+    # Firstly, check whether the backup still exists
+    if test ! -e $DISTBIN; then
+      if test "$ENVIRONMENT" == "SDK"; then return; fi # SDK comes without BB
+      echo -e "Warning: the backup of the original BusyBox binary is missing!" \
+        "/bin/busybox will not be touched.\n" >> /tmp/busybox-power-error
+      return
     fi
 
-    if test ! -e $INSTALLDIR/busybox.original; then
-      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
-    fi
-}
-
-# N900-specific code executed prior to uninstalling the enhanced binary
-E_N900_PRERM() {
-    if test -e $INSTALLDIR/busybox.power.md5; then
-      INSTBINARY_MD5=`md5sum /bin/busybox | awk '{ print $1 }'`
-      ORIGBINARY_MD5=`cat $INSTALLDIR/busybox.power.md5`
-      if test ! "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
-        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
+    # Secondly, check the integrity of the backup
+    if test -e $INSTALLDIR/busybox.distrib.sha1; then
+      if test "`cat $INSTALLDIR/busybox.distrib.sha1`" != "$ORIGBINARY_SHA1"; then
+        if test "`cat $INSTALLDIR/busybox.distrib.version`" == "`GETBBVERSION`"; then
+          # The backup has been changed whilst busybox hasn't been upgraded
+          echo -e "Warning: the backup of the original BusyBox binary has" \
+            "been modified since installing busybox-power (invalid SHA1" \
+            "checksum). Do not continue unless you're sure that $DISTBIN" \
+            "is not corrupted.\n" >> /tmp/busybox-power-error
+        fi
       fi
+    else
+      echo -e "Warning: could not load the saved SHA1 checksum of the backup" \
+        "of the original BusyBox binary; the integrity of the backup of the" \
+        "original binary can not be guaranteed.\n" >> /tmp/busybox-power-error
     fi
+}
 
-    if test -e $INSTALLDIR/busybox.original.md5; then
-      INSTBINARY_MD5=`cat $INSTALLDIR/busybox.original.md5`
-      ORIGBINARY_MD5=`md5sum $INSTALLDIR/busybox.original | awk '{ print $1 }'`
-      if test ! "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
-        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
-      fi
-    else
-      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
+# Check whether /bin/busybox has been modified after bb-power's installation
+CHECK_INSTALLEDBIN() {
+    if test "$INSTBINARY_SHA1" != "`sha1sum /bin/busybox | awk '{ print $1 }'`"; then
+      echo -e "Warning: /bin/busybox has been modified since installing" \
+        "busybox-power (invalid SHA1 checksum). Your current /bin/busybox" \
+        "won't be touched and the diversion of /bin/busybox to $DISTBIN will" \
+        "not be removed. \n" >> /tmp/busybox-power-error
+      MODIFIEDBIN="1"
     fi
 }
 
@@ -102,8 +83,9 @@ DISPLAY_ERRORS() {
         echo "   Hit [ctrl-c] to break"
         read 
         ;;
-      N900)
-        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
+      FREMANTLE)
+        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
         echo "Please confirm the text on the screen of your device"
         maemo-confirm-text "Attention!" /tmp/busybox-power-error
         res=$?
@@ -115,16 +97,20 @@ DISPLAY_ERRORS() {
 
 # Uninstallation of the enhanced binary
 UNINSTALL() {
-    if test -e $INSTALLDIR/busybox.original; then
-      cp -f $INSTALLDIR/busybox.original /bin/busybox
+    if test $MODIFIEDBIN == 1; then
+      # /bin/busybox has been modified since installing busybox-power
+      # Leave both the file and the diversion in place
+      return
+    elif test -e $DISTBIN; then
+      cp -af $DISTBIN /bin/busybox
       if test -e /bin/busybox; then
-        rm $INSTALLDIR/busybox.original; fi
-    else
-      if test "$ENVIRONMENT" == "SDK"; then
-        # There was no /bin/busybox to begin with..
-        rm /bin/busybox
-      fi
+        rm $DISTBIN; fi
+    elif test "$ENVIRONMENT" == "SDK"; then
+      # There was no /bin/busybox to begin with..
+      rm /bin/busybox
     fi
+
+    /usr/sbin/dpkg-divert --remove /bin/busybox
 }
 
 # Remove all symlinks that the installation script has made
@@ -156,7 +142,8 @@ UNSYMLINK() {
       ECHO_VERBOSE "\nRemoving symlinks in $DIR"
       # Walk through all applications from the current destination
       for APP in $APPLICATIONS; do
-        # The following code is executed for every application in the current destination
+        # The following code is executed for every application in the current
+        # destination
         if test -h $DIR/$APP; then 
           # Good, this app is still a symbolic link ..
           if test -n "`ls -l $DIR/$APP | grep busybox`"; then
@@ -171,8 +158,8 @@ UNSYMLINK() {
 # Action to be performed after restoring original busybox
 CLEANUP() {
     OLDFILES="busybox-power.symlinks
-      busybox.power.md5
-      busybox.original.md5"
+      busybox.distrib.version
+      busybox.distrib.sha1"
 
     for file in $OLDFILES; do
       if test -e $INSTALLDIR/$file; then
@@ -186,13 +173,14 @@ ECHO_VERBOSE "busybox-power: verbose mode"
 ECHO_VERBOSE "  binary: $EXECPWR"
 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
-GENERIC_CHECKS
-case $ENVIRONMENT in
-  N900)
-    E_N900_CHECKS
-    E_N900_PRERM
-    ;;
-esac
+
+CHECK_STANDALONE
+CHECK_SYMLINKSFILE
+if test "$ENVIRONMENT" != "SDK"; then
+  CHECK_ROOT
+  CHECK_BACKUP
+  CHECK_INSTALLEDBIN
+fi
 if test -e /tmp/busybox-power-error; then
   # An error has occured during the checks
   DISPLAY_ERRORS