move (un)installation script content to postinst/prerm
[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
22 INSTBINARY_SHA1=`$EXECPWR sha1sum $EXECPWR | $EXECPWR awk '{ print $1 }'`
23 ORIGBINARY_SHA1=`$EXECPWR sha1sum /bin/busybox | $EXECPWR awk '{ print $1 }'`
24
25 # Load shared functions
26 source $INSTALLDIR/functions
27
28 # Check whether the applets file exists
29 CHECK_APPLETSFILE() {
30     if test ! -e $INSTALLDIR/applets; then
31       echo "error: cannot find list of defined applets"
32       exit 1
33     fi
34 }
35
36 # Check whether symlinks have been made before
37 CHECK_SYMLINKSFILE() {
38     if test -e $INSTALLDIR/busybox-power.symlinks; then
39       echo "error: symlinks already seem to be made?"
40       echo "  this script is not supposed to be ran twice"
41       exit 1
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 # Overwrite the installed binary with the enhanced binary
61 INSTALL() {
62     /usr/sbin/dpkg-divert --local --divert $DISTBIN /bin/busybox
63     $EXECPWR cp -f $EXECPWR /bin/busybox
64 }
65
66 # Create missing symlinks to the enhanced binary
67 SYMLINK() {
68     # Load defined BusyBox applets
69     source $INSTALLDIR/applets
70
71     # Get a list of supported applets by busybox-power
72     if test -d /tmp/busybox-power; then
73       $EXECPWR rm -Rf /tmp/busybox-power; fi
74     $EXECPWR mkdir -p /tmp/busybox-power
75     $EXECPWR --install -s /tmp/busybox-power
76     $EXECPWR ls /tmp/busybox-power/ > $INSTALLDIR/applets_supported
77     $EXECPWR rm -Rf /tmp/busybox-power
78
79     # Prepare file that will keep track of installed symlinks by busybox-power
80     echo "# Automatically generated by busybox-power. DO NOT EDIT" > $INSTALLDIR/busybox-power.symlinks
81     echo -e "\nDESTINATIONS=\"$DESTINATIONS\"" >> $INSTALLDIR/busybox-power.symlinks
82     echo -e "\n# Installed symlinks" >> $INSTALLDIR/busybox-power.symlinks
83
84     # Walk through all possible destinations
85     for DESTDIR in $DESTINATIONS; do
86       # Enable us to see all entries in $DESTINATION as variables
87       eval "APPLICATIONS=\$$DESTDIR"
88
89       # Set destination directory accordingly
90       case $DESTDIR in
91         DEST_BIN)
92           DIR="/bin"
93           ;;
94         DEST_SBIN)
95           DIR="/sbin"
96           ;;
97         DEST_USRBIN)
98           DIR="/usr/bin"
99           ;;
100         DEST_USRSBIN)
101           DIR="/usr/sbin"
102           ;;
103       esac
104
105       # Keep track of installed symlinks per destination
106       SYMLINKS="$DESTDIR=\""
107
108       ECHO_VERBOSE "\nSymlinking applets in $DIR"
109       # Walk through all applications from the current destination
110       for APP in $APPLICATIONS; do
111         # The following code is executed for all applets in the current destination
112         if test ! -e $DIR/$APP; then
113           # Check whether the applet is supported by the busybox binary
114           if `$EXECPWR grep -Fq "$APP" $INSTALLDIR/applets_supported`; then
115             ECHO_VERBOSE "Symlinking: /bin/busybox -> $DIR/$APP"
116             $EXECPWR ln -s /bin/busybox $DIR/$APP
117             SYMLINKS="$SYMLINKS $APP" 
118           fi
119         fi
120       done
121
122       # Write out installed symlinks
123       echo "$SYMLINKS\"" >> $INSTALLDIR/busybox-power.symlinks
124     done
125
126     $EXECPWR rm $INSTALLDIR/applets_supported
127 }
128
129 ### Codepath ###
130 ECHO_VERBOSE "busybox-power: verbose mode"
131 ECHO_VERBOSE "  binary: $EXECPWR"
132 ECHO_VERBOSE "  version string: `$EXECPWR | $EXECPWR head -n 1`"
133 CHECK_ENV && ECHO_VERBOSE "  environment: $ENVIRONMENT"
134
135 CHECK_STANDALONE
136 CHECK_APPLETSFILE
137 CHECK_SYMLINKSFILE
138 if test "$ENVIRONMENT" != "SDK"; then
139   CHECK_ROOT; fi
140 BACKUP
141 INSTALL
142 SYMLINK
143