#!/bin/sh
# postinst script for s6-linux-initrc-services.
#
# See: dh_installdeb(1).

set -e

# Summary of how this script can be called:
#        * <postinst> 'configure' <most-recently-configured-version>
#        * <old-postinst> 'abort-upgrade' <new version>
#        * <conflictor's-postinst> 'abort-remove' 'in-favour' <package>
#          <new-version>
#        * <postinst> 'abort-remove'
#        * <deconfigured's-postinst> 'abort-deconfigure' 'in-favour'
#          <failed-install-package> <version> 'removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.


case "$1" in
    configure)
		echo "compiling 'release' database to act as fallback"
		s6-rc-compile /etc/s6-rc/compiled-release /etc/s6-rc/sv-release

	if test -e /run/s6-rc/compiled; then
		echo "making 'release' database active"
		s6-rc-update /etc/s6-rc/compiled
	fi
	
		echo "compiling 'active' database"
		s6-rc-compile /etc/s6-rc/compiled-active /etc/s6-rc/sv
		
		echo "removing symlink from old database"
	if test -e /etc/s6-rc/compiled; then
		rm -r /etc/s6-rc/compiled
	fi
		echo "creating symlink to new 'active' database"
		ln -sf /etc/s6-rc/compiled-active /etc/s6-rc/compiled
		
	if test -e /run/s6-rc/compiled; then
		echo "making new database active"
		s6-rc-update /etc/s6-rc/compiled
	fi

		chmod +x /lib/s6-rc/init.d/*
		chmod +x /lib/s6-rc/lsb/init-functions
		chmod +x /lib/s6-rc/lsb/init-functions.d/*
		
    ;;

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

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#
