#!/bin/sh -e

action="$1"
oldversion="$2"

. /usr/share/debconf/confmodule
db_version 2.0

umask 022

if [ "$action" != configure ]
	then
	exit 0
fi

# functions

setup_aprsc_user() {
        if ! getent passwd aprsc >/dev/null; then
        	echo "Creating user account: 'aprsc'"
                adduser --quiet --system --no-create-home --home /var/run/aprsc --shell /usr/sbin/nologin --group aprsc
        fi
}

fix_permissions() {
	chown aprsc:aprsc /opt/aprsc/logs /opt/aprsc/data
	setcap cap_net_bind_service=+eip /opt/aprsc/sbin/aprsc || true
}

setup_chroot_devices() {
	( cd /opt/aprsc/dev && cp -a /dev/urandom /dev/random /dev/null /dev/zero . )
}

apparmor_config() {
	# Reload AppArmor profile
	APP_PROFILE="/etc/apparmor.d/opt.aprsc.sbin.aprsc"
	if [ -f "$APP_PROFILE" ] && aa-status --enabled 2>/dev/null; then
		echo "Installing apparmor profile..."
		apparmor_parser -r -T -W "$APP_PROFILE" || true
	fi
}

munin_config() {
	if [ -d "/etc/munin/plugins" ]; then
		echo "Setting up munin plugin..."
		( cd /etc/munin/plugins && /opt/aprsc/sbin/aprsc_munin makelinks )
	fi
}

# List the active aprsc systemd units, instances included
systemd_units() {
	systemctl list-units --no-legend --plain --state=active 'aprsc.service' 'aprsc@*.service' 2>/dev/null | awk '{ print $1 }'
}

# Live upgrade of a running aprsc systemd unit: check the configuration
# with the new binary first, using the running instance's own command
# line options, and then ask the instance to exec the new binary with
# SIGUSR2. Clients stay connected over the upgrade. argv[0] of the
# running process is not usable for the check: after a live upgrade it
# is a path within the chroot.
systemd_liveupgrade() {
	unit="$1"
	pid=`systemctl show -p MainPID --value "$unit" 2>/dev/null || true`
	if [ -z "$pid" ] || [ "$pid" = 0 ] || [ ! -r "/proc/$pid/cmdline" ]; then
		return 0
	fi
	args=`tr '\0' ' ' < "/proc/$pid/cmdline" | cut -d ' ' -f 2-`
	if ! /opt/aprsc/sbin/aprsc $args -y > /dev/null 2>&1; then
		echo "Configuration check failed for $unit, not performing live upgrade:" >&2
		/opt/aprsc/sbin/aprsc $args -y || true
		return 0
	fi
	echo "Performing live upgrade of $unit"
	systemctl kill --signal=USR2 --kill-who=main "$unit" || true
	_liveupgraded=1
}

# Live upgrade of all running aprsc instances
systemd_liveupgrade_all() {
	_liveupgraded=0
	for unit in `systemd_units`; do
		systemd_liveupgrade "$unit"
	done
	if [ "$_liveupgraded" = 1 ]; then
		echo "Live upgrade triggered - remember to check the logs in /opt/aprsc/logs for any errors!"
	fi
}

# main

setup_aprsc_user
fix_permissions
setup_chroot_devices
apparmor_config
munin_config

# Finally, do a start or restart
if [ -z "${DPKG_ROOT:-}" ] && [ -d /run/systemd/system ]; then
	# systemd: the init script is not installed, so handle the
	# upgrade here. The service is not started or enabled on a fresh
	# install - the shipped configuration has to be edited first.
	systemctl --system daemon-reload >/dev/null || true
	if [ -n "$2" ]; then
		systemd_liveupgrade_all
	fi
elif [ -x "/etc/init.d/aprsc" ]; then
	# set up links
	update-rc.d aprsc defaults >/dev/null
	# start or upgrade
	if [ -n "$2" ]; then
		# Upgrading. If previous version is new enough, perform
		# a live upgrade.
		if dpkg --compare-versions "$2" gt "1.6.4"; then
			_dh_action=liveupgrade
		else
			_dh_action=restart
		fi
	else
		_dh_action=start
	fi
	if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
		invoke-rc.d aprsc $_dh_action || exit $?
	else
		/etc/init.d/aprsc $_dh_action || exit $?
	fi
fi

exit 0

