#!/bin/sh
# postrm script for xivocc-installer
#
# see: dh_installdeb(1)

set -e

COMPOSE_PATH="/etc/docker/compose"
ENV_FILE=".env"

OLD_XIVOCC_CONFIG_SCRIPT="/usr/bin/configure-xivo"

check_if_xivo_reachable(){
    if ping -c 1 "${XIVO_HOST}" > /dev/null 2>&1
    then
      return 0
    else
      return 1
    fi
}

remove_docker_files() {
    rm -f ${COMPOSE_PATH}/${ENV_FILE}*
}

remove_ssl_keys() {
    rm -f /etc/docker/nginx/ssl/xivoxc.crt
    rm -f /etc/docker/nginx/ssl/xivoxc.csr
    rm -f /etc/docker/nginx/ssl/xivoxc.key
}

remove_xivocc_config_from_xivo() {
    ssh "root@${XIVO_HOST}" "cd /tmp && sudo -u asterisk psql asterisk -c \"DELETE FROM accesswebservice where name='xivows'\""
    ssh "root@${XIVO_HOST}" "rm -f /etc/asterisk/manager.d/02-xivocc.conf"
    ssh "root@${XIVO_HOST}" 'asterisk -rx "manager reload" && asterisk -rx "module reload"'
    ssh "root@${XIVO_HOST}" "apt-get purge xivocc-docker-components"
    ssh "root@${XIVO_HOST}" "rm -f /var/lib/xivo/xc_enabled"
    if ssh "root@${XIVO_HOST}" stat $OLD_XIVOCC_CONFIG_SCRIPT \> /dev/null 2\>\&1
    then
	    # shellcheck disable=SC2029
        ssh "root@${XIVO_HOST}" "rm -f $OLD_XIVOCC_CONFIG_SCRIPT"
    fi
    whiptail --title "Restarting XiVO Services" --msgbox "The XiVO services will be restarted now." 8 78
    ssh "root@${XIVO_HOST}" "xivo-dcomp up -d --remove-orphans"
}

remove_generated_logrotate() {
    rm -f /etc/logrotate.d/docker-container
}

ask_remove_xivocc_config_from_xivo(){
    if (whiptail --title "Remove XiVOcc config from XiVO PBX" --yesno "Would you like to remove XiVOcc config from XiVO PBX ?" 8 78); then
        remove_xivocc_config_from_xivo
    else
        echo "Please remove XiVOcc configuration from XiVO PBX manually"
    fi
}

case "$1" in
    purge)
        # Extract XiVO PBX ip address from env file
        if [ -f ${COMPOSE_PATH}/${ENV_FILE} ]; then
            XIVO_HOST=$(grep -oP -m 1 '^\s*XIVO_HOST=\K.*' ${COMPOSE_PATH}/${ENV_FILE})
        else
            whiptail --title "IP address Not found" --msgbox "Please enter the XiVO PBX IP address." 8 50
            XIVO_HOST=$(whiptail --inputbox "Enter the XiVO PBX IP address: " 8 50 3>&1 1>&2 2>&3)
        fi
        if ! check_if_xivo_reachable ;
            then
                whiptail --title "XiVO unreachable" --msgbox "Please check your network setup and re-run the installation" 8 50
                exit 1
        fi

        # Remove XiVocc configuration from XiVO PBX
        ask_remove_xivocc_config_from_xivo

        # Remove docker compose file and template
        remove_docker_files

        # Remove Nginx keys
        remove_ssl_keys

        remove_generated_logrotate

        # Comment-out alias from ~/.bashrc
        sed -e '/alias dcomp=/ s/^#*/#/' -i ~/.bashrc
    ;;

    remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)

    ;;
    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
