#!/bin/sh -e

modify_init=false

# runit-helper
NAME='vdev' ENABLE='yes' ONUPGRADE='restart' /lib/runit-helper/runit-helper postinst "$@"

case "$1" in
    configure)
        for i in vdev vdev-modprobe.sh vdev-settle-sync.sh; do
            if [ ! -f /etc/init.d/"$i" ] && [ -f /usr/share/vdev/"$i" ]; then
                cp /usr/share/vdev/"$i" /etc/init.d/"$i"
                chmod 755 /etc/init.d/"$i"
                update-rc.d "$i" defaults >/dev/null
                update-rc.d "$i" enable S >/dev/null
            fi
        done
        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
        
        [ -f /usr/share/initramfs-tools/hooks/vdev ] && chmod 755 /usr/share/initramfs-tools/hooks/vdev
        [ -f /usr/share/initramfs-tools/scripts/init-top/vdev ] && chmod 755 /usr/share/initramfs-tools/scripts/init-top/vdev
        [ -f /usr/share/initramfs-tools/scripts/init-bottom/vdev ] && chmod 755 /usr/share/initramfs-tools/scripts/init-bottom/vdev
        [ -f /usr/share/initramfs-tools/scripts/local-block/forcelvm ] && chmod 755 /usr/share/initramfs-tools/scripts/local-block/forcelvm

        if [ -e /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 || true
            rm -f /etc/rcS.d/S[0-9][0-9]eudev
            rm -f /etc/rcS.d/K[0-9][0-9]eudev
        fi

        for script in vdev-modprobe.sh vdev vdev-settle-sync.sh; do
            if [ -f "/etc/init.d/$script" ]; then
                chmod 755 "/etc/init.d/$script"
                update-rc.d -f "$script" defaults
                update-rc.d "$script" enable S
            fi
        done

        # Remove legacy init script superseded by vdev-settle-sync.sh.  Run
        # update-rc.d even when /etc/init.d/vdev-x11-sync.sh is already gone: on
        # upgrade dpkg removes the file before postinst, and the old guard skipped
        # cleanup so rc*.d symlinks were left behind.
        if [ -e /etc/init.d/vdev-x11-sync.sh ]; then
            chmod 755 /etc/init.d/vdev-x11-sync.sh || true
            update-rc.d -f vdev-x11-sync.sh defaults || true
            update-rc.d vdev-x11-sync.sh disable S || true
            update-rc.d -f vdev-x11-sync.sh remove || true
            rm -f /etc/init.d/vdev-x11-sync.sh || true
            rm -f /etc/rcS.d/S[0-9][0-9]vdev-x11-sync.sh
            rm -f /etc/rcS.d/K[0-9][0-9]vdev-x11-sync.sh
        fi
        
        rm -f /etc/runit/core-services/02-vdev.sh
        rm -f /etc/boot.d/vdev /etc/boot.d/vdev.dpkg-old
        rm -f /lib/vdev/input-seat.sh /lib/vdev/nouveau.sh
        rm -f /etc/vdev/actions/input-seat.act /etc/vdev/actions/nouveau.act
        rm -f /etc/vdev/actions.1/input-seat.act /etc/vdev/actions.1/nouveau.act
        
        rm -f /lib/vdev/os-early-synthetic-audit.sh || true
        rm -f /lib/vdev/input-phase1-mark.sh || true
        rm -f /lib/vdev/blkid.sh || true
        rm -f /etc/vdev/actions.1/os-early-synthetic-audit.act || true
        rm -f /etc/vdev/actions/000-input-phase1-mark.act || true
        rm -f /etc/vdev/actions/blkid.act || true

        if $modify_init; then
		echo "
**********************************************************
  Warning: vdev has modified your default startup script.
  Please note that vdev would not function properly
  without these modifications, as it requires
  mounting /dev on tmpfs, unlike eudev, which works
  with /dev on devtmpfs.
  It is worth noting that it is not necessary to revert
  the changes if you decide to restore eudev. The
  script itself will detect the current device manager
  before proceeding to mount /dev as needed. Despite
  this, a backup of the startup script has been saved at:

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

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

        # This command may take a long time and fail if /boot is full
        if command -v update-initramfs >/dev/null; then
            update-initramfs -u -k all || echo "Warning: update-initramfs failed"
        fi
        ;;

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

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

#DEBHELPER#
