#!/bin/bash
# script to reconfigure grommunio-meet jitsi packages

set -e

PROGNAME="`basename $0`"

# Process command line arguments

USAGE="Usage: $PROGNAME [-fhj] [-e email] [-c cert-file] [-k key-file] [-x upload|lets-encrypt|self-signed] <hostname>"
usage () {
        echo "$USAGE"  1>&2
        exit 2
}

usage_full () {
        echo "$USAGE"  1>&2
        echo "" 1>&2
	echo "    -c        set X509 certificate file path" 1>&2
	echo "    -e        set email" 1>&2
	echo "    -f        force full web config reconfigure" 1>&2
        echo "    -h        This help" 1>&2
	echo "    -j        Set up JAAS telphony" 1>&2
	echo "    -k        set X509 key file path" 1>&2
	echo "    -x        set certificate choice" 1>&2
        exit 2
}

OPTIND=1
DB_HOSTNAME=''
DB_EMAIL=''
DB_JAAS='false'
DB_CRT_FILE=''
DB_KEY_FILE=''
DB_CERT_CHOICE='self-signed'
WEB_RECONFIG_FORCE=0
while getopts fhjc:k:e:x: F; do
        case $F in
	c)
		DB_CRT_FILE="$OPTARG"
		;;
	e)
		DEB_EMAIL="$OPTARG"
		;;
	f)
		WEB_RECONFIG_FORCE=1
		;;
        h)
                usage_full
                ;;
	j)
		DB_JAAS="true"
		;;
	k)
		DB_KEY_FILE="$OPTARG"
		;;
	x)
		DB_CERT_CHOICE="$OPTARG"
		;;
        \?)
                usage
                ;;
        esac
done
shift $(( OPTIND - 1 ))
if [ $# != 1 ]; then
        usage
fi

# bail out if we are not root
if [ "`id -un`" != "root" ] ; then
        echo 1>&2
        echo "  `basename $0`: you must be 'root' to run this command." 1>&2
        echo 1>&2
        exit 1
fi

case $PROGNAME in
	*-reconfigure)
		MEET_INSTALL_CHECK="$(dpkg-query -f '${Status}' -W 'grommunio-meet' 2>/dev/null | awk '{print $3}' || true)"
		if [ "$MEET_INSTALL_CHECK" != "installed" ]; then
			echo 1>&2
			echo "${PROGNAME}: grommunio-meet must be installed to run this." 2>&1
			echo 1>&2
		fi
		;;
esac

case $DB_CERT_CHOICE in
	self-signed|upload|lets-encrypt)
		;;
	*)
		echo "${PROGNAME}: Wrong cert choice, must be one of 'self-signed', 'lets-ncrypt' or 'upload'." 1>&2
		exit 2
		;;
esac

DB_HOSTNAME="$1"

if [ -z "$DB_HOSTNAME" ]; then
	echo "${PROGNAME}: hostname must be set"
	exit 2
fi

UPLOAD_CERT_CHOICE="I want to use my own certificate"
LE_CERT_CHOICE="Let's Encrypt certificates"
SS_CERT_CHOICE="Generate a new self-signed certificate"

case $DB_CERT_CHOICE in
	self-signed)
		CERT_CHOICE="$SS_CERT_CHOICE"
		;;
	lets-encrypt)
		CERT_CHOICE="$LE_CERT_CHOICE"
		;;
	upload)
		CERT_CHOICE="$UPLOAD_CERT_CHOICE"
		;;
esac

JVB_HOSTNAME_OLD=$(debconf-get-selections |grep ^grommunio-meet-setup.*jvb-hostname| cut -f 4)

