#!/bin/sh

# Postinstall script for Network UPS Tools package

NUT_DIR="@prefix@"
prefix="@prefix@" # expanded as part of some autoconf macros below

# TODO/FIXME : Should "/var/run" be a configure variable?
# Note that "/var/run" is transient tmpfs, so upgrade has to be done during same uptime.
ACTIVE_ENUMERATOR_FMRI_FILE="/var/run/nut-driver-enumerator-fmri.prev"

# make sure the nut user exists and has correct memberships
res="`getent group @RUN_AS_GROUP@`" || res=""
if [ -z "$res" ]; then
	/usr/sbin/groupadd "@RUN_AS_GROUP@"
fi
res="`getent passwd @RUN_AS_USER@`" || res=""
if [ -z "$res" ]; then
	/usr/sbin/useradd -c "Network UPS Tools" -g "@RUN_AS_GROUP@" -G root -d "@STATEPATH@" -s /bin/false @RUN_AS_USER@
fi

res="`groups "@RUN_AS_GROUP@" | grep -w "@RUN_AS_USER@"`" || res=""
if [ -z "$res" ]; then
	/usr/sbin/usermod -g "@RUN_AS_GROUP@" -G root "@RUN_AS_USER@"
fi

# make sure that conffiles are secured and have the correct ownerships
if [ -d "@CONFPATH@" ] ; then
	chown "root:@RUN_AS_GROUP@" "@CONFPATH@"
fi
for file in nut.conf ups.conf upsd.conf upsmon.conf upsd.users upssched.conf nut-driver-enumerator.conf; do
	if [ -f "@CONFPATH@/$file" ] ; then
		chown "root:@RUN_AS_GROUP@" "@CONFPATH@/$file"
		chmod 640 "@CONFPATH@/$file"
	fi
done

# make sure that /var/run exists (for privileged processes)
if [ ! -d "@PIDPATH@" ] ; then
	mkdir -p "@PIDPATH@"
fi

# make sure that /var/run/nut exists and has the correct ownerships
if [ ! -d "@ALTPIDPATH@" ] ; then
	mkdir -p "@ALTPIDPATH@"
fi
if [ -d "@ALTPIDPATH@" ] ; then
	chown "root:@RUN_AS_GROUP@" "@ALTPIDPATH@"
	chmod 770 "@ALTPIDPATH@"
fi

# make sure that /var/state/ups exists and has the correct ownerships
if [ ! -d "@STATEPATH@" ] ; then
	mkdir -p "@STATEPATH@"
fi
if [ -d "@STATEPATH@" ] ; then
	chown "root:@RUN_AS_GROUP@" "@STATEPATH@"
	chmod 770 "@STATEPATH@"
fi

if [ -n "@auglensdir@" ] && [ -d "@auglensdir@" ] && [ -d "@datarootdir@/augeas-lenses" ] ; then
	( cd "@datarootdir@/augeas-lenses" && cp -prf ./ "@auglensdir@"/ )
fi

if test -x /usr/sbin/svcadm && test -x /usr/sbin/svccfg && test -x /usr/bin/svcs ; then
	echo "Register SMF services..."
	for S in nut-driver-enumerator nut-driver nut-server nut-monitor nut ; do
		echo "Importing NUT service manifest: $S..."
		/usr/sbin/svccfg import "@datarootdir@/solaris-smf/manifest/$S.xml"
	done
	# Enable services if the system already has a configuration (e.g. upgrade)
	if test -s "@CONFPATH@/ups.conf" ; then
		echo "Stopping NUT drivers, if any (in case of upgrade)..."
		@SBINDIR@/upsdrvsvcctl stop
		@SBINDIR@/upsdrvctl -DDDDD stop
		sleep 5
		echo "(Re-)register NUT drivers (if any)..."
		REPORT_RESTART_42=no AUTO_START=no "@NUT_LIBEXECDIR@/nut-driver-enumerator.sh" --reconfigure
		sleep 2
		echo "Enable NUT drivers (if any)..."
		# Note: we now provide two services, a daemon that keeps checking
		# the config for changes and a default one that should be refreshed
		# manually to reconfigure nut-driver instances - and is "cheaper".
		# This may still fail if the daemon instance is somehow enabled (admin)
		PREV_ACTIVE_ENUMERATOR=""
		if test -s "${ACTIVE_ENUMERATOR_FMRI_FILE}" ; then
			PREV_ACTIVE_ENUMERATOR="`head -1 "${ACTIVE_ENUMERATOR_FMRI_FILE}"`"
		fi
		[ x"nut-driver-enumerator:default" = x"${PREV_ACTIVE_ENUMERATOR}" ] && PREV_ACTIVE_ENUMERATOR=""
		for ACTIVE_ENUMERATOR in ${PREV_ACTIVE_ENUMERATOR} nut-driver-enumerator:default ; do
			/usr/sbin/svcadm enable -s ${ACTIVE_ENUMERATOR} || \
			{ /usr/sbin/svcadm clear ${ACTIVE_ENUMERATOR} 2>/dev/null ; \
			  /usr/sbin/svcadm enable -s ${ACTIVE_ENUMERATOR} ; } && break || true
		done
		@SBINDIR@/upsdrvsvcctl start
	else
		echo "NOT ENABLING nut-driver-enumerator at this time : missing or empty @CONFPATH@/ups.conf" >&2
	fi
	if test -s "@CONFPATH@/ups.conf" && test -e "@CONFPATH@/upsd.conf" && test -e "@CONFPATH@/upsd.users" ; then
		# Note on the mix of "-s" and "-e" in tests above:
		# it is a valid use-case for an admin to have just touched an
		# empty upsd.conf and so use default settings for the daemon
		echo "Enable NUT upsd data server..."
		/usr/sbin/svcadm enable -s nut-server
	else
		echo "NOT ENABLING nut-server at this time : missing at least one of : @CONFPATH@/ups.conf @CONFPATH@/upsd.conf @CONFPATH@/upsd.users" >&2
	fi
	if test -s "@CONFPATH@/upsmon.conf" ; then
		echo "Enable NUT upsmon client..."
		/usr/sbin/svcadm enable -s nut-monitor
	else
		echo "NOT ENABLING nut-monitor at this time : missing or empty @CONFPATH@/upsmon.conf" >&2
	fi
	echo "Enable NUT umbrella service..."
	/usr/sbin/svcadm enable -s nut
else
	echo "Put init script in /etc/init.d..."
	cp -pf "@NUT_DATADIR@/solaris-init/nut" /etc/init.d
	chown root:bin /etc/init.d/nut
	chmod 744 /etc/init.d/nut

	ln -s ../init.d/nut /etc/rc3.d/S90nut > /dev/null 2>&1
	ln -s ../init.d/nut /etc/rc3.d/K10nut > /dev/null 2>&1

	# Start nut services

	#echo "Starting nut services"
	#$NUT_DIR/sbin/upsdrvctl start #> /dev/null 2>&1
	#$NUT_DIR/sbin/upsd #> /dev/null 2>&1
	#$NUT_DIR/sbin/upsmon #> /dev/null 2>&1
fi
