clean up scripts
[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: 03-11-2012 (MM-DD-YYYY)
16
17
18 INSTALLDIR="/opt/busybox-power"
19 EXECPWR="$INSTALLDIR/busybox.power"
20 VERBOSE="0"
21
22 ECHO_VERBOSE() {
23         if test $VERBOSE == 1; then 
24                 echo -e "$1"; fi
25 }
26
27 # Detect environment
28 CHECK_ENV() {
29     if test -d /scratchbox; then
30       ENVIRONMENT="SDK"
31     else
32       PROD=$($EXECPWR cat /proc/component_version | $EXECPWR grep product | $EXECPWR cut -d" " -f 6)
33       case $PROD in
34         RX-51)
35           ENVIRONMENT="N900"
36           ;;
37         *)
38           # Unsupported, use the least strict environment (SDK)
39           ENVIRONMENT="SDK"
40           ;;
41       esac
42     fi
43 }
44
45 # Environment-independent checks before continuing
46 GENERIC_CHECKS() {
47     #if test -n "`pgrep dpkg`" -o "`pgrep apt`"
48     if ! lsof /var/lib/dpkg/lock >> /dev/null; then 
49       echo "error: you're running me as a stand-alone application"
50       echo "  do not do this, I will be called automatically upon"
51       echo "  installation of busybox-power"
52       exit 1
53     fi
54
55     if test ! -e $INSTALLDIR/applets; then
56       echo "error: cannot find list of defined applets"
57       exit 1
58     fi
59
60     if test -e $INSTALLDIR/busybox-power.symlinks; then
61       echo "error: symlinks already seem to be made?"
62       echo "  this script is not supposed to be ran twice"
63       exit 1
64     fi
65 }
66
67 # Additional checks for the N900
68 E_N900_CHECKS() {
69     if test "`$EXECPWR id -u`" -ne 0; then
70       echo "error: you're not running me as root, aborting"
71       echo "  also, DO NOT run me as a stand-alone application"
72       echo "  I will be called automatically upon installation"
73       echo "  of busybox-power"
74       exit 1
75     fi
76 }
77
78 # N900-specific code executed prior to installing the enhanced binary
79 E_N900_PREINST() {
80     $EXECPWR md5sum $INSTALLDIR/busybox.power | $EXECPWR awk '{ print $1 }' \
81       > $INSTALLDIR/busybox.power.md5
82     $EXECPWR md5sum /bin/busybox | $EXECPWR awk '{ print $1 }' \
83       > $INSTALLDIR/busybox.original.md5
84
85     # Check whether busybox-power isn't installed already
86     INSTBINARY_MD5=`$EXECPWR cat $INSTALLDIR/busybox.power.md5`
87     ORIGBINARY_MD5=`$EXECPWR cat $INSTALLDIR/busybox.original.md5`
88     if test "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"; then
89       echo "warning: installed busybox binary matches the binary"
90       echo "  that is to be installed"
91       if ! test -e $INSTALLDIR/busybox.original; then 
92         $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
93     else
94       $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original
95     fi
96 }
97
98 # SDK-specific code executed prior to installing the enhanced binary
99 E_SDK_PREINST() {
100     if test -e /bin/busybox; then
101       $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
102 }
103
104 # Overwrite the installed binary with the enhanced binary
105 INSTALL() {
106     $EXECPWR cp -f $INSTALLDIR/busybox.power /bin/busybox
107 }
108
109 # Creates missing symlinks to the enhanced binary
110 SYMLINK() {
111     # Load defined BusyBox applets
112     source $INSTALLDIR/applets
113
114     # Get a list of supported applets by busybox-power
115     if test -d /tmp/busybox-power; then 
116       $EXECPWR rm -Rf /tmp/busybox-power; fi
117     $EXECPWR mkdir -p /tmp/busybox-power
118     $EXECPWR --install -s /tmp/busybox-power
119     $EXECPWR ls /tmp/busybox-power/ > $INSTALLDIR/applets_supported
120     $EXECPWR rm -Rf /tmp/busybox-power
121
122     # Prepare file that will keep track of installed symlinks by busybox-power
123     echo "# Automatically generated by busybox-power. DO NOT EDIT" > $INSTALLDIR/busybox-power.symlinks
124     echo -e "\nDESTINATIONS=\"$DESTINATIONS\"" >> $INSTALLDIR/busybox-power.symlinks
125     echo -e "\n# Installed symlinks" >> $INSTALLDIR/busybox-power.symlinks
126
127     # Walk through all possible destinations
128     for DESTDIR in $DESTINATIONS; do 
129       # Enable us to see all entries in $DESTINATION as variables
130       eval "APPLICATIONS=\$$DESTDIR"
131
132       # Set destination directory accordingly
133       case $DESTDIR in
134         DEST_BIN)
135           DIR="/bin"
136           ;;
137         DEST_SBIN)
138           DIR="/sbin"
139           ;;
140         DEST_USRBIN)
141           DIR="/usr/bin"
142           ;;
143         DEST_USRSBIN)
144           DIR="/usr/sbin"
145           ;;
146       esac
147
148       # Keep track of installed symlinks per destination
149       SYMLINKS="$DESTDIR=\""
150
151       ECHO_VERBOSE "\nSymlinking applets in $DIR"
152       # Walk through all applications from the current destination
153       for APP in $APPLICATIONS; do
154         # The following code is executed for all applets in the current destination
155         if test ! -e $DIR/$APP; then
156           # Check whether the applet is supported by the busybox binary
157           if `$EXECPWR grep -Fq "$APP" $INSTALLDIR/applets_supported`; then
158             ECHO_VERBOSE "Symlinking: /bin/busybox -> $DIR/$APP"
159             $EXECPWR ln -s /bin/busybox $DIR/$APP
160             SYMLINKS="$SYMLINKS $APP" 
161           fi
162         fi
163       done
164
165       # Write out installed symlinks
166       echo "$SYMLINKS\"" >> $INSTALLDIR/busybox-power.symlinks
167     done
168
169     $EXECPWR rm $INSTALLDIR/applets_supported
170 }
171
172 ### Codepath ###
173 ECHO_VERBOSE "busybox-power: verbose mode"
174 ECHO_VERBOSE "  binary: $EXECPWR"
175 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
176 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
177 GENERIC_CHECKS
178 case $ENVIRONMENT in
179   SDK)
180     E_SDK_PREINST
181     ;;
182   N900)
183     E_N900_CHECKS
184     E_N900_PREINST
185     ;;
186 esac
187 INSTALL
188 SYMLINK
189