#!/bin/sh

echo "---Hyperion ambient light preinst ---"

# search for users in system, returns first entry
FOUND_USR=`who | grep -o -m1 '^\w*\b'` || "root"

# stop running daemon before we install
if pgrep hyperiond > /dev/null 2>&1
then
	if grep -m1 systemd /proc/1/comm > /dev/null
	then
		echo "--> stop init deamon: systemd"
		# systemd
		systemctl stop hyperiond"@${FOUND_USR}" 2> /dev/null

	elif [ -e /sbin/initctl ]
	then
		echo "--> stop init deamon: upstart"
		# upstart
		initctl stop hyperiond

	else
		echo "--> stop init deamon: sysV"
		# sysV
		service hyperiond stop 2>/dev/null
	fi
fi

# In case we don't use a service kill all instances
killall hyperiond 2> /dev/null

exit 0


#$USR=hyperionIS;

#addToGroup()
##{
#	getent group $1 && adduser $USR $1;
#}

#check if user exists
#if id $USR >/dev/null 2>&1; then
#    echo "--> hyperion user exists, skip creation";
#else
	## create user
#	echo "--> Create Hyperion user";
#	adduser --system --group $USR;
#fi

# add user to groups if required
## secondary user groups that are required to access system things
#addToGroup(dialout);
#addToGroup(video);
#addToGroup(audio);
#addToGroup(systemd-journal);
# platform specific groups
#addToGroup(i2c);
#addToGroup(spi);
#addToGroup(gpio);