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