#!/bin/sh
#
# Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
#
# Mumble-Django is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

set -e
set -u

MUMBLE_DJANGO_INSTDIR="/usr/share/mumble-django"
MUMBLE_DJANGO_MUNIN="/etc/munin/plugins/mumble-django"
MURMUR_CONNSTR="Meta:tcp -h 127.0.0.1 -p 6502"

# Query dpkg-statoverride to find out the user MD is running as
if dpkg-statoverride --list /var/lib/mumble-django/mumble-django.db3
then
	MUMBLE_DJANGO_USER=$(dpkg-statoverride --list /var/lib/mumble-django/mumble-django.db3 | cut -f1 '-d ')
else
	MUMBLE_DJANGO_USER="www-data"
fi

echo "Welcome to mumble-django-configure!"
echo "-------------------------------------------"

if [ ! -z "$@" ]; then
	echo " This script allows you to configure various aspects of Mumble-Django."
	echo " It lets you detect new Mumble-Server instances, create new superuser"
	echo " accounts and install or remove the Munin plugin."
	echo " This script should at least be run once after installing the Mumble-"
	echo " Django package, because it will create the initial database needed"
	echo " in order to run."
	echo
	echo " Options: none - this script is meant to be run interactively. you will"
	echo " be asked a series of questions after starting the script, and it will"
	echo " act accordingly."
	exit 1
fi

if [ ! -z "${MUMBLE_DJANGO_USER}" -a "${USER}" != "root" -a "${USER}" != "${MUMBLE_DJANGO_USER}" ]; then
	echo " ERROR: If manage.py should be run as a different user, then"
	echo " $0 must be run as that user or as root."
	echo " Configuring Apache2 and Munin is only possible when running as root."
	exit 1
fi

invoke_manage() {
	if [ -z "${MUMBLE_DJANGO_USER}" -o "${USER}" = "${MUMBLE_DJANGO_USER}" ]; then
		echo "Running as $USER: manage.py $@"
		./pyweb/manage.py $@
	else
		echo "Running as ${MUMBLE_DJANGO_USER}: manage.py $@"
		su "${MUMBLE_DJANGO_USER}" -s /bin/bash -c "./pyweb/manage.py $@"
	fi
}

cd "${MUMBLE_DJANGO_INSTDIR}"

echo " What do you want to do?"
echo " > 1) Detect a new Mumble-Server instance and make it known to Mumble-Django"
echo "   2) Create a new SuperUser for Mumble-Django's web admin interface"
echo "      Note: This will be done automatically when you run 1) for the first time."
echo "   3) Drop to a Python shell."
echo "   4) Drop to a Database shell."

read ACTION

if [ -z "$ACTION" -o "$ACTION" = "1" ]; then
	echo " Mumble-Server detection"
	echo " -----------------------"
	echo " If this is the first time you run this script, you might want to probe for the"
	echo " Debian default configuration instead of entering the service string yourself."
	echo " Please choose what service string to use."
	echo " > 1) Debian default ($MURMUR_CONNSTR)"
	echo "   2) user defined"
	
	read CHOICE
	if [ -z "$CHOICE" -o "$CHOICE" = "1" ]; then
		if [ "$USER" = "root" -a -x "/etc/init.d/mumble-server" ]; then
			/usr/sbin/invoke-rc.d --quiet mumble-server start
		fi
		export MURMUR_CONNSTR
	fi
	
	invoke_manage syncdb

elif [ "$ACTION" = "2" ]; then
	invoke_manage createsuperuser

elif [ "$ACTION" = "3" ]; then
	# Not using invoke_manage here, because www-data can't start iPython and
	# running the shell as root usually isn't a problem anyway.
	./pyweb/manage.py shell
	exit 0

elif [ "$ACTION" = "4" ]; then
	invoke_manage dbshell
	exit 0
fi
echo


if [ "$USER" = "root" ]; then
	echo " Apache2"
	echo " -----------------------"
	echo " If you have changed any settings in settings.py, you should reload the Web server"
	echo " in order for the changes to take effect. Do you want to reload Apache2 now?"
	echo "   1) Yes, reload Apache2."
	echo " > 2) No, don't do anything."
	
	read ACTION
	
	if [ "$ACTION" = "1" ]; then
		/usr/sbin/invoke-rc.d apache2 reload
	fi
	echo
	
	echo " Munin"
	echo " -----------------------"
	if [ -x "/etc/init.d/munin-node" ]; then
		if [ -x "${MUMBLE_DJANGO_MUNIN}" ]; then
			echo " The Munin plugin is currently installed. Do you wish to uninstall it?"
			echo "   1) Yes, uninstall it."
			echo " > 2) No, don't do anything."
			
			read ACTION
			if [ "$ACTION" = "1" ]; then
				rm "${MUMBLE_DJANGO_MUNIN}"
				invoke-rc.d munin-node restart
			fi
		else
			echo " Mumble-Django ships with a Munin plugin, and you appear to have Munin-Node installed."
			echo " Probing now if installing the plugin is advised..."
			echo -n "      "
			./munin.py autoconf
			echo
			echo " Do you want to install the Munin plugin?"
			echo "   1) Yes, install it."
			echo " > 2) No, don't do anything."
			
			read ACTION
			if [ "$ACTION" = "1" ]; then
				ln -s "${MUMBLE_DJANGO_INSTDIR}/munin.py" "${MUMBLE_DJANGO_MUNIN}"
				invoke-rc.d munin-node restart
			fi
		fi
	else
		echo " You do not appear to have Munin-Node installed, skipping."
	fi
else
	echo " Configuring Apache2 and Munin is only possible when running as root."
fi
echo


echo "Running a few checks..."
invoke_manage checkenv

echo "Goodbye."


