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