restructure (un)installation scripts; modularize
authorDennis Groenen <tj.groenen@gmail.com>
Fri, 24 Aug 2012 12:29:02 +0000 (14:29 +0200)
committerDennis Groenen <tj.groenen@gmail.com>
Fri, 24 Aug 2012 12:29:02 +0000 (14:29 +0200)
debian/rules
debian/scripts/functions [new file with mode: 0644]
debian/scripts/install-binary.sh
debian/scripts/uninstall-binary.sh

index fddeb41..0fffe9e 100755 (executable)
@@ -85,6 +85,7 @@ binary-arch_busybox: $(STAMPS_DIR)/build_busybox
        install -D -m644 debian/scripts/applets debian/busybox-power/opt/busybox-power/applets
        install -D -m755 debian/scripts/install-binary.sh debian/busybox-power/opt/busybox-power/install-binary.sh
        install -D -m755 debian/scripts/uninstall-binary.sh debian/busybox-power/opt/busybox-power/uninstall-binary.sh
+       install -D -m644 debian/scripts/functions debian/busybox-power/opt/busybox-power/functions
        install -D -m755 $(DIR)/busybox_unstripped debian/busybox-power/opt/busybox-power/busybox.power
 
 binary-arch_all:
diff --git a/debian/scripts/functions b/debian/scripts/functions
new file mode 100644 (file)
index 0000000..10bfb5e
--- /dev/null
@@ -0,0 +1,56 @@
+#!/bin/sh
+# This file contains functions that are used in multiple other scripts, i.e.
+# shared functions. The purpose of centralising these, is to deduplicate code
+# and increase maintainability
+#
+# By Dennis Groenen <tj.groenen@gmail.com>
+# GPLv3 licensed
+#
+# Last updated: 08-24-2012 (MM-DD-YYYY)
+# 
+
+# Verbose-aware echo
+ECHO_VERBOSE() {
+  if test $VERBOSE == 1; then 
+    echo -e "$1"; fi
+}
+
+# Detect the current environment
+CHECK_ENV() {
+    if test -d /scratchbox; then
+      ENVIRONMENT="SDK"
+    else
+      PROD=$($EXECPWR cat /proc/component_version | $EXECPWR grep product | $EXECPWR cut -d" " -f 6)
+      case $PROD in
+        RX-51)
+          ENVIRONMENT="FREMANTLE"
+          ;;
+        *)
+          # Unsupported, use the least strict environment (SDK)
+          ENVIRONMENT="SDK"
+          ;;
+      esac
+    fi
+}
+
+# Check whether I'm running standalone
+CHECK_STANDALONE() {
+    #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 when"
+      echo "  required by busybox-power"
+      exit 1
+    fi
+}
+
+# Check whether the user is root
+CHECK_ROOT() {
+    if test "`$EXECPWR 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 when required by"
+      echo "  busybox-power"
+      exit 1
+    fi
+}
index b010e08..81c5bc3 100755 (executable)
 # By Dennis Groenen <tj.groenen@gmail.com>
 # GPLv3 licensed
 #
-# Last updated: 03-11-2012 (MM-DD-YYYY)
+# Last updated: 08-24-2012 (MM-DD-YYYY)
 # 
 
 INSTALLDIR="/opt/busybox-power"
 EXECPWR="$INSTALLDIR/busybox.power"
 VERBOSE="0"
 
-ECHO_VERBOSE() {
-       if test $VERBOSE == 1; then 
-               echo -e "$1"; fi
-}
-
-# Detect environment
-CHECK_ENV() {
-    if test -d /scratchbox; then
-      ENVIRONMENT="SDK"
-    else
-      PROD=$($EXECPWR cat /proc/component_version | $EXECPWR grep product | $EXECPWR cut -d" " -f 6)
-      case $PROD in
-        RX-51)
-          ENVIRONMENT="FREMANTLE"
-          ;;
-        *)
-          # Unsupported, use the least strict environment (SDK)
-          ENVIRONMENT="SDK"
-          ;;
-      esac
-    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 "  installation of busybox-power"
-      exit 1
-    fi
+# Load shared functions
+source $INSTALLDIR/functions
 
+# Check whether the applets file exists
+CHECK_APPLETSFILE() {
     if test ! -e $INSTALLDIR/applets; then
       echo "error: cannot find list of defined applets"
       exit 1
     fi
+}
 
