#!/bin/sh
set -e

# We don't want these services started on first install.
# We only want them started on upgrade if they are enabled,
# and some of them are conflicting with the others, so
# build the list of enabled services, then start only the
# enabled services (all at  once)

# The first part of this is the same as the postinst generated
# by the packaging tools, but without the code to enable services

# Normally automatically added by dh_systemd_enable
if deb-systemd-helper debian-installed ptp4l.service; then
	# This will only remove masks created by d-s-h on package removal.
	deb-systemd-helper unmask ptp4l.service >/dev/null || true
fi

# Update the statefile to add new symlinks (if any), which need to be cleaned
# up on purge. Also remove old symlinks.
deb-systemd-helper update-state ptp4l.service >/dev/null || true

if deb-systemd-helper debian-installed phc2sys.service; then
	# This will only remove masks created by d-s-h on package removal.
	deb-systemd-helper unmask phc2sys.service >/dev/null || true
fi

# Update the statefile to add new symlinks (if any), which need to be cleaned
# up on purge. Also remove old symlinks.
deb-systemd-helper update-state phc2sys.service >/dev/null || true

if deb-systemd-helper debian-installed ptp4l.service; then
	# This will only remove masks created by d-s-h on package removal.
	deb-systemd-helper unmask ptp4l.service >/dev/null || true
fi

# Update the statefile to add new symlinks (if any), which need to be cleaned
# up on purge. Also remove old symlinks.
deb-systemd-helper update-state ptp4l.service >/dev/null || true

if deb-systemd-helper debian-installed phc2sys.service; then
	# This will only remove masks created by d-s-h on package removal.
	deb-systemd-helper unmask phc2sys.service >/dev/null || true
fi

# Update the statefile to add new symlinks (if any), which need to be cleaned
# up on purge. Also remove old symlinks.
deb-systemd-helper update-state phc2sys.service >/dev/null || true

if deb-systemd-helper debian-installed ptp4l.service; then
	# This will only remove masks created by d-s-h on package removal.
	deb-systemd-helper unmask ptp4l.service >/dev/null || true
fi

# Update the statefile to add new symlinks (if any), which need to be cleaned
# up on purge. Also remove old symlinks.
deb-systemd-helper update-state ptp4l.service >/dev/null || true

# End copy of normally automatically added section

# Modified standard code; only (re)start services that are enabled
if [ -d /run/systemd/system ]; then
	systemctl --system daemon-reload >/dev/null || true
    { deb-systemd-helper --quiet was-enabled ptp4l.service &&
        start=ptp4l.service ; } || true
    { deb-systemd-helper --quiet was-enabled phc2sys.service && 
        start="$start phc2sys.service" ; } || true
    case "$start" in
    *.service*)
        deb-systemd-invoke start $start >/dev/null || true ;;
    esac
fi
