#!/bin/sh -e

modify_init=false

# Unlike postrm, I can be sure, that runit-helper is present on
# postinst.
NAME='vdev' ENABLE='yes' ONUPGRADE='restart' /lib/runit-helper/runit-helper postinst "$@"

case "$1" in
    configure)
		if ! getent group fuse > /dev/null; then
                        addgroup --quiet --system fuse || true
                fi
                sed -i -r -e "s|^#user_allow_other.*\$|user_allow_other|" /etc/fuse.conf

		##[ -x update-initramfs -a -e /etc/initramfs-tools/initramfs.conf ] \
		##	|| return 0

		chmod -R 755 /usr/share/initramfs-tools/hooks/vdev
		chmod -R 755 /usr/share/initramfs-tools/scripts/init-top/vdev
		chmod -R 755 /usr/share/initramfs-tools/scripts/init-bottom/vdev
		chmod -R 755 /usr/share/initramfs-tools/scripts/local-block/forcelvm

                if [ -x /etc/init.d/vdev-modprobe.sh ]; then
                        update-rc.d -f vdev-modprobe.sh defaults
                        update-rc.d vdev-modprobe.sh enable S
                fi

                if [ -x /etc/init.d/vdev ]; then
                        update-rc.d -f vdev defaults
                        update-rc.d vdev enable S
                fi

                echo \
"#!/bin/sh

if [ -x /sbin/vdevd ] && [ -d /proc/1/. ]; then
    if [ \"\$(/bin/cat /proc/1/comm)\" = 'runit' ]; then
        if [ -L /etc/rcS.d/S[0-9][0-9]bootlogd ] && [ -e /etc/rcS.d/S[0-9][0-9]bootlogd ]; then
            /usr/sbin/update-rc.d bootlogd disable
        fi
    fi
fi
:
" > /etc/boot.d/vdev
                rm -f /etc/boot.d/vdev.dpkg-old

                if [ -f /etc/boot.d/vdev ]; then
                        chmod +x /etc/boot.d/vdev
                fi

		if test -x /etc/init.d/vdev-coldplug.sh; then
                        update-rc.d -f vdev-coldplug.sh defaults >/dev/null
                        update-rc.d vdev-coldplug.sh disable S >/dev/null
                        update-rc.d -f vdev-coldplug.sh remove
                        rm -f /etc/init.d/vdev-coldplug.sh
                        rm -f /etc/rcS.d/S[0-9][0-9]vdev-coldplug.sh
                        rm -f /etc/rcS.d/K[0-9][0-9]vdev-coldplug.sh
		fi

		if test -x /etc/init.d/vdev-aux.sh; then
                        update-rc.d -f vdev-aux.sh defaults >/dev/null
                        update-rc.d vdev-aux.sh disable S >/dev/null
                        update-rc.d -f vdev-aux.sh remove
                        rm -f /etc/init.d/vdev-aux.sh
                        rm -f /etc/rcS.d/S[0-9][0-9]vdev-aux.sh
                        rm -f /etc/rcS.d/K[0-9][0-9]vdev-aux.sh
		fi

                ## Suggestion: use dh_installinit in eudev as well
		if test -f /etc/init.d/eudev; then
                        chmod +x /etc/init.d/eudev
                        update-rc.d -f eudev defaults >/dev/null
                        update-rc.d eudev disable S >/dev/null
                        update-rc.d -f eudev remove
                        #rm -f /etc/init.d/eudev
                        #rm -f /etc/rcS.d/S[0-9][0-9]eudev
                        #rm -f /etc/rcS.d/K[0-9][0-9]eudev
		fi

                if [ -x /etc/runit/core-services/02-vdev.sh ]; then
                        rm -f /etc/runit/core-services/02-vdev.sh
                fi

                if [ -f /etc/vdev/actions.1/netdev.act ]; then
                        rm -f /etc/vdev/actions.1/netdev.act
                fi

                if [ -f /etc/vdev/actions.1/002-modalias.act ]; then
                        rm -f /etc/vdev/actions.1/002-modalias.act
                fi

                if [ -d /etc/sv/vdev-coldplug ]; then
                        rm -rf /etc/sv/vdev-coldplug
                fi

                if [ -L /etc/runit/runsvdir/default/vdev-coldplug ]; then
                        rm -f /etc/runit/runsvdir/default/vdev-coldplug
                fi

                rm -f /etc/vdev/actions/*.act.dpkg-old
                rm -f /etc/vdev/actions.1/*.act.dpkg-old

		if $modify_init; then
		echo "
**********************************************************
  Warning: vdev has modified your default init script.
  Please, understand that vdev wouldn't work properly
  without these modifications because it requires to
  mount /dev in tmpfs, as opposed to eudev that works
  with /dev in devtmpfs.
  It's worth to clarify that there is no need to revert
  the changes in case you decide to restore eudev. The
  script itself will detect the current device manager
  before procceding to mount /dev as required. Despite
  this, a backup of the original init has been saved at:

        /usr/share/initramfs-tool/init.bak

  so that you'll always be able to restore it if you wish.
**********************************************************
		"
		fi

		update-initramfs -u -k all
		;;

	abort-upgrade|abort-remove|abort-deconfigure)
		;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
esac

#DEBHELPER#
