#!/bin/bash
set -e
# This script installs a MediaServer/Worker/Miris Manager/Cache server/MediaVault using envsetup on a system

# Prerequisites
# 1. create DNS entries
#   alpha.ubicast.net
#   alpha-mm.ubicast.net
#   alpha-mon.ubicast.net
# 2. create system in panel
# 3. launcher.sh

# log
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
mkdir -p "${DIR}/log"
LOG_FILE="${DIR}/log/launcher_$(date +%F).log"
exec > >(tee -i ${LOG_FILE})

# log envsetup version
version="$( cd "${DIR}" && git log -1 --pretty='format:%H %ad' --date=iso )"
echo "Envsetup version:"
echo "$version"

# envsetup action calls
init() {
	python3 -u /root/envsetup/update_envsetup.py

	python3 -u /root/envsetup/envsetup.py 31
	python3 -u /root/envsetup/envsetup.py 32
	python3 -u /root/envsetup/envsetup.py 33
	python3 -u /root/envsetup/envsetup.py 34
	python3 -u /root/envsetup/envsetup.py 35

	python3 -u /root/envsetup/envsetup.py 11
	python3 -u /root/envsetup/envsetup.py 12
	python3 -u /root/envsetup/envsetup.py 13
	python3 -u /root/envsetup/envsetup.py 14

	python3 -u /root/envsetup/envsetup.py 21
	python3 -u /root/envsetup/envsetup.py 22
	python3 -u /root/envsetup/envsetup.py 28
}

monitor() {
	python3 -u /root/envsetup/envsetup.py 25
	python3 -u /root/envsetup/envsetup.py 26
	python3 -u /root/envsetup/envsetup.py 41
	python3 -u /root/envsetup/envsetup.py 28
}

mediaserver() {
	python3 -u /root/envsetup/envsetup.py 23
	python3 -u /root/envsetup/envsetup.py 24
	python3 -u /root/envsetup/envsetup.py 71
	python3 -u /root/envsetup/envsetup.py 25
	python3 -u /root/envsetup/envsetup.py 51
	python3 -u /root/envsetup/envsetup.py 25
	python3 -u /root/envsetup/envsetup.py 28
}

worker() {
	python3 -u /root/envsetup/envsetup.py 72
	python3 -u /root/envsetup/envsetup.py 26
}

mirismanager() {
	python3 -u /root/envsetup/envsetup.py 23
	python3 -u /root/envsetup/envsetup.py 25
	python3 -u /root/envsetup/envsetup.py 61
	python3 -u /root/envsetup/envsetup.py 62
	python3 -u /root/envsetup/envsetup.py 25
	python3 -u /root/envsetup/envsetup.py 28
}

cache() {
	python3 -u /root/envsetup/envsetup.py 81
	python3 -u /root/envsetup/envsetup.py 82
	python3 -u /root/envsetup/envsetup.py 25
	python3 -u /root/envsetup/envsetup.py 26
}

bench() {
	python3 -u /root/envsetup/envsetup.py 52
}

tests() {
	python3 -u /root/envsetup/tester.py
}

backup_server() {
	python3 -u /root/envsetup/envsetup.py 91
}

exportvm() {
	python3 -u /root/envsetup/envsetup.py 111
}

exportvm_local() {
	python3 -u /root/envsetup/envsetup.py 112
}

case "$1" in
	"ms")
		init
		monitor
		mirismanager
		mediaserver
		[[ $2 = "--skip-tests" ]] || tests
	;;

	"w")
		init
		worker
		[[ $2 = "--skip-tests" ]] || tests
	;;

	"mm")
		init
		monitor
		mirismanager
		[[ $2 = "--skip-tests" ]] || tests
	;;

	"cache")
		init
		cache
	;;

	"mv")
		init
		backup_server
	;;

	"demokit")
        init
        monitor
        mirismanager
        # install celerity-worker first so that vod test works
        python3 -u /root/envsetup/envsetup.py 72
        mediaserver
        # install netcapture
        python3 -u /root/envsetup/envsetup.py 121
        # demokit scripts: deploy videos, custom conf, etc
        python3 -u /root/envsetup/envsetup.py 131
        # deploy ssl self-signed certificates
        python3 -u /root/envsetup/envsetup.py 133
        [[ $2 = "--skip-tests" ]] || tests
	;;

    "demokit-reset")
        # purge content
        python3 -u /root/envsetup/envsetup.py 132
        # demokit scripts: deploy videos, custom conf, etc
        python3 -u /root/envsetup/envsetup.py 131
    ;;

	"b")
		init
		bench
	;;

	"exportvm")
		CONF="$2"
		exportvm
	;;

	"exportvm_local")
		VM_NAME="$2"
		exportvm_local
	;;

	*)
		echo "
Usage: $0 [arg] [--skip-tests]
ms              build a MediaServer & Miris Manager
w               build a Worker
mm              build a Miris Manager
cache           build a Cache server (munin needs manual configuration on master AND node)
mv              build a MediaVault
demokit         build a full demo kit
demokit-reset   purge all content from demokit

After usage a log file will be generated under /root/$(date +%F)_envsetup.log

From admin machine:
Usage: $0 exportvm
exportvm        [conf_file]    export VM from an hypervisor
exportvm_local  [vm name]      export VM from local vbox
"
	;;
esac
