#!/bin/bash

## Config
#

# git
git_url="https://gitlab.cvh-server.de/aco/"

## Mumble
#
mumble_dir=build-mumble-web
mumble_url="$git_url$mumble_dir".git

## Jitsi
#
jitsi_dir=jitsi-auto-config
jitsi_url="$git_url$jitsi_dir".git


## letsencrypt
#
le_dir=letsencrypt-webserver
le_url="$git_url$le_dir".git

## noVNC
#
novnc_dir=novnc
novnc_url="$git_url$novnc_dir".git


# Remove all existing repositories
# and configurations.
function remove_all() {
    echo "Removing all previous configs..."
    git clean -fd
    git pull
    rm -rf $mumble_dir $jitsi_dir $le_dir $novnc_dir
    echo "Removed all repositories."
}


# Clone repositories
function clone_repositories() {
    git clone $mumble_url
    git clone $jitsi_url
    git clone $le_url
    git clone $novnc_url
}

## Configure domains and letsencrypt
#
domain=armin-co.de
sub_domains=mumble,mumble-web,jitsi
mail=your-webmaster-mail@somwhere.org
function configure_letsencrypt() {
    echo ""
    echo "Setting your letsencrypt domain settings."
    echo ""
    le_compose=$le_dir/docker-compose.yml
    sed -i "s/your-domain.com/$domain/" $le_compose
    sed -i "s/your_subdomains/$sub_domains/" $le_compose
    sed -i "s/your_email/$mail/" $le_compose
    # to use not only subdomains disable next line
    # sed -i "s/- ONLY_SUBDOMAINS=true/- ONLY_SUBDOMAINS=false/" $le_dir

    # start once to generate directories and certificates
    docker-compose -f docker-compose.yml -f $le_compose up -d && docker logs reverse-proxy && docker-compose -f $le_compose down
    echo ""
    echo "Done configuring LetsEncrypt and generating certificates."
}


## Configure Murmur (mumble server) and mumble-web
#
function configure_mumble() {
    cd build-mumble-web
    git clone https://github.com/Johni0702/mumble-web.git
    cd ..
    mkdir murmur
    cp $mumble_dir/murmur/mumble-server.ini murmur/mumble-server.ini
    touch murmur/mumble-server.log
}


## Run configuration
#
echo ""
echo "Starting configuration with cleaning up and pulling again."
echo ""

./stop_containers.sh
remove_all
clone_repositories

# configure letsencrypt
configure_letsencrypt

# configure mumble
configure_mumble


./stop_containers.sh

## pull and update all images
#
./update_containers.sh