base-files: add update-alternatives call to default_{postinst,prerm}
[openwrt/staging/yousong.git] / package / base-files / files / lib / functions.sh
index dde1ac4f4707b66d6f096320cc8b587471a1f257..42c4f6c08565999e8d5a1e58999eefa69d45a949 100755 (executable)
@@ -35,7 +35,6 @@ list_contains() {
 }
 
 config_load() {
-       [ -n "$IPKG_INSTROOT" ] && return 0
        uci_load "$@"
 }
 
@@ -164,20 +163,29 @@ insert_modules() {
 }
 
 default_prerm() {
+       local root="${IPKG_INSTROOT}"
        local name
+
        name=$(basename ${1%.*})
-       [ -f /usr/lib/opkg/info/${name}.prerm-pkg ] && . /usr/lib/opkg/info/${name}.prerm-pkg
-       for i in `cat /usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do
-               $i disable
-               $i stop
+       [ -f "$root/usr/lib/opkg/info/${name}.prerm-pkg" ] && . "$root/usr/lib/opkg/info/${name}.prerm-pkg"
+       check_update_alternatives remove "${name}"
+
+       local shell="$(which bash)"
+       for i in `cat "$root/usr/lib/opkg/info/${name}.list" | grep "^/etc/init.d/"`; do
+               if [ -n "$root" ]; then
+                       ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" disable
+               else
+                       if [ "$PKG_UPGRADE" != "1" ]; then
+                               "$i" disable
+                       fi
+                       "$i" stop || /bin/true
+               fi
        done
 }
 
-default_postinst() {
-       local root="${IPKG_INSTROOT}"
-       local pkgname="$(basename ${1%.*})"
+add_group_and_user() {
+       local pkgname="$1"
        local rusers="$(sed -ne 's/^Require-User: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)"
-       local ret=0
 
        if [ -n "$rusers" ]; then
                local tuple oIFS="$IFS"
@@ -208,25 +216,65 @@ default_postinst() {
                        unset uid gid uname gname
                done
        fi
+}
+
+check_update_alternatives() {
+       local cmd="$1"
+       local pkgname="$2"
+       local altspecs="$(sed -ne 's/^Alternatives: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)"
+       local spec
+       local oIFS
+       local shell="$(which bash)"
+
+       shell="${shell:-/bin/sh}"
+       oIFS="$IFS"; IFS=", "
+       for spec in $altspecs; do
+               $shell ${IPKG_INSTROOT}/usr/sbin/update-alternatives "$cmd" --spec "$spec"
+       done
+       IFS="$oIFS"
+}
+
+default_postinst() {
+       local root="${IPKG_INSTROOT}"
+       local pkgname="$(basename ${1%.*})"
+       local ret=0
+
+       add_group_and_user "${pkgname}"
+       check_update_alternatives update "${pkgname}"
 
        if [ -f "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ]; then
                ( . "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" )
                ret=$?
        fi
 
+       if [ -d "$root/rootfs-overlay" ]; then
+               cp -R $root/rootfs-overlay/. $root/
+               rm -fR $root/rootfs-overlay/
+       fi
+
+       if [ -z "$root" ] && grep -q -s "^/etc/uci-defaults/" "/usr/lib/opkg/info/${pkgname}.list"; then
+               . /lib/functions/system.sh
+               [ -d /tmp/.uci ] || mkdir -p /tmp/.uci
+               for i in $(sed -ne 's!^/etc/uci-defaults/!!p' "/usr/lib/opkg/info/${pkgname}.list"); do (
+                       cd /etc/uci-defaults
+                       [ -f "$i" ] && . "$i" && rm -f "$i"
+               ) done
+               uci commit
+       fi
+
        [ -n "$root" ] || rm -f /tmp/luci-indexcache 2>/dev/null
 
-       if [ "$PKG_UPGRADE" != "1" ]; then
-               local shell="$(which bash)"
-               for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
-                       if [ -n "$root" ]; then
-                               ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable
-                       else
+       local shell="$(which bash)"
+       for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
+               if [ -n "$root" ]; then
+                       ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable
+               else
+                       if [ "$PKG_UPGRADE" != "1" ]; then
                                "$i" enable
-                               "$i" start
                        fi
-               done
-       fi
+                       "$i" start
+               fi
+       done
 
        return $ret
 }
@@ -322,4 +370,4 @@ user_exists() {
        grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
 }
 
-[ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh
+. $IPKG_INSTROOT/lib/config/uci.sh