#!/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 /usr/sbin/update-initramfs -a -e /etc/initramfs-tools/initramfs.conf ] \
		##	|| return 0

		/bin/chmod -R 755 /usr/share/initramfs-tools/hooks/vdev
		/bin/chmod -R 755 /usr/share/initramfs-tools/scripts/init-top/vdev
		/bin/chmod -R 755 /usr/share/initramfs-tools/scripts/init-bottom/vdev
		/bin/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
                /bin/rm -f /etc/boot.d/vdev.dpkg-old
                
                if [ -f /etc/boot.d/vdev ]; then
                        /bin/chmod +x /etc/boot.d/vdev
                fi
		
		if test -x /etc/init.d/vdev-coldplug.sh; then
                        /usr/sbin/update-rc.d -f vdev-coldplug.sh defaults >/dev/null
                        /usr/sbin/update-rc.d vdev-coldplug.sh disable S >/dev/null
                        /usr/sbin/update-rc.d -f vdev-coldplug.sh remove
                        /bin/rm -f /etc/init.d/vdev-coldplug.sh
                        /bin/rm -f /etc/rcS.d/S[0-9][0-9]vdev-coldplug.sh
                        /bin/rm -f /etc/rcS.d/K[0-9][0-9]vdev-coldplug.sh
		fi
		
		if test -x /etc/init.d/vdev-aux.sh; then
                        /usr/sbin/update-rc.d -f vdev-aux.sh defaults >/dev/null
                        /usr/sbin/update-rc.d vdev-aux.sh disable S >/dev/null
                        /usr/sbin/update-rc.d -f vdev-aux.sh remove
                        /bin/rm -f /etc/init.d/vdev-aux.sh
                        /bin/rm -f /etc/rcS.d/S[0-9][0-9]vdev-aux.sh
                        /bin/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
                        /bin/chmod +x /etc/init.d/eudev
                        /usr/sbin/update-rc.d -f eudev defaults >/dev/null
                        /usr/sbin/update-rc.d eudev disable S >/dev/null
                        /usr/sbin/update-rc.d -f eudev remove
                        #/bin/rm -f /etc/init.d/eudev
                        #/bin/rm -f /etc/rcS.d/S[0-9][0-9]eudev
                        #/bin/rm -f /etc/rcS.d/K[0-9][0-9]eudev
		fi

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

                if [ -f /etc/vdev/actions.1/netdev.act ]; then
                        /bin/rm -f /etc/vdev/actions.1/netdev.act
                fi
                
                if [ -f /etc/vdev/actions.1/002-modalias.act ]; then
                        /bin/rm -f /etc/vdev/actions.1/002-modalias.act
                fi
                
                if [ -d /etc/sv/vdev-coldplug ]; then
                        /bin/rm -rf /etc/sv/vdev-coldplug
                fi
                
                if [ -L /etc/runit/runsvdir/default/vdev-coldplug ]; then
                        /bin/rm -f /etc/runit/runsvdir/default/vdev-coldplug
                fi
                
                /bin/rm -f /etc/vdev/actions/*.act.dpkg-old
                /bin/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#
