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