b0e2966362aaf2452d58e2309c04cf4d96fea815
[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(_root) binary supports it. If so, it creates a symlink to /bin/busybox(_root).
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 # Version 0.41 09-10-2011 (MM-DD-YYYY)
16 # 0.1: Initial release
17 # 0.2: Use $EXECPWR to not have to rely on /bin/busybox' functions
18 #      Minor clean-ups and be quieter
19 # 0.3: Add support for multiple environments
20 #      Make use of functions in this script
21 #      Implement additional checks
22 # 0.4: Add support for symlinking against busybox_root
23 #      Update email address
24 # 0.41: Minor fix in SYMLINK(), SUID symlinking part
25
26 INSTALLDIR="/opt/busybox-power"
27 EXECPWR="$INSTALLDIR/busybox.power"
28 BUSYBOXROOT="/bin/busybox_root"
29 VERBOSE="0"
30
31 # Print extra information in verbose mode
32 if test $VERBOSE == 1; then 
33   echo "busybox-power: verbose mode" \ 
34   echo "  binary: $EXECPWR" \ 
35   echo "  version string: `$EXECPWR | $EXECPWR head -n 1`"
36 fi
37
38 # Detect environment
39 CHECK_ENV() {
40     if test -d /scratchbox
41       then
42         ENVIRONMENT="SDK"
43       else
44         PROD=$($EXECPWR cat /proc/component_version | $EXECPWR grep product | $EXECPWR cut -d" " -f 6)
45         case $PROD in
46           RX-51)
47             ENVIRONMENT="N900"
48           ;;
49           *)
50             # Unsupported, use the least strict environment (SDK)
51             ENVIRONMENT="SDK"
52           ;;
53         esac
54     fi
55
56     if test $VERBOSE == 1; then echo "  environment: $ENVIRONMENT"; fi
57 }
58
59 # Environment-independent checks before continuing
60 GENERIC_CHECKS() {
61     #if test -n "`pgrep dpkg`" -o "`pgrep apt`"
62     if ! lsof /var/lib/dpkg/lock >> /dev/null; then 
63       echo "error: you're running me as a stand-alone application"
64       echo "  do not do this, I will be called automatically upon"
65       echo "  installation of busybox-power"
66       exit 1
67     fi
68
69     if test ! -e $INSTALLDIR/functions; then
70       echo "error: cannot find list of defined functions"
71       exit 1
72     fi
73
74     if test -e $INSTALLDIR/busybox-power.symlinks; then
75       echo "error: symlinks already seem to be made?"
76       echo "  this script is not supposed to be ran twice"
77       exit 1
78     fi
79 }
80
81 # Additional checks for the N900
82 E_N900_CHECKS() {
83     if test "`$EXECPWR id -u`" -ne 0; then
84       echo "error: you're not running me as root, aborting"
85       echo "  also, DO NOT run me as a stand-alone application"
86       echo "  I will be called automatically upon installation"
87       echo "  of busybox-power"
88       exit 1
89     fi
90 }
91
92 # N900-specific code executed prior to installing the enhanced binary
93 E_N900_PREINST() {
94     md5sum $INSTALLDIR/busybox.power | $EXECPWR awk '{ print $1 }' > $INSTALLDIR/busybox.power.md5
95     md5sum /bin/busybox | $EXECPWR awk '{ print $1 }' > $INSTALLDIR/busybox.original.md5
96
97     # Check whether busybox-power isn't installed already
98     INSTBINARY_MD5=`$EXECPWR cat $INSTALLDIR/busybox.power.md5`
99     ORIGBINARY_MD5=`$EXECPWR cat $INSTALLDIR/busybox.original.md5`
100     if test "$INSTBINARY_MD5" == "$ORIGBINARY_MD5"
101       then
102         echo "warning: installed busybox binary matches the binary"
103         echo "  that is to be installed"
104         if ! test -e $INSTALLDIR/busybox.original; then 
105           $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original; fi
106       else
107         $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original
108     fi
109 }
110
111 # SDK-specific code executed prior to installing the enhanced binary
112 E_SDK_PREINST() {
113     if test -e /bin/busybox
114       then
115         $EXECPWR cp /bin/busybox $INSTALLDIR/busybox.original
116     fi
117 }
118
119 # Overwrite old busybox binary with bbpower's one
120 INSTALL() {
121     $EXECPWR cp -f $INSTALLDIR/busybox.power /bin/busybox
122 }
123
124 # Creates missing symlinks to busybox' binary
125 SYMLINK() {
126     # Load defined BusyBox functions
127     source $INSTALLDIR/functions
128
129     # Get a list of supported functions by busybox-power (including busybox_root)
130     if test -d /tmp/busybox-power; then $EXECPWR rm -Rf /tmp/busybox-power; fi
131     $EXECPWR mkdir -p /tmp/busybox-power
132     $INSTALLDIR/busybox.power --install -s /tmp/busybox-power
133     $BUSYBOXROOT --install -s /tmp/busybox-power
134     $EXECPWR ls /tmp/busybox-power/ > $INSTALLDIR/functions_supported
135     $EXECPWR rm -Rf /tmp/busybox-power
136
137     # Prepare file which keeps track of installed symlinks by busybox-power
138     echo "# Automatically generated by busybox-power. DO NOT EDIT" > $INSTALLDIR/busybox-power.symlinks
139     echo -e "\nDESTINATIONS=\"$DESTINATIONS\"" >> $INSTALLDIR/busybox-power.symlinks
140     echo -e "\n# Installed symlinks" >> $INSTALLDIR/busybox-power.symlinks
141
142     RESYM="RESYM=\""
143     # Walk through all possible destinations
144     for DESTDIR in $DESTINATIONS
145       do 
146         # Enable us to see all entries in $DESTINATION as variables
147         eval "APPLICATIONS=\$$DESTDIR"
148
149         # Set destination directory accordingly
150         case $DESTDIR in
151           *DEST_BIN)
152             DIR="/bin"
153           ;;
154           *DEST_SBIN)
155             DIR="/sbin"
156           ;;
157           *DEST_USRBIN)
158             DIR="/usr/bin"
159           ;;
160           *DEST_USRSBIN)
161             DIR="/usr/sbin"
162           ;;
163         esac
164
165       # Determine whether we have to symlink to busybox or busybox_root
166       if test `echo $DESTDIR | $EXECPWR cut -c1-1` == "S"
167         then SUID=1; else SUID=0; fi
168
169       # Keep track of installed symlinks per destination
170       SYMLINKS="$DESTDIR=\""
171
172       if test $VERBOSE == 1; then echo -e "\nSymlinking functions in $DIR (SUID=$SUID)"; fi
173       # Walk through all applications from the current destination
174       for APP in $APPLICATIONS
175         do
176           # The following code is executed for every application in the current destination
177           if test ! -e $DIR/$APP
178             then
179               # Check whether the function is supported by the busybox binary
180               if `$EXECPWR grep -Fq "$APP" $INSTALLDIR/functions_supported` 
181                 then
182                   if test $SUID == 0
183                     then
184                       if test $VERBOSE == 1; then echo "Symlinking: /bin/busybox -> $DIR/$APP"; fi
185                       $EXECPWR ln -s /bin/busybox $DIR/$APP
186                     else
187                       if test $VERBOSE == 1; then echo "Symlinking: $BUSYBOXROOT -> $DIR/$APP"; fi
188                       $EXECPWR ln -s $BUSYBOXROOT $DIR/$APP
189                   fi
190                   SYMLINKS="$SYMLINKS $APP" 
191               fi
192             else
193               if test $SUID == 1; then
194                 if test -h $DIR/$APP # Check if the app is a symbolic link
195                   then
196                     if test -n "`$EXECPWR ls -l $DIR/$APP | $EXECPWR grep busybox`" # Check if the symbolic link points to busybox
197                       then
198                         if test $VERBOSE == 1; then echo "Re-symlinking: $BUSYBOXROOT -> $DIR/$APP"; fi
199                         $EXECPWR rm $DIR/$APP
200                         $EXECPWR ln -s $BUSYBOXROOT $DIR/$APP
201                         SYMLINKS="$SYMLINKS $APP"
202                         RESYM="$RESYM $APP"
203                     fi
204                 fi
205               fi
206           fi
207       done
208
209       # Write out installed symlinks
210       echo "$SYMLINKS\"" >> $INSTALLDIR/busybox-power.symlinks
211     done
212
213     echo "$RESYM\"" >> $INSTALLDIR/busybox-power.symlinks
214     $EXECPWR rm $INSTALLDIR/functions_supported
215 }
216
217 ### Codepath ###
218 CHECK_ENV
219 GENERIC_CHECKS
220 case $ENVIRONMENT in
221   SDK)
222     E_SDK_PREINST
223   ;;
224   N900)
225     E_N900_CHECKS
226     E_N900_PREINST
227   ;;
228 esac
229 INSTALL
230 SYMLINK
231