use the diversion target for backing up the original binary
[busybox-power] / debian / scripts / install-binary.sh
1 #!/bin/sh
2 # A script to replace /bin/busybox and create missing symlinks to its applets.
3 #
4 # The target directories for BusyBox' applets are defined in the "applets" file.
5 # This script will only create symbolic links when 1) they do not already exist 
6 # in the filesystem, and 2) the BusyBox binary supports the applet. A list of 
7 # all made symbolic links is written out to the file "busybox-power.symlinks", 
8 # which will be used during uninstallation of busybox-power.
9 #
10 # NB The BusyBox binary needs to support the install applet.
11 #
12 # By Dennis Groenen <tj.groenen@gmail.com>
13 # GPLv3 licensed
14 #
15
16 INSTALLDIR="/opt/busybox-power"
17 EXECPWR="$INSTALLDIR/busybox.power"
18 DISTBIN="/bin/busybox.distrib"
19 VERBOSE="0"
20
21 INSTBINARY_SHA1=`$EXECPWR sha1sum $EXECPWR | $EXECPWR awk '{ print $1 }'`
22 ORIGBINARY_SHA1=`$EXECPWR sha1sum /bin/busybox | $EXECPWR awk '{ print $1 }'`
23
24 # Load shared functions
25 source $INSTALLDIR/functions
26
27 # Check whether the applets file exists
28 CHECK_APPLETSFILE() {
29     if test ! -e $INSTALLDIR/applets; then
30       echo "error: cannot find list of defined applets"
31       exit 1
32     fi
33 }
34
35 # Check whether symlinks have been made before
36 CHECK_SYMLINKSFILE() {
37     if test -e $INSTALLDIR/busybox-power.symlinks; then
38       echo "error: symlinks already seem to be made?"
39       echo "  this script is not supposed to be ran twice"
40       exit 1
41     fi
42 }
43
44 # Backup the original BusyBox binary
45 BACKUP() {
46     if test ! -e /bin/busybox; then
47       # Scratchbox does not ship with BusyBox by default
48       return
49     fi
50
51     if ! test "$INSTBINARY_SHA1" == "$ORIGBINARY_SHA1"; then
52       $EXECPWR cp -a /bin/busybox $DISTBIN
53       $EXECPWR sha1sum $DISTBIN | $EXECPWR awk '{ print $1 }' \
54         > $INSTALLDIR/busybox.distrib.sha1
55     fi
56 }
57
58 # Overwrite the installed binary with the enhanced binary
59 INSTALL() {
60     /usr/sbin/dpkg-divert --local --divert $DISTBIN /bin/busybox
61     $EXECPWR cp -f $EXECPWR /bin/busybox
62 }
63
64 # Create missing symlinks to the enhanced binary
65 SYMLINK() {
66     # Load defined BusyBox applets
67     source $INSTALLDIR/applets
68
69     # Get a list of supported applets by busybox-power
70     if test -d /tmp/busybox-power; then 
71       $EXECPWR rm -Rf /tmp/busybox-power; fi
72     $EXECPWR mkdir -p /tmp/busybox-power
73     $EXECPWR --install -s /tmp/busybox-power
74     $EXECPWR ls /tmp/busybox-power/ > $INSTALLDIR/applets_supported
75     $EXECPWR rm -Rf /tmp/busybox-power
76
77     # Prepare file that will keep track of installed symlinks by busybox-power
78     echo "# Automatically generated by busybox-power. DO NOT EDIT" > $INSTALLDIR/busybox-power.symlinks
79     echo -e "\nDESTINATIONS=\"$DESTINATIONS\"" >> $INSTALLDIR/busybox-power.symlinks
80     echo -e "\n# Installed symlinks" >> $INSTALLDIR/busybox-power.symlinks
81
82     # Walk through all possible destinations
83     for DESTDIR in $DESTINATIONS; do 
84       # Enable us to see all entries in $DESTINATION as variables
85       eval "APPLICATIONS=\$$DESTDIR"
86
87       # Set destination directory accordingly
88       case $DESTDIR in
89         DEST_BIN)
90           DIR="/bin"
91           ;;
92         DEST_SBIN)
93           DIR="/sbin"
94           ;;
95         DEST_USRBIN)
96           DIR="/usr/bin"
97           ;;
98         DEST_USRSBIN)
99           DIR="/usr/sbin"
100           ;;
101       esac
102
103       # Keep track of installed symlinks per destination
104       SYMLINKS="$DESTDIR=\""
105
106       ECHO_VERBOSE "\nSymlinking applets in $DIR"
107       # Walk through all applications from the current destination
108       for APP in $APPLICATIONS; do
109         # The following code is executed for all applets in the current destination
110         if test ! -e $DIR/$APP; then
111           # Check whether the applet is supported by the busybox binary
112           if `$EXECPWR grep -Fq "$APP" $INSTALLDIR/applets_supported`; then
113             ECHO_VERBOSE "Symlinking: /bin/busybox -> $DIR/$APP"
114             $EXECPWR ln -s /bin/busybox $DIR/$APP
115             SYMLINKS="$SYMLINKS $APP" 
116           fi
117         fi
118       done
119
120       # Write out installed symlinks
121       echo "$SYMLINKS\"" >> $INSTALLDIR/busybox-power.symlinks
122     done
123
124     $EXECPWR rm $INSTALLDIR/applets_supported
125 }
126
127 ### Codepath ###
128 ECHO_VERBOSE "busybox-power: verbose mode"
129 ECHO_VERBOSE "  binary: $EXECPWR"
130 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
131 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
132
133 CHECK_STANDALONE
134 CHECK_APPLETSFILE
135 CHECK_SYMLINKSFILE
136 if test "$ENVIRONMENT" != "SDK"; then
137   CHECK_ROOT; fi
138 BACKUP
139 INSTALL
140 SYMLINK
141