don't set up a local diversion
[busybox-power] / debian / busybox-power.postinst
1 #!/opt/busybox-power/busybox.power 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
16 INSTALLDIR="/opt/busybox-power"
17 EXECPWR="$INSTALLDIR/busybox.power"
18 DISTBIN="/bin/busybox.distrib"
19
20 VERBOSE="0"
21 SYMLINK="1"
22
23 INSTBINARY_SHA1=`$EXECPWR sha1sum $EXECPWR | $EXECPWR awk '{ print $1 }'`
24 ORIGBINARY_SHA1=`$EXECPWR sha1sum /bin/busybox | $EXECPWR awk '{ print $1 }'`
25
26 # Load shared functions
27 source $INSTALLDIR/functions
28
29 # Check whether the applets file exists
30 CHECK_APPLETSFILE() {
31     if test ! -e $INSTALLDIR/applets; then
32       echo "error: cannot find list of defined applets"
33       exit 1
34     fi
35 }
36
37 # Check whether symlinks have been made before
38 CHECK_SYMLINKSFILE() {
39     if test -e $INSTALLDIR/busybox-power.symlinks; then
40       ECHO_VERBOSE "symlinks are already made, skipping creation"
41       SYMLINK="0"
42     fi
43 }
44
45 # Backup the original BusyBox binary
46 BACKUP() {
47     if test ! -e /bin/busybox; then
48       # Scratchbox does not ship with BusyBox by default
49       return
50     fi
51
52     if test "$INSTBINARY_SHA1" != "$ORIGBINARY_SHA1"; then
53       $EXECPWR cp -a /bin/busybox $DISTBIN
54       $EXECPWR sha1sum $DISTBIN | $EXECPWR awk '{ print $1 }' \
55         > $INSTALLDIR/busybox.distrib.sha1
56       GETBBVERSION > $INSTALLDIR/busybox.distrib.version
57     fi
58 }
59
60 # Rollback procedure for Harmattan based devices
61 ROLLBACK_HARMATTAN() {
62     echo -e "\nWarning: an error has occured! Rolling back..."
63     $EXECPWR cp -af $TMPBINBAK /bin/busybox
64     $EXECPWR cp -a $TMPHASHBAK /var/lib/aegis/refhashlist
65     echo "End of roll-back"
66     exit 1
67 }
68
69 # Overwrite the installed binary with the enhanced binary
70 # dpkg-divert is disallowed on Harmattan, do not use it there!
71 INSTALL() {
72     case $ENVIRONMENT in
73       SDK|DIABLO|FREMANTLE)
74         # Divert in postinst instead of preinst, we don't want to leave Maemo
75         # without /bin/busybox for a short period of time
76         /usr/sbin/dpkg-divert --package busybox-power --add \
77           --divert $DISTBIN /bin/busybox
78         $EXECPWR cp -f $EXECPWR /bin/busybox
79         ;;
80       HARMATTAN)
81         TMPHASHBAK=`$EXECPWR mktemp`
82         TMPBINBAK=`$EXECPWR mktemp`
83         ORIGINCHECK=`GETORIGINCHECK_STATUS`
84  
85         # Useful information for Harmattan-based devices
86         ECHO_VERBOSE "refhashlist backup: $TMPHASHBAK"
87         ECHO_VERBOSE "busybox backup: $TMPBINBAK"
88         ECHO_VERBOSE "instbinary: $INSTBINARY_SHA1"
89         ECHO_VERBOSE "origbinary: $ORIGBINARY_SHA1"
90         ECHO_VERBOSE "device mode: $DEVICEMODE"
91         ECHO_VERBOSE "origincheck: $ORIGINCHECK"
92
93         if test $ORIGINCHECK -eq 1; then
94           SETORIGINCHECK_STATUS 0; fi
95
96         $EXECPWR cp -a /bin/busybox $TMPBINBAK || exit 1
97         $EXECPWR cp -a /var/lib/aegis/refhashlist $TMPHASHBAK || exit 1
98         $EXECPWR cp -af $EXECPWR /bin/busybox || ROLLBACK_HARMATTAN
99         $EXECPWR sed -i "s/$ORIGBINARY_SHA1/$INSTBINARY_SHA1/" /var/lib/aegis/refhashlist || ROLLBACK_HARMATTAN
100         /usr/bin/accli -c tcb-sign -F /var/lib/aegis/refhashlist < /var/lib/aegis/refhashlist || ROLLBACK_HARMATTAN
101         /usr/sbin/validator-init
102
103         if test $ORIGINCHECK -eq 1; then
104           SETORIGINCHECK_STATUS 1; fi
105
106         $EXECPWR rm $TMPBINBAK
107         $EXECPWR rm $TMPHASHBAK
108         ;;
109     esac
110 }
111
112 # Create missing symlinks to the enhanced binary
113 SYMLINK() {
114     # Load defined BusyBox applets
115     source $INSTALLDIR/applets
116
117     # Get a list of supported applets by busybox-power
118     if test -d /tmp/busybox-power; then
119       $EXECPWR rm -Rf /tmp/busybox-power; fi
120     $EXECPWR mkdir -p /tmp/busybox-power
121     $EXECPWR --install -s /tmp/busybox-power
122     $EXECPWR ls /tmp/busybox-power/ > $INSTALLDIR/applets_supported
123     $EXECPWR rm -Rf /tmp/busybox-power
124
125     # Prepare file that will keep track of installed symlinks by busybox-power
126     echo "# Automatically generated by busybox-power. DO NOT EDIT" > $INSTALLDIR/busybox-power.symlinks
127     echo -e "\nDESTINATIONS=\"$DESTINATIONS\"" >> $INSTALLDIR/busybox-power.symlinks
128     echo -e "\n# Installed symlinks" >> $INSTALLDIR/busybox-power.symlinks
129
130     # Walk through all possible destinations
131     for DESTDIR in $DESTINATIONS; do
132       # Enable us to see all entries in $DESTINATION as variables
133       eval "APPLICATIONS=\$$DESTDIR"
134
135       # Set destination directory accordingly
136       case $DESTDIR in
137         DEST_BIN)
138           DIR="/bin"
139           ;;
140         DEST_SBIN)
141           DIR="/sbin"
142           ;;
143         DEST_USRBIN)
144           DIR="/usr/bin"
145           ;;
146         DEST_USRSBIN)
147           DIR="/usr/sbin"
148           ;;
149       esac
150
151       # Keep track of installed symlinks per destination
152       SYMLINKS="$DESTDIR=\""
153
154       ECHO_VERBOSE "\nSymlinking applets in $DIR"
155       # Walk through all applications from the current destination
156       for APP in $APPLICATIONS; do
157         # The following code is executed for all applets in the current destination
158         if test ! -e $DIR/$APP; then
159           # Check whether the applet is supported by the busybox binary
160           if `$EXECPWR grep -Fq "$APP" $INSTALLDIR/applets_supported`; then
161             ECHO_VERBOSE "Symlinking: /bin/busybox -> $DIR/$APP"
162             $EXECPWR ln -s /bin/busybox $DIR/$APP
163             SYMLINKS="$SYMLINKS $APP" 
164           fi
165         fi
166       done
167
168       # Write out installed symlinks
169       echo "$SYMLINKS\"" >> $INSTALLDIR/busybox-power.symlinks
170     done
171
172     $EXECPWR rm $INSTALLDIR/applets_supported
173 }
174
175 ### Codepath ###
176 ECHO_VERBOSE "busybox-power: verbose mode"
177 ECHO_VERBOSE "  binary: $EXECPWR"
178 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
179 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
180
181 CHECK_APPLETSFILE
182 CHECK_SYMLINKSFILE
183 if test "$ENVIRONMENT" != "SDK"; then
184   CHECK_ROOT
185 fi
186 BACKUP
187 INSTALL
188 if test $SYMLINK == 1; then
189   SYMLINK
190 fi
191