#!/bin/sh


set -e


#-------------------------------------------------------------------------


post_install()
{
	CONFIGURATOR="/usr/local/bin/host_configurator.py"

	# create portos config for this host
	$CONFIGURATOR

	if [ -x /bin/systemctl -a -f /etc/systemd/system/portos.service ] ; then
		if ! /bin/systemctl is-active portos >/dev/null ; then
			if ! /bin/systemctl restart portos ; then
				echo "Cannot start portos service!"
			fi
			sleep 5
		fi
	elif [ -x /usr/bin/service -a -f /etc/init/portos.conf ] ; then
		if ! /usr/bin/service portos status | grep -q 'start/running' ; then 
			if ! /usr/bin/service portos start ; then
				echo "Cannot start portos service!"
			fi
			sleep 5
		fi
	fi
	# reload configuration may cause containers restart
	#echo "read disk" | nc localhost 9050 >/dev/null 2>&1 &
}


#-------------------------------------------------------------------------


case "$1" in
	configure)
		post_install || true
	;;

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

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

#DEBHELPER#

exit 0
