#! /bin/bash

DIR=$HOME/murmur
SYSDIR=/usr/share/doc/mumble-server/examples

if [ $UID == 0 ] || [ $EUID == 0 ]; then
  echo "You should never run this script as root. If you want a system-wide install, see "
  echo "the documentation included with this package."
  exit 2
fi

unset SETPW
DO_KILL=0
DO_STATUS=0
DO_INITONLY=0

while getopts "sid:p:k" flag; do
  case "$flag" in
  	"s")
  		DO_STATUS=1
  		;;
  	"i")
  		DO_INITONLY=1
  		;;
  	"d")
  		DIR=$OPTARG
  		;;
  	"p")
  		SETPW=$OPTARG
  		;;
  	"k")
  		DO_KILL=1
  		;;
  	*)
  		exit 2
  		;;
  esac
done
DBUSFILE=$DIR/.dbus.sh

if [ $DO_KILL == 1 ]; then
	if pkill -U $UID -u $EUID -o -x -f "/usr/sbin/murmurd -ini $DIR/murmur.ini"; then
		echo "Termination signal sent"
	else
		echo "Murmur process not found; not terminated"
        exit 2
	fi
	exit 0
fi

if [ ! -d $DIR ]; then
  echo "Making $DIR"
  mkdir -m 0700 $DIR
fi

cd $DIR || exit 2

if [ ! -f $DIR/murmur.ini ]; then
  echo "Creating $DIR/murmur.ini"
  if [ -f $SYSDIR/murmur.ini ]; then
    cp $SYSDIR/murmur.ini $DIR
  elif [ -f $SYSDIR/murmur.ini.gz ]; then
    gzip -cd $SYSDIR/murmur.ini.gz > $DIR/murmur.ini
  else
    echo "Could not find template for murmur.ini in $SYSDIR."
    exit 2
  fi
elif [ $DO_INITONLY == 1 ]; then
  echo "$DIR/murmur.ini already exists, initialization failed."
  exit 2
fi

if [ $DO_INITONLY == 1 ]; then
  echo "Initialization done. Please edit $DIR/murmur.ini"
  exit 0
fi

if [ "X$SETPW" != "X" ]; then
	echo "Setting superuser password to \"$SETPW\""
	/usr/sbin/murmurd -ini $DIR/murmur.ini -supw $SETPW
	exit 0
fi

PID=$(pgrep -U $UID -u $EUID -o -x -f "/usr/sbin/murmurd -ini $DIR/murmur.ini")

if [ $DO_STATUS == 1 ]; then
	if [ "X$PID" != "X" ]; then
		echo "Murmur is running"
		exit 1
	else
		echo "Murmur is not running"
		exit 0
	fi
fi

if [ "X$PID" != "X" ]; then
  echo "Murmur is already running."
  exit 1
fi

DBUS_SESSION_BUS_ADDRESS=invalid:/
[ -f $DBUSFILE ] && . $DBUSFILE

if ! dbus-send --print-reply --dest=org.freedesktop.DBus --type=method_call / org.freedesktop.DBus.GetId 2> /dev/null > /dev/null; then
  echo "Launching D-Bus session"
  dbus-launch --sh-syntax > $DBUSFILE
  . $DBUSFILE
  if ! dbus-send --print-reply --dest=org.freedesktop.DBus --type=method_call / org.freedesktop.DBus.GetId 2> /dev/null > /dev/null; then
    echo "Failed to launch session DBUS, bailing out."
    exit 2
  fi
fi

echo "Starting Murmur"
exec /usr/sbin/murmurd -ini $DIR/murmur.ini