+# Check whether symlinks have been made before
+CHECK_SYMLINKSFILE() {
     if test -e $INSTALLDIR/busybox-power.symlinks; then
       echo "error: symlinks already seem to be made?"
       echo "  this script is not supposed to be ran twice"
@@ -64,41 +39,36 @@ GENERIC_CHECKS() {
     fi
 }
 
-# Additional checks for Fremantle
-E_FREMANTLE_CHECKS() {
-    if test "`$EXECPWR 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 installation"
-      echo "  of busybox-power"
-      exit 1
-    fi
-}
-
-# Fremantle-specific code executed prior to installing the enhanced binary
-E_FREMANTLE_PREINST() {
+# Create MD5 hashes of relevant binaries
+HASH_BINARIES() {
     $EXECPWR md5sum $INSTALLDIR/busybox.power | $EXECPWR awk '{ print $1 }' \
       > $INSTALLDIR/busybox.power.md5
     $EXECPWR md5sum /bin/busybox | $EXECPWR awk '{ print $1 }' \
       > $INSTALLDIR/busybox.original.md5
-
-    # Check whether busybox-power isn't installed already
-    INSTBINARY_MD5=`$EXECPWR cat $INSTALLDIR/busybox.power.md5`
-    ORIGBINARY_MD5=`$EXECPWR cat $INSTALLDIR/busybox.original.md5`
-    if test "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
-      echo "warning: installed busybox binary matches the binary"
-      echo "  that is to be installed"
-      if ! test -e $INSTALLDIR/busybox.original; then 
-        $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
-    else
-      $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original
-    fi
 }
 
-# SDK-specific code executed prior to installing the enhanced binary
-E_SDK_PREINST() {
-    if test -e /bin/busybox; then
-      $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
+# Backup the original BusyBox binary
+BACKUP() {
+    case $ENVIRONMENT in
+      SDK)
+        # Scratchbox does not ship with BusyBox by default
+        if test -e /bin/busybox; then
+          $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
+        ;;
+      FREMANTLE)
+        # Check whether busybox-power isn't somehow installed already
+        INSTBINARY_MD5=`$EXECPWR cat $INSTALLDIR/busybox.power.md5`
+        ORIGBINARY_MD5=`$EXECPWR cat $INSTALLDIR/busybox.original.md5`
+        if test "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
+          echo "warning: installed busybox binary matches the binary"
+          echo "  that is to be installed"
+          if ! test -e $INSTALLDIR/busybox.original; then 
+            $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
+        else
+          $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original
+        fi
+        ;;
+    esac
 }
 
 # Overwrite the installed binary with the enhanced binary
@@ -106,7 +76,7 @@ INSTALL() {
     $EXECPWR cp -f $INSTALLDIR/busybox.power /bin/busybox
 }
 
-# Creates missing symlinks to the enhanced binary
+# Create missing symlinks to the enhanced binary
 SYMLINK() {
     # Load defined BusyBox applets
     source $INSTALLDIR/applets
@@ -174,16 +144,15 @@ 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
-  SDK)
-    E_SDK_PREINST
-    ;;
-  FREMANTLE)
-    E_FREMANTLE_CHECKS
-    E_FREMANTLE_PREINST
-    ;;
-esac
+
+CHECK_STANDALONE
+CHECK_APPLETSFILE
+CHECK_SYMLINKSFILE
+if test "$ENVIRONMENT" != "SDK"; then
+  CHECK_ROOT
+  HASH_BINARIES
+fi
+BACKUP
 INSTALL
 SYMLINK
 
index b770047..7e23d8e 100755 (executable)
 # By Dennis Groenen <tj.groenen@gmail.com>
 # GPLv3 licensed
 #
-# Last updated: 03-11-2012 (MM-DD-YYYY)
+# Last updated: 08-24-2012 (MM-DD-YYYY)
 # 
 
 INSTALLDIR="/opt/busybox-power"
 EXECPWR="$INSTALLDIR/busybox.power"
 VERBOSE="0"
 
-ECHO_VERBOSE() {
-  if test $VERBOSE == 1; then 
-    echo -e "$1"; fi
-}
-
-# 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="FREMANTLE"
-          ;;
-        *)
-          # Unsupported, use the least strict environment (SDK)
-          ENVIRONMENT="SDK"
-          ;;
-      esac
-    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
     fi
 }
 
-# Additional checks for Fremantle
-E_FREMANTLE_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
-    fi
-
+# Check the (integrity) of our BusyBox backup
+CHECK_BACKUP() {
+    # Firstly, check whether the backup still exists
     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
-}
-
-# Fremantle-specific code executed prior to uninstalling the enhanced binary
-E_FREMANTLE_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
-      fi
+      return
     fi
 
+    # Secondly, check the integrity of the backup
     if test -e $INSTALLDIR/busybox.original.md5; then
       INSTBINARY_MD5=`cat $INSTALLDIR/busybox.original.md5`
       ORIGBINARY_MD5=`md5sum $INSTALLDIR/busybox.original | awk '{ print $1 }'`
@@ -91,6 +47,17 @@ E_FREMANTLE_PRERM() {
     fi
 }
 
+# Check whether /bin/busybox has been modified after bb-power's installation
+CHECK_INSTALLEDBIN() {
+    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
+      fi
+    fi
+}
+
 # Display encountered errors
 DISPLAY_ERRORS() {
     case $ENVIRONMENT in
@@ -186,13 +153,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
-  FREMANTLE)
-    E_FREMANTLE_CHECKS
-    E_FREMANTLE_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