#!/bin/sh
# postinst script for 66-service-rc-local-user.
#
# 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.

SERVICE1="rc-local"
USER_SERVICE1="rc-local"


case "$1" in
    configure)
	##### GLOBAL SERVICES #####
	if [ -d /run/66/scandir ] ; then
		echo "parsing/reconfiguring/enabling/starting $SERVICE1"
		66 parse $SERVICE1 && 66 reconfigure $SERVICE1 && 66 enable $SERVICE1 && 66 start $SERVICE1	
	else
		echo "parsing/reconfiguring/enabling $SERVICE1"
		66 parse $SERVICE1 && 66 reconfigure $SERVICE1 && 66 enable $SERVICE1
	fi	
		
	##### STOP HERE IF antiX BUILD DETECTED #####
	if [ "$(stat -c %d/%i /)" != "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ]; then
			echo "A chroot environment has been detected - Not proceeding with postinst"
			exit 0
	fi
	
	##### USER SERVICES #####
	getent passwd $(basename -a /home/*) | awk -F: '{print $1}' | while read user; do 
		if [ -d /run/66/scandir ] ; then
			echo "parsing/reconfiguring/enabling/starting $USER_SERVICE1"
			runuser -u $user -- sh -c "mkdir -p '$(/usr/lib/execline/bin/homeof $user)/.66/script/$USER_SERVICE1' && cat /etc/66/script/$USER_SERVICE1/README > '$(/usr/lib/execline/bin/homeof $user)/.66/script/$USER_SERVICE1/README' "
			runuser -u $user -- 66 parse $USER_SERVICE1 && runuser -u $user -- 66 reconfigure $USER_SERVICE1 && runuser -u $user -- 66 enable $USER_SERVICE1 && runuser -u $user -- 66 start $USER_SERVICE1;
			runuser -u $user -- sh -c "echo '#!/bin/sh' > $(/usr/lib/execline/bin/homeof $user)/.66/rc.local" && chmod +x "$(/usr/lib/execline/bin/homeof $user)/.66/rc.local"
		else
			echo "parsing/reconfiguring/enabling $USER_SERVICE1"
			runuser -u $user -- sh -c "mkdir -p '$(/usr/lib/execline/bin/homeof $user)/.66/script/$USER_SERVICE1' && cat /etc/66/script/$USER_SERVICE1/README > '$(/usr/lib/execline/bin/homeof $user)/.66/script/$USER_SERVICE1/README' "
			runuser -u $user -- 66 parse $USER_SERVICE1 && runuser -u $user -- 66 reconfigure $USER_SERVICE1 && runuser -u $user -- 66 enable $USER_SERVICE1;
			runuser -u $user -- sh -c "echo '#!/bin/sh' > $(/usr/lib/execline/bin/homeof $user)/.66/rc.local" && chmod +x "$(/usr/lib/execline/bin/homeof $user)/.66/rc.local"
		fi
		echo	
	done
	
	;;

    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#

exit 0
