#!/bin/sh
# postinst script for runit-service-ufw.
#
# See: dh_installdeb(1).

#set -e

# Service Names must match the frontend filenames otherwise entries will be ignored
SERVICES="
rc-local
service2
"

USER_SERVICES="
rc-local
user-service2
"

USERS="
$(getent passwd $(basename -a /home/*) | awk -F: '{print $1}')
"


case "$1" in
    configure)

	# Temorarily modifying Internal Field Separator (IFS) for POSIX shell to emulate array newline function
	default_ifs=$IFS
	IFS='
	'
	   
	# SYSTEM SERVICES
	if [ $(ps -p1 | grep -ic "runit") -eq 1 ]; then
	echo "runit is running as the system's init system - starting new services"
		for SERVICE in $SERVICES ; do 
			[ -d /usr/share/runit/sv/$SERVICE ] && \
			echo "Starting Runit System Service: $SERVICE" && \
			sv start "/usr/share/runit/sv/$SERVICE"
		done
	fi
	
	##### STOP HERE IF antiX BUILD DETECTED #####
	# Users do no exist during antiX build - therefore processing them leads to failures
	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
	for USER_SERVICE in $USER_SERVICES ; do
		for user in $USERS ; do 
		# This section deals with setting up the home dir of each user - rc-local is a fundamental skel service 
			if [ -e "$(getent passwd $user | cut -d: -f6)"/.config/service ] && [ ! -L "$(getent passwd $user | cut -d: -f6)"/.config/service ]; then
				echo "$(getent passwd $user | cut -d: -f6)/.config/service is not a symlink - backing up"
				runuser -u $user -- mv "$(getent passwd $user | cut -d: -f6)"/.config/service "$(getent passwd $user | cut -d: -f6)"/.config/service-@$(date '+%Y%m%d-%H%M%S')
			fi
			if [ -e "$(getent passwd $user | cut -d: -f6)"/.config/service-env ] && [ ! -L "$(getent passwd $user | cut -d: -f6)"/.config/service-env ]; then
				echo "$(getent passwd $user | cut -d: -f6)/.config/service-env is not a symlink - backing up"
				runuser -u $user -- mv "$(getent passwd $user | cut -d: -f6)"/.config/service-env "$(getent passwd $user | cut -d: -f6)"/.config/service-env-@$(date '+%Y%m%d-%H%M%S')
			fi
				echo "Recreating $(getent passwd $user | cut -d: -f6)/.runit & refreshing files from /etc/skel/.runit/"
				runuser -u $user -- mkdir -p "$(getent passwd $user | cut -d: -f6)"/.runit
				runuser -u $user -- cp -rf /etc/skel/.runit/* "$(getent passwd $user | cut -d: -f6)"/.runit/ 2>/dev/null
				echo "Refreshing files from /etc/skel/.config/"
				echo "Recreating $(getent passwd $user | cut -d: -f6)/.config/service & copying symlink from /etc/skel/.config/service"
				runuser -u $user -- cp -Pf /etc/skel/.config/service "$(getent passwd $user | cut -d: -f6)"/.config/ 2>/dev/null
				echo "Recreating $(getent passwd $user | cut -d: -f6)/.config/service-env & copying symlink from /etc/skel/.config/service-env"	
				runuser -u $user -- cp -Pf /etc/skel/.config/service-env "$(getent passwd $user | cut -d: -f6)"/.config/ 2>/dev/null
		# This section resumes normal runit user service setup	
			[ -d /usr/share/runit/usersv/$USER_SERVICE ] && \
			echo "Enabling Runit User Service: $USER_SERVICE for User: $user" && \
			runuser -u $user -- cp -rf "/usr/share/runit/usersv/$USER_SERVICE" "$(getent passwd $user | cut -d: -f6)/.runit/usersv/" 2>/dev/null && \
			runuser -u $user -- ln -sf "../usersv/$USER_SERVICE" "$(getent passwd $user | cut -d: -f6)/.runit/service/" 2>/dev/null && \
			[ $(ps -p1 | grep -ic "runit") -eq 1 ] && users | grep -qw "$user" && \
			echo "Staring Runit User Service: $USER_SERVICE for detected active User: $user" && \
			runuser -u $user -- sv start "$(getent passwd $user | cut -d: -f6)/.runit/service/$USER_SERVICE"
		done
		echo	
	done
	echo	
	
	
	#~ # Restoring default Internal Field Separator (IFS) for POSIX shell
	IFS=$default_ifs
		
	;;

    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
