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