#!/bin/bash
# Set up repos for grommunio-meet

set -e

PROGNAME="`basename $0`"

check_with_user () {
        if [ $1 -le 0 ]; then
                if [ -z "$2" ]; then
                        echo -n "Operation will destroy grommunio-meet install."
                else
                        echo -n "$2"
                fi
                echo -n " Proceed? (y/N)"
                read ANS
                case $ANS in
                y|Y)
                        # Proceeding
                        ;;
                *)
                        exit 1
                        ;;
                esac
        fi
}


# Process command line arguments

USAGE="Usage: $PROGNAME [-h]"
usage () {
        echo "$USAGE"  1>&2
        exit 2
}

usage_full () {
        echo "$USAGE"  1>&2
        echo "" 1>&2
        echo "    -f        force purge" 1>&2
        echo "    -h        This help" 1>&2
        exit 2
}

OPTIND=1
FORCE=0
while getopts fh F; do
        case $F in
	f)
		FORCE=1
		;;
        h)
                usage_full
                ;;
        \?)
                usage
                ;;
        esac
done
shift $(( OPTIND - 1 ))
if [ $# != 0 ]; 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

check_with_user $FORCE 

dpkg --purge jitsi-videobridge2 jitsi-meet-web jitsi-meet-web-config jitsi-meet-prosody jitsi-meet jitsi-meet-turnserver grommunio-meet jicofo || true
sleep 5
# Try again jicofo is a bit touchy
dpkg --purge jicofo || true
# Remove extra junk
apt-get -y autoremove

