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