systemd_units=(@_F_systemd_units@)
systemd_sockets=(@_F_systemd_sockets@)
systemd_path=(@_F_systemd_path@)

reload_tmpfiles_sysusers() {

	[[ -e /usr/bin/systemd-sysusers ]] && /usr/bin/systemd-sysusers
	[[ -e /usr/bin/systemd-tmpfiles ]] && /usr/bin/systemd-tmpfiles --create 2>/dev/null
}

handle_service()
{
	local unit sunit
	local op sop
	local i j


	for i in ${systemd_units[@]}; do
		unit=$(echo $i | cut -f 1 -d '=')
		op=$(echo $i | cut -f 2 -d '=')
		if echo $op | grep -q 'e'; then
			/usr/bin/systemctl -q enable $unit.service >/dev/null 2>&1
		fi
		if echo $op | grep -q 's'; then
			/usr/bin/systemctl -q start $unit.service >/dev/null 2>&1
		fi
		if echo $op | grep -q 'd'; then
			/usr/bin/systemctl -q disable $unit.service >/dev/null 2>&1
		fi
	done

	for j in ${systemd_sockets[@]}; do
		sunit=$(echo $j | cut -f 1 -d '=')
		sop=$(echo $j | cut -f 2 -d '=')
		if echo $sop | grep -q 'e'; then
			/usr/bin/systemctl -q enable $sunit.socket >/dev/null 2>&1
		fi
		if echo $sop | grep -q 's'; then
			/usr/bin/systemctl -q start $sunit.socket >/dev/null 2>&1
		fi
                if echo $op | grep -q 'd'; then
			/usr/bin/systemctl -q disable $unit.socket >/dev/null 2>&1
		fi
	done

        for j in ${systemd_path[@]}; do
		path=$(echo $j | cut -f 1 -d '=')
		pop=$(echo $j | cut -f 2 -d '=')
		if echo $pop | grep -q 'e'; then
			/usr/bin/systemctl -q enable $path.path >/dev/null 2>&1
		fi
		if echo $pop | grep -q 's'; then
			/usr/bin/systemctl -q start $path.path >/dev/null 2>&1
		fi
		if echo $pop | grep -q 'd'; then
			/usr/bin/systemctl -q disable $path.path >/dev/null 2>&1
		fi
	done

	reload_tmpfiles_sysusers
}

post_install() {

	handle_service
}

post_upgrade()
{

	# reload first .. stuff may be changed on upgrade
	/usr/bin/systemctl daemon-reload >/dev/null 2>&1
	reload_tmpfiles_sysusers
	handle_service
}

pre_remove()
{
	local unit sunit
	local i j

	for i in ${systemd_units[@]}; do
		unit=$(echo $i | cut -f 1 -d '=')
		/usr/bin/systemctl --no-reload -q disable $unit.service >/dev/null 2>&1
		/usr/bin/systemctl -q stop $unit.service >/dev/null 2>&1
	done

	## usually a service will take down the socket but
	## there to much strange things around..
	for j in ${systemd_sockets[@]}; do
		sunit=$(echo $j | cut -f 1 -d '=')
		/usr/bin/systemctl --no-reload -q disable $sunit.socket >/dev/null 2>&1
		/usr/bin/systemctl -q stop $sunit.socket >/dev/null 2>&1
	done

        for j in ${systemd_paths[@]}; do
		path=$(echo $j | cut -f 1 -d '=')
		/usr/bin/systemctl --no-reload -q disable $sunit.path >/dev/null 2>&1
		/usr/bin/systemctl -q stop $sunit.path >/dev/null 2>&1
	done

}

post_remove()
{
	/usr/bin/systemctl daemon-reload >/dev/null 2>&1
}

op=$1
shift
$op $*
