#!/bin/bash

standard=$1

help1="
You have to specify which webUi should be set to be standard and accessible on port 80.
"

help2="
The script takes only one argument which can be either 'new' or 'old'.
It will then set the standard webUi to new, or old respectively.
"

function testForConfigJson {
if [ ! -e /data/webui_config.json ];then
cat >/data/webui_config.json <<EOF
{
    "setFromOmgSetWebui": true,
    "port": 80,
    "https": false,
    "frontendDir": "webApp/bin",
    "logLevel": "warn"
}
EOF
fi
}

function showFullHelp {
    echo "$help1$help2"
    exit;
}

function showHelp2 {
    echo "$help2"
    exit;
}

function stopUi {
    service omgwebui stop
    service httpd stop
}

function startUi {
    service omgwebui start
    service httpd start
}

if [[ "$standard" = "old" ]]; then
    stopUi
    testForConfigJson
    echo "Setting old webUi as standard on port 80"
    #set apache to port 80
    sed -i -e "s/Listen 8080/Listen 80/" /etc/httpd/conf/httpd.conf
    #set node to port 8080
    sed -i -e "s/: 80,/: 8080,/" /data/webui_config.json
    startUi
elif [[ "$standard" = "new" ]]; then
    stopUi
    testForConfigJson
    echo "Setting new webUi as standard on port 80"
    #set apache to port 8080
    sed -i -e "s/Listen 80$/Listen 8080/" /etc/httpd/conf/httpd.conf
    # set node server to port 80
    sed -i -e "s/: 8080/: 80/" /data/webui_config.json
    startUi

elif [[ "$standard" == "-h" ]]; then
    showHelp2

elif [[ "$standard" == "--help" ]]; then
    showHelp2

else
    echo -e "\e[31;1mUnknown argument!\e[0m"
    showFullHelp
fi
