
# figure out what sort if init|systemctl|... we're using to
# launch daemons and services
do_systemctl=false
if which systemctl >/dev/null 2>&1
then
    # we have a systemctl executable, but it might be disabled,
    # e.g. on MX Linux or in a container with no systemd running
    systemctl whoami >/dev/null 2>&1 && do_systemctl=true
fi
do_systemd_helper=false
if $do_systemctl
then
    which deb-systemd-helper >/dev/null 2>&1 && do_systemd_helper=true
fi
do_sysv=false
if which update-rc.d >/dev/null 2>&1
then
    if which invoke-rc.d >/dev/null 2>&1
    then
	if which runlevel >/dev/null 2>&1
	then
	    if runlevel >/dev/null 2>&1
	    then
		# System-V init installed and running
		do_sysv=true
	    fi
	fi
    fi
fi

# only need to check pmcd.service, if it is here they will all
# be here ... the real service files may have already been removed
# by now, so check the "dangling" symlinks
#
if [ -L /etc/systemd/system/multi-user.target.wants/pmcd.service ]
then
    :
else
    do_systemd_helper=false
    do_systemctl=false
fi
# ditto for the System-V variant for pmlogger
if [ -f /etc/init.d/pmlogger ]
then
    :
else
    do_sysv=false
fi

systemd_units='
pcp-geolocate.service
pcp-reboot-init.service
pmproxy.service
pmlogger_farm_check.timer pmlogger_farm_check.service
pmlogger_farm.service
pmlogger_daily.timer pmlogger_daily.service
pmlogger_check.timer pmlogger_check.service
pmlogger.service
pmie_farm_check.timer pmie_farm_check.service
pmie_daily.timer pmie_daily.service
pmie_check.timer pmie_check.service
pmie.service
pmfind.timer pmfind.service
pmcd.service'

if [ "$1" = remove ]
then
    if $do_systemd_helper
    then
	for unit in $systemd_units
	do
	    deb-systemd-helper remove $unit >/dev/null
	done
    elif $do_sysv
    then
	for svc in pmproxy pmie pmlogger pmcd
	do
	    if update-rc.d $svc remove >/dev/null
	    then
		:
	    else
		echo >&2 "pcp.postrm: Warning: update-rc.d $svc remove failed"
	    fi
	done
    fi
fi

if [ "$1" = purge ]
then
    if $do_systemd_helper
    then
	for unit in $systemd_units
	do
	    deb-systemd-helper purge $unit >/dev/null
	done
    fi
fi
