#!/bin/bash

set -x
export PS4='+($(basename ${BASH_SOURCE}):${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

if [ "$1" != "first" -a "$1" != "join" ]; then
    echo "Usage: $(basename $0) <first|join>"
    echo ""
    echo "Execute this script on host that should be setup to be a CmonHA node."
    echo "Specify whether this is the first CMonHa node or should join to "
    echo "existing CmonHA cluster."
    echo ""
    echo "If you are using s9s_experimental_cmonha_setup, there is no need to "
    echo "manually execute this script. The s9s_experimental_cmonha_setup "
    echo "will copy this to node and execute as well."
    echo ""
    echo "Note: This is a highly experimental script to collect the main steps "
    echo "      and is not even planned to be supported or enhanced. "
    echo "      Take it as an examples of commands and tailor it to your needs."
    echo ""
    exit -1
fi

CMON_USER="myadmin"
CMON_PASSWORD="pwd"

export DEBIAN_FRONTEND="noninteractive"

function wait_apt_unlocked()
{
    echo "Waiting at most 5 minutes for /var/lib/dpkg/lock-frontend to be removed"
    for delay in $(seq 1 100); do
        if sudo [ ! -e /var/lib/dpkg/lock-frontend ]; then
            break;
        fi
        sleep 3
    done
}

wait_apt_unlocked

echo "deb [arch=amd64] http://repo.severalnines.com/deb ubuntu main" | \
    sudo tee /etc/apt/sources.list.d/s9s-repo.list
wget http://repo.severalnines.com/severalnines-repos.asc -O- | \
    sudo apt-key add -

sudo apt-get update > /dev/null
if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

sudo apt-get install --assume-yes clustercontrol-controller > /dev/null
if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

echo 'RPC_BIND_ADDRESSES="0.0.0.0"' | sudo tee /etc/default/cmon

echo "deb http://repo.severalnines.com/s9s-tools/focal/ ./" | \
    sudo tee /etc/apt/sources.list.d/s9s-tools.list

sudo apt-get update > /dev/null
if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

sudo apt-get install --assume-yes s9s-tools > /dev/null
if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

sudo systemctl stop cmon

if [ "$1" = "first" ]; then
    sudo cmon --init \
        --mysql-username="cmon" \
        --mysql-password="cmon"
    if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

    sudo systemctl enable cmon
    if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

    sudo systemctl start cmon
    if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

    echo "Waiting for cmon to create /var/lib/cmon/usermgmt.fifo"
    for delay in $(seq 1 100); do
        if sudo [ -e "/var/lib/cmon/usermgmt.fifo" ]; then
            echo "Found /var/lib/cmon/usermgmt.fifo"
            break;
        fi
        sleep 3
        if [ "$delay" = "100" ]; then
            echo "Failed to find /var/lib/cmon/usermgmt.fifo needed for initial user creation."
            exit 1;
        fi
    done

    s9s user  \
        --create \
        --group=admins \
        --generate-key \
        --controller=https://localhost:9501 \
        --new-password="${CMON_PASSWORD}" \
        --email-address=${CMON_USER}@cmonhahost1 \
        --first-name=Firstname \
        --last-name=Lastname \
        ${CMON_USER}
    if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

    s9s controller \
        --controller=https://localhost:9501 \
        --enable-cmon-ha
    if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi
else # join
    sudo systemctl enable cmon
    if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi

    sudo systemctl start cmon
    if [ "$?" -ne 0 ]; then echo "Install failed."; exit 1; fi
fi

# Give time to cmon to initialize
sleep 30

