#!/bin/sh

fatal()
{
	printf '%s\n' "${0##*/}: $*" >&2
	exit 1
}

[ $# -eq 1 ] ||
	fatal 'usage: post_service <service>'

[ -n "${1##*/*}" ] ||
	fatal "$1: invalid service name"

[ "$RPM_INSTALL_ARG1" -ge 1 ] 2>/dev/null ||
	fatal 'RPM_INSTALL_ARG1: invalid or undefined variable'

SYSTEMCTL=systemctl
PATH=/sbin:/usr/sbin:/bin:/usr/bin

if sd_booted && "$SYSTEMCTL" --version >/dev/null 2>&1; then
	"$SYSTEMCTL" daemon-reload
	if [ "$RPM_INSTALL_ARG1" -eq 1 ]; then
		"$SYSTEMCTL" -q preset "$1.service"
	else
		"$SYSTEMCTL" try-restart "$1.service"
	fi
else
	if [ "$RPM_INSTALL_ARG1" -eq 1 ]; then
		chkconfig --add "$1"
	else
		chkconfig "$1" resetpriorities
		service "$1" condrestart
	fi
fi

exit 0
