#!/bin/sh
# config helper

MUID=250
MGID=250
USERNAME=mumble-server
GROUPNAME=mumble-server
COMMENT=murmur-server
LOGINSHELL=/usr/bin/false
DIR=mumble-server

echo ""
echo "This script tries to create:"
echo "user $USERNAME with user id $MUID"
echo "group $GROUPNAME with group id $MGID"
echo "If the user or group id already exists, it automatically tries higher ids."
echo ""
echo "After that it creates the needed directories with proper permissions, too."
echo ""
echo "Hit ENTER to continue (CTRL+C to cancel)"
read JUNK
echo ""

if grep -qe "^$GROUPNAME:" /etc/group; then
	echo "group $GROUPNAME already exists. Exiting..."
	exit 1
fi
if grep -qe "^$USERNAME:" /etc/passwd; then
	echo "user $USERNAME already exists. Exiting..."
	exit 1
fi

NEXTGID="$(MGID=$MGID awk -F: '{uid[$3]=1} END { for (i=ENVIRON["MGID"];i in uid;i++);print i}' /etc/group)"
echo creating group $GROUPNAME with GID $NEXTGID...
/usr/sbin/groupadd -g $NEXTGID $GROUPNAME
# if nscd is running update group cache
[ $(pidof nscd) ] && { nscd --invalidate group; }

NEXTUID="$(MUID=$MUID awk -F: '{uid[$3]=1} END { for (i=ENVIRON["MUID"];i in uid;i++);print i}' /etc/passwd)"
echo creating user $USERNAME with UID $NEXTUID...
/usr/sbin/useradd -g $GROUPNAME -u $NEXTUID -s $LOGINSHELL -d /dev/null -c $COMMENT $USERNAME
# if nscd is running update user cache
[ $(pidof nscd) ] && { nscd --invalidate passwd; }

echo creating  /var/log/$DIR /var/lib/$DIR and /var/run/$DIR
mkdir /var/log/$DIR /var/lib/$DIR /var/run/$DIR
chown -R $USERNAME:$USERGROUP /var/lib/$DIR /var/log/$DIR /var/run/$DIR
