#! /bin/bash

function main() {
    case "$1" in
        start )
            start-synapse &
            start-riot &
            service jicofo start
            service jitsi-videobridge2 start
            service prosody start
            service coturn start
            service coturn start
            service uuidd start
            service nginx start
            wait -n 1
            kill $(jobs -p)
            wait
        ;;
        * )
            eval "$@" 
        ;;
    esac
}

function start-synapse() {
    set -e
    rm -f /etc/matrix-synapse/conf.d/server_name.yaml 
    source /etc/default/matrix-synapse 
    cd /var/lib/matrix-synapse 
    /opt/venvs/matrix-synapse/bin/python -m synapse.app.homeserver \
        --config-path=/etc/matrix-synapse/homeserver.yaml \
        --config-path=/etc/matrix-synapse/conf.d/ \
        --generate-keys  
    exec /opt/venvs/matrix-synapse/bin/python -m synapse.app.homeserver \
        --config-path=/etc/matrix-synapse/homeserver.yaml \
        --config-path=/etc/matrix-synapse/conf.d/
}

function start-riot() {
    exec /main/exec-simpleserve -p 39988 -d /var/www/riot.dangerousdemos.net/riot
}

if [ "$0" == "$BASH_SOURCE" ]; then
    main "$@"
fi
