#!/bin/sh

set -e

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

umask 022


#****************************************
dbus_reload() {
	if [ -f /usr/bin/dbus-send ]; then
		/usr/bin/dbus-send --system --type=method_call --dest=org.freedesktop.DBus / org.freedesktop.DBus.ReloadConfig
	fi
}

#****************************************
systemd_add() {
	service=$1
	deb-systemd-helper unmask "${service}" >/dev/null || true
	if deb-systemd-helper --quiet was-enabled "${service}"; then
		deb-systemd-helper enable "${service}" >/dev/null || true
		if [ -d /run/systemd/system ]; then
			systemctl --system daemon-reload >/dev/null || true
			deb-systemd-invoke start "${service}" >/dev/null || true
		fi
	else
		deb-systemd-helper update-state "${service}" >/dev/null || true
	fi
}

#****************************************
# configure
#****************************************
if [ "${action}" = configure ]; then

    if [ ! -s /etc/nvme/hostnqn ]; then
        stasadm hostnqn -f /etc/nvme/hostnqn
    fi
    if [ ! -s /etc/nvme/hostid ]; then
        stasadm hostid -f /etc/nvme/hostid
    fi

	dbus_reload
	/usr/sbin/modprobe nvme-tcp
	systemd_add stafd.service
	systemd_add stacd.service

fi

#DEBHELPER#

exit 0


