#!/bin/sh
# postinst script for jitsi-meet
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

case "$1" in
    configure)

        . /etc/jitsi/videobridge/config

        # loading debconf
        . /usr/share/debconf/confmodule

        # detect dpkg-reconfigure, just delete old links
        db_get jitsi-meet/jvb-hostname
        JVB_HOSTNAME_OLD=$RET
        if [ -n "$RET" ] && [ ! "$JVB_HOSTNAME_OLD" = "$JVB_HOSTNAME" ] ; then
            rm -f /etc/nginx/sites-enabled/$JVB_HOSTNAME_OLD.conf
            rm -f /etc/jitsi/meet/$JVB_HOSTNAME_OLD-config.js
        fi

        # stores the hostname so we will reuse it later, like in purge
        db_set jitsi-meet/jvb-hostname $JVB_HOSTNAME

        # nginx conf
        if [ ! -f /etc/nginx/sites-available/$JVB_HOSTNAME.conf ]; then
            cp /usr/share/doc/jitsi-meet/jitsi-meet.example /etc/nginx/sites-available/$JVB_HOSTNAME.conf
            if [ ! -f /etc/nginx/sites-enabled/$JVB_HOSTNAME.conf ]; then
                ln -s /etc/nginx/sites-available/$JVB_HOSTNAME.conf /etc/nginx/sites-enabled/$JVB_HOSTNAME.conf
            fi
            sed -i "s/jitsi-meet.example.com/$JVB_HOSTNAME/g" /etc/nginx/sites-available/$JVB_HOSTNAME.conf
        fi

        # SSL for nginx
        db_get jitsi-meet/cert-choice
        CERT_CHOICE="$RET"
        if [ "$CERT_CHOICE" = 'A certificate is available and the files are uploaded on the server' ]; then
            db_set jitsi-meet/cert-path-key "/etc/ssl/$JVB_HOSTNAME.key"
            db_input critical jitsi-meet/cert-path-key || true
            db_go
            db_get jitsi-meet/cert-path-key
            CERT_KEY="$RET"
            db_set jitsi-meet/cert-path-crt "/etc/ssl/$JVB_HOSTNAME.crt"
            db_input critical jitsi-meet/cert-path-crt || true
            db_go
            db_get jitsi-meet/cert-path-crt
            CERT_CRT="$RET"
            # replace self-signed certificate paths with user provided ones
            CERT_KEY_ESC=$(echo $CERT_KEY | sed 's/\./\\\./g')
            CERT_KEY_ESC=$(echo $CERT_KEY_ESC | sed 's/\//\\\//g')
            sed -i "s/ssl_certificate_key\ \/var\/lib\/prosody\/.*key/ssl_certificate_key\ $CERT_KEY_ESC/g" \
                /etc/nginx/sites-available/$JVB_HOSTNAME.conf
            CERT_CRT_ESC=$(echo $CERT_CRT | sed 's/\./\\\./g')
            CERT_CRT_ESC=$(echo $CERT_CRT_ESC | sed 's/\//\\\//g')
            sed -i "s/ssl_certificate\ \/var\/lib\/prosody\/.*crt/ssl_certificate\ $CERT_CRT_ESC/g" \
                /etc/nginx/sites-available/$JVB_HOSTNAME.conf
        fi

        # jitsi meet
        JITSI_MEET_CONFIG="/etc/jitsi/meet/$JVB_HOSTNAME-config.js"
        if [ ! -f $JITSI_MEET_CONFIG ]; then
            cp /usr/share/doc/jitsi-meet/config.js $JITSI_MEET_CONFIG
            sed -i "s/jitsi-meet.example.com/$JVB_HOSTNAME/g" $JITSI_MEET_CONFIG
        fi

        # and we're done with debconf
        db_stop

        invoke-rc.d nginx reload
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst 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
