-#!/bin/sh
-set -e
+#!/opt/busybox-power/busybox.power sh
+# A script to replace /bin/busybox and create missing symlinks to its applets.
+#
+# The target directories for BusyBox' applets are defined in the "applets" file.
+# This script will only create symbolic links when 1) they do not already exist
+# in the filesystem, and 2) the BusyBox binary supports the applet. A list of
+# all made symbolic links is written out to the file "busybox-power.symlinks",
+# which will be used during uninstallation of busybox-power.
+#
+# NB The BusyBox binary needs to support the install applet.
+#
+# By Dennis Groenen <tj.groenen@gmail.com>
+# GPLv3 licensed
+#
-/opt/busybox-power/busybox.power sh /opt/busybox-power/install-binary.sh
+INSTALLDIR="/opt/busybox-power"
+EXECPWR="$INSTALLDIR/busybox.power"
+DISTBIN="/bin/busybox.distrib"
+
+VERBOSE="0"
+SYMLINK="1"
+
+INSTBINARY_SHA1=`$EXECPWR sha1sum $EXECPWR | $EXECPWR awk '{ print $1 }'`
+ORIGBINARY_SHA1=`$EXECPWR sha1sum /bin/busybox | $EXECPWR awk '{ print $1 }'`
+
+# 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_VERBOSE "symlinks are already made, skipping creation"
+ SYMLINK="0"
+ fi
+}
+
+# Backup the original BusyBox binary
+BACKUP() {
+ if test ! -e /bin/busybox; then
+ # Scratchbox does not ship with BusyBox by default
+ return
+ fi
+
+ if test "$INSTBINARY_SHA1" != "$ORIGBINARY_SHA1"; then
+ $EXECPWR cp -a /bin/busybox $DISTBIN
+ $EXECPWR sha1sum $DISTBIN | $EXECPWR awk '{ print $1 }' \
+ > $INSTALLDIR/busybox.distrib.sha1
+ GETBBVERSION > $INSTALLDIR/busybox.distrib.version
+ fi
+}
+
+# Rollback procedure for Harmattan based devices
+ROLLBACK_HARMATTAN() {
+ echo -e "\nWarning: an error has occured! Rolling back..."
+ $EXECPWR cp -af $TMPBINBAK /bin/busybox
+ $EXECPWR cp -a $TMPHASHBAK /var/lib/aegis/refhashlist
+ echo "End of roll-back"
+ exit 1
+}
+
+# Overwrite the installed binary with the enhanced binary
+# dpkg-divert is disallowed on Harmattan, do not use it there!
+INSTALL() {
+ case $ENVIRONMENT in
+ SDK|DIABLO|FREMANTLE)
+ # Divert in postinst instead of preinst, we don't want to leave Maemo
+ # without /bin/busybox for a short period of time
+ /usr/sbin/dpkg-divert --package busybox-power --add \
+ --divert $DISTBIN /bin/busybox
+ $EXECPWR cp -f $EXECPWR /bin/busybox
+ ;;
+ HARMATTAN)
+ TMPHASHBAK=`$EXECPWR mktemp`
+ TMPBINBAK=`$EXECPWR mktemp`
+ ORIGINCHECK=`GETORIGINCHECK_STATUS`
+
+ # Useful information for Harmattan-based devices
+ ECHO_VERBOSE "refhashlist backup: $TMPHASHBAK"
+ ECHO_VERBOSE "busybox backup: $TMPBINBAK"
+ ECHO_VERBOSE "instbinary: $INSTBINARY_SHA1"
+ ECHO_VERBOSE "origbinary: $ORIGBINARY_SHA1"
+ ECHO_VERBOSE "device mode: $DEVICEMODE"
+ ECHO_VERBOSE "origincheck: $ORIGINCHECK"
+
+ if test $ORIGINCHECK -eq 1; then
+ SETORIGINCHECK_STATUS 0; fi
+
+ $EXECPWR cp -a /bin/busybox $TMPBINBAK || exit 1
+ $EXECPWR cp -a /var/lib/aegis/refhashlist $TMPHASHBAK || exit 1
+ $EXECPWR cp -af $EXECPWR /bin/busybox || ROLLBACK_HARMATTAN
+ $EXECPWR sed -i "s/$ORIGBINARY_SHA1/$INSTBINARY_SHA1/" /var/lib/aegis/refhashlist || ROLLBACK_HARMATTAN
+ /usr/bin/accli -c tcb-sign -F /var/lib/aegis/refhashlist < /var/lib/aegis/refhashlist || ROLLBACK_HARMATTAN
+ /usr/sbin/validator-init
+
+ if test $ORIGINCHECK -eq 1; then
+ SETORIGINCHECK_STATUS 1; fi
+
+ $EXECPWR rm $TMPBINBAK
+ $EXECPWR rm $TMPHASHBAK
+ ;;
+ esac
+}
+
+# Create missing symlinks to the enhanced binary
+SYMLINK() {
+ # Load defined BusyBox applets
+ source $INSTALLDIR/applets
+
+ # Get a list of supported applets by busybox-power
+ if test -d /tmp/busybox-power; then
+ $EXECPWR rm -Rf /tmp/busybox-power; fi
+ $EXECPWR mkdir -p /tmp/busybox-power
+ $EXECPWR --install -s /tmp/busybox-power
+ $EXECPWR ls /tmp/busybox-power/ > $INSTALLDIR/applets_supported
+ $EXECPWR rm -Rf /tmp/busybox-power
+
+ # Prepare file that will keep track of installed symlinks by busybox-power
+ echo "# Automatically generated by busybox-power. DO NOT EDIT" > $INSTALLDIR/busybox-power.symlinks
+ echo -e "\nDESTINATIONS=\"$DESTINATIONS\"" >> $INSTALLDIR/busybox-power.symlinks
+ echo -e "\n# Installed symlinks" >> $INSTALLDIR/busybox-power.symlinks
+
+ # Walk through all possible destinations
+ for DESTDIR in $DESTINATIONS; do
+ # Enable us to see all entries in $DESTINATION as variables
+ eval "APPLICATIONS=\$$DESTDIR"
+
+ # Set destination directory accordingly
+ case $DESTDIR in
+ DEST_BIN)
+ DIR="/bin"
+ ;;
+ DEST_SBIN)
+ DIR="/sbin"
+ ;;
+ DEST_USRBIN)
+ DIR="/usr/bin"
+ ;;
+ DEST_USRSBIN)
+ DIR="/usr/sbin"
+ ;;
+ esac
+
+ # Keep track of installed symlinks per destination
+ SYMLINKS="$DESTDIR=\""
+
+ ECHO_VERBOSE "\nSymlinking applets in $DIR"
+ # Walk through all applications from the current destination
+ for APP in $APPLICATIONS; do
+ # The following code is executed for all applets in the current destination
+ if test ! -e $DIR/$APP; then
+ # Check whether the applet is supported by the busybox binary
+ if `$EXECPWR grep -Fq "$APP" $INSTALLDIR/applets_supported`; then
+ ECHO_VERBOSE "Symlinking: /bin/busybox -> $DIR/$APP"
+ $EXECPWR ln -s /bin/busybox $DIR/$APP
+ SYMLINKS="$SYMLINKS $APP"
+ fi
+ fi
+ done
+
+ # Write out installed symlinks
+ echo "$SYMLINKS\"" >> $INSTALLDIR/busybox-power.symlinks
+ done
+
+ $EXECPWR rm $INSTALLDIR/applets_supported
+}
+
+### Codepath ###
+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"
+
+CHECK_APPLETSFILE
+CHECK_SYMLINKSFILE
+if test "$ENVIRONMENT" != "SDK"; then
+ CHECK_ROOT
+fi
+BACKUP
+INSTALL
+if test $SYMLINK == 1; then
+ SYMLINK
+fi