DEBCONF="
grommunio-meet	jitsi-meet/cert-choice	select	${CERT_CHOICE}
grommunio-meet	jitsi-meet/cert-choice	seen	true
grommunio-meet-setup	jitsi-meet/cert-choice	select	${CERT_CHOICE}
grommunio-meet-setup	jitsi-meet/cert-choice	seen	true
jitsi-meet-web-config	jitsi-meet/cert-choice	select	${CERT_CHOICE}
jitsi-meet-web-config	jitsi-meet/cert-choice	seen	true
grommunio-meet	jitsi-meet/cert-path-crt	string	${DB_CRT_FILE}
grommunio-meet	jitsi-meet/cert-path-crt	seen	true
grommunio-meet-setup	jitsi-meet/cert-path-crt	string	${DB_CRT_FILE}
grommunio-meet-setup	jitsi-meet/cert-path-crt	seen	true
jitsi-meet-web-config	jitsi-meet/cert-path-crt	string	${DB_CRT_FILE}
jitsi-meet-web-config	jitsi-meet/cert-path-crt	seen	true
grommunio-meet	jitsi-meet/cert-path-key	string	${DB_KEY_FILE}
grommunio-meet	jitsi-meet/cert-path-key	seen	true
grommunio-meet-setup	jitsi-meet/cert-path-key	string	${DB_KEY_FILE}
grommunio-meet-setup	jitsi-meet/cert-path-key	seen	true
jitsi-meet-web-config	jitsi-meet/cert-path-key	string	${DB_KEY_FILE}
jitsi-meet-web-config	jitsi-meet/cert-path-key	seen	true
grommunio-meet	jitsi-meet/email	string	${DB_EMAIL}
grommunio-meet	jitsi-meet/email	seen	true
grommunio-meet-setup	jitsi-meet/email	string	${DB_EMAIL}
grommunio-meet-setup	jitsi-meet/email	seen	true
jitsi-meet-web-config	jitsi-meet/email	string	${DB_EMAIL}
jitsi-meet-web-config	jitsi-meet/email	seen	true
grommunio-meet	jitsi-meet/jaas-choice	boolean	${DB_JAAS}
grommunio-meet	jitsi-meet/jaas-choice	seen	true
grommunio-meet-setup	jitsi-meet/jaas-choice	boolean	${DB_JAAS}
grommunio-meet-setup	jitsi-meet/jaas-choice	seen	true
jitsi-meet-web-config	jitsi-meet/jaas-choice	boolean	${DB_JAAS}
jitsi-meet-web-config	jitsi-meet/jaas-choice	seen	true
grommunio-meet	jitsi-videobridge/jvb-hostname	string	${DB_HOSTNAME}
grommunio-meet	jitsi-videobridge/jvb-hostname	seen	true
grommunio-meet-setup	jitsi-videobridge/jvb-hostname	string	${DB_HOSTNAME}
grommunio-meet-setup	jitsi-videobridge/jvb-hostname	seen	true
jicofo	jitsi-videobridge/jvb-hostname	string	${DB_HOSTNAME}
jicofo	jitsi-videobridge/jvb-hostname	seen	true
jitsi-meet-prosody	jitsi-videobridge/jvb-hostname	string	${DB_HOSTNAME}
jitsi-meet-prosody	jitsi-videobridge/jvb-hostname	seen	true
jitsi-meet-turnserver	jitsi-videobridge/jvb-hostname	string	${DB_HOSTNAME}
jitsi-meet-turnserver	jitsi-videobridge/jvb-hostname	seen	true
jitsi-meet-web-config	jitsi-videobridge/jvb-hostname	string	${DB_HOSTNAME}
jitsi-meet-web-config	jitsi-videobridge/jvb-hostname	seen	true
jitsi-videobridge2	jitsi-videobridge/jvb-hostname	string	${DB_HOSTNAME}
jitsi-videobridge2	jitsi-videobridge/jvb-hostname	seen	true
jitsi-meet-prosody	jitsi-meet-prosody/jvb-hostname	string	${DB_HOSTNAME}
jitsi-meet-prosody	jitsi-meet-prosody/jvb-hostname	seen	true
jitsi-meet-turnserver	jitsi-meet-turnserver/jvb-hostname	string	${DB_HOSTNAME}
jitsi-meet-turnserver	jitsi-meet-turnserver/jvb-hostname	seen	true
jitsi-meet-web-config	jitsi-meet/jvb-hostname	string	${NOTHING}
jitsi-meet-web-config	jitsi-meet/jvb-hostname	seen	true
"
#                                                  This above is deliberate to force reconfigure etc

# Preconfigure grommunio-meet
echo "$DEBCONF" | debconf-set-selections


case $PROGNAME in
	*-reconfigure)
		# Read in functions
		[ -r /usr/share/grommunio-meet/scripts/functions.sh ] \
			&& . /usr/share/grommunio-meet/scripts/functions.sh 
		if [ $WEB_RECONFIG_FORCE -gt 0 ]; then
			rm -f /var/lib/grommunio-meet/data/jitsi-meet-web.version || true
		fi
		# Basically a full config reset
		if [ -n "$JVB_HOSTNAME_OLD" ]; then
			rm -f /etc/jitsi/videobridge/jvb.conf || true
			rm -f "/etc/jitsi/meet/${JVB_HOSTNAME_OLD}-config.js" || true
			rm -f "/etc/nginx/sites-enabled/${JVB_HOSTNAME_OLD}.conf" || true
			rm -f "/etc/nginx/sites-available/${JVB_HOSTNAME_OLD}.conf" || true
			systemctl try-reload-or-restart nginx || true
		fi
		# Clean up ucf and config files
		clean_ucf_conf
		# Reconfigure all jitsi packages
		dpkg-reconfigure -f noninteractive jitsi-videobridge2 jitsi-meet jitsi-meet-web jitsi-meet-web-config jitsi-meet-turnserver jitsi-meet-prosody
		dpkg-reconfigure -f noninteractive grommunio-meet
		;;
	*-install)
		/usr/sbin/grommunio-meet-repos -f || true
		apt-get update
		apt-get install -y grommunio-meet
		;;
esac

