#!/bin/bash
_main_(){

# ----------------------------------------------------------------------------

#grep -q '^Ubuntu 16.04' /etc/issue 2> /dev/null || { >&2 echo 'Error: This is neither Ubuntu 16.04 nor 18.04'; exit 1; }
if   $(grep -q '^Ubuntu 14.04' /etc/issue 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_u14.py'
elif $(grep -q '^Ubuntu 16.04' /etc/issue 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_u16.py'
elif $(grep -q '^Ubuntu 18.04' /etc/issue 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_u18.py'
elif $(grep -q '^Ubuntu 20.04' /etc/issue 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_u20.py'
elif $(grep -q '^Ubuntu 21.04' /etc/issue 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_u21.py'
elif $(grep -q '^Ubuntu 22.04' /etc/issue 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_u22.py'
elif $(grep -q '^Ubuntu 24.04' /etc/issue 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_u24.py'
elif $(grep -q '^Welcome to openSUSE Tumbleweed' /etc/issue 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_suseT.py'
elif $(grep -q '^CentOS Linux release 7' /etc/centos-release 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_cOS7or8_aml8.py'
elif $(grep -q '^CentOS Linux release 8' /etc/centos-release 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_cOS7or8_aml8.py'
elif $(grep -q '^AlmaLinux release 8' /etc/almalinux-release 2> /dev/null) ; then
    UPDATE_PY='_update_settings_on_this_machine_cOS7or8_aml8.py'
else
    >&2 echo 'Error: OS/Distribution not supported. Supported OS:'
    >&2 echo '       Ubuntu 14.04'
    >&2 echo '       Ubuntu 16.04'
    >&2 echo '       Ubuntu 18.04'
    >&2 echo '       Ubuntu 20.04'
    >&2 echo '       Ubuntu 21.04'
    >&2 echo '       Ubuntu 22.04'
    >&2 echo '       Ubuntu 24.04'
    >&2 echo '       openSUSE Tumbleweed'
    >&2 echo '       CentOS Linux release 7'
    >&2 echo '       CentOS Linux release 8'
    >&2 echo '       AlmaLinux release 8'
    exit 1
fi
test "$( id -u )" = 0 || { >&2 echo 'Error: You are not root, please use "sudo" to run the script'; exit 1; }
#command -v sshd > /dev/null || { >&2 echo 'Error: The package "openssh-server" is not installed'; exit 1; }
command -v sshd || { >&2 echo 'Error: The package "openssh-server" is not installed'; exit 1; }
command -v git > /dev/null || { >&2 echo 'Error: The package "git" is not installed'; exit 1; }

# ----------------------------------------------------------------------------

ORIG_DIR=$PWD
TEMP_DIR=$( mktemp -d )

test -n "$TEMP_DIR" && cd "$TEMP_DIR" || { >&2 echo 'Error: Failed to enter a temporary directory'; exit 1; }

_clean_up_tmp_files () { echo 'Deleting temporary files...'; cd "$ORIG_DIR"; rm -rf "$TEMP_DIR"; echo 'Deleting temporary files...  DONE'; }
trap _clean_up_tmp_files EXIT

# ----------------------------------------------------------------------------

cd "$TEMP_DIR"
echo 'Downloading the latest snapshot from the GitHub git repository...'
git clone --quiet --depth 1 https://github.com/fast-crypto-lab/cluster
echo 'Downloading the latest snapshot from the GitHub git repository...  DONE'

# ----------------------------------------------------------------------------

cd "$TEMP_DIR/cluster"

# ----------------------------------------------------------------------------

echo 'Trying to acquire a lock...'
if bash -c "./${UPDATE_PY} lock"
then
    echo 'Trying to acquire a lock...  DONE'
else
    >&2 echo 'Error: Failed to acquire a lock, the file /fcl-cluster-maintenance.lock already exists'
    exit 2
fi

# ----------------------------------------------------------------------------

echo 'Checking whether this host is already listed in fcl.json...'
if bash -c "./${UPDATE_PY} check-this-host-in-json"
then
    echo 'Checking whether this host is already listed in fcl.json...  DONE'
else
    >&2 echo 'Error: the command `./_update_settings_on_this_machine.py check-this-host-in-json` failed'
    bash -c "./${UPDATE_PY} unlock"
    exit 3
fi

# ----------------------------------------------------------------------------

echo 'Checking whether fcl.json is applicable for this host...'
if bash -c "./${UPDATE_PY} check-json-applicability"
then
    echo 'Checking whether fcl.json is applicable for this host...  DONE'
else
    >&2 echo 'Error: the command `./_update_settings_on_this_machine.py check-json-applicability` failed'
    bash -c "./${UPDATE_PY} unlock"
    exit 4
fi

# ----------------------------------------------------------------------------

echo 'Applying fcl.json to this host...'
if bash -c "./${UPDATE_PY} apply-json"
then
    echo 'Applying fcl.json to this host...  DONE'
else
    >&2 echo 'Error: the command `./_update_settings_on_this_machine.py apply-json` failed'
    bash -c "./${UPDATE_PY} unlock"
    exit 5
fi

# ----------------------------------------------------------------------------

echo 'Releasing the lock...'
bash -c "./${UPDATE_PY} unlock"
echo 'Releasing the lock...  DONE'

# ----------------------------------------------------------------------------

};_main_
