#!/bin/sh -e

modify_init=false

# Drop an OpenRC service from all standard runlevels and remove its init script
# / runlevel symlinks. Used for obsolete vdev scripts and for the rival eudev
# service after a Conflicts/Replaces swap (dh-openrc only cleans the package
# being removed, and only fully on purge with older helpers).
openrc_drop_service() {
	_name="$1"
	if command -v rc-update >/dev/null 2>&1; then
		for _rl in boot default sysinit recovery off shutdown nonetwork; do
			rc-update -q del "$_name" "$_rl" 2>/dev/null || true
		done
	fi
	rm -f /etc/runlevels/*/"$_name"
	rm -f "/etc/init.d/$_name"
}

# runit-helper
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
        if [ -f /etc/fuse.conf ]; then
            sed -i -r -e "s|^#user_allow_other.*\$|user_allow_other|" /etc/fuse.conf
        fi
        
        [ -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

        # Rival device manager (eudev) leftovers after Conflicts/Replaces.
        if command -v rc-service >/dev/null 2>&1; then
            rc-service eudev stop >/dev/null 2>&1 || true
        fi
        openrc_drop_service eudev
        # Legacy name some setups still link in sysinit.
        openrc_drop_service udev

        # Always refresh OpenRC scripts from the package. Leaving stale
        # /etc/init.d/vdev around (postinst used to copy only if missing)
        # mixes old start_pre/eval with new vdevd.*.conf and breaks boots.
        for i in vdev vdev-coldplug.sh; do
            if [ -f /usr/share/vdev/"$i" ]; then
                cp /usr/share/vdev/"$i" /etc/init.d/"$i"
                chmod 755 /etc/init.d/"$i"
                if command -v rc-update >/dev/null 2>&1; then
                    case "$i" in
                        vdev-coldplug.sh)
                        rc-update add "$i" boot >/dev/null 2>&1 || true
                        ;;
                        *)
                        rc-update add "$i" sysinit >/dev/null 2>&1 || true
                        ;;
                    esac
                fi
            fi
        done

        # Drop obsolete init scripts / share copies from prior boot layouts.
        for i in vdev-modprobe.sh vdev-settle-sync.sh vdev-terminal-coldplug.sh \
                 vdev-x11-sync.sh; do
            openrc_drop_service "$i"
            rm -f "/usr/share/vdev/$i"
        done

        # Obsolete profile dirs/confs from older packages (not individual *.act —
        # discarded actions are omitted via profiles.*.actions in vdevd.globals.json).
        rm -rf /etc/vdev/actions.0 /etc/vdev/actions.1
        rm -f /etc/vdev/vdevd.0.conf /etc/vdev/vdevd.2.conf

        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#
