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