#!/bin/sh
#
#######################################################
#
# automate-005-configure-ntp-dns
#
#######################################################
#
# License: GPL
# Author: Fabian Herschel 2016
# (c) 2016-2023 SUSE Linux GmbH, Nuremberg, Germany
#
# To be called at LandscapeMaster
#
# Tasks automated:
# - configure ntp client at the VMs 
#
# Preequisites:
# - VMs installed and ssh login configured
#
# Syntax:
# automate-005-configure-ntp-dns [--owner=<owner> | --group=<group>] [--node=<nodeName>]
#
# Next Automation Script
#    TBD
#
#######################################################
#
# Version 3.0.2023.04.17.1
#
source /usr/share/Landscape/bin/get_values
export PATH="$PATH:$LandscapeMain/bin"

GROUP=""
node="all"
declare -a allSystems=()

while [ $# -gt 0 ]; do
    case "$1" in
        --group=* | --owner=* ) GROUP=${1#--group=}
                   ;;
        --node=* ) node=${1#--node=}
                   ;;
        --force )
                   reset_options="${reset_options:+$reset_options }--force"
                   ;;
    esac
    shift
done

function filterSystem()
{
    local systemDef="" systemKV=""
    local fields="SYSTEM"
    local f="" ptrF="" outPut=0 outStr=""
    while [ $# -gt 0 ]; do
        case "$1" in
            --system ) systemDef="$2"; shift
                       ;;
            --fields ) fields="$2"; shift
                       ;;
        esac
        shift
    done
    for systemKV in ${systemDef}; do
        sKey="${systemKV%=*}"; sKey=${sKey^^}
        sValue=${systemKV#*=}
        local SYSTEM_$sKey=$sValue
    done
    # BAUSTELLE
    if [ $GROUP = "all" -a $node = "all" ]; then
        outPut=1
    elif [ $GROUP = "$SYSTEM_OWNER" -a $node = "all" ]; then
        outPut=1
    elif [ $GROUP = "all" -a $node = "$SYSTEM_SYSTEM" ]; then
        outPut=1
    elif [ $GROUP = "$SYSTEM_OWNER" -a $node = "$SYSTEM_SYSTEM" ]; then
        outPut=1
    fi
    if [ $outPut -eq 1 ]; then
        for f in $fields; do
            ptrF="SYSTEM_$f"
            outStr="${outStr}${outStr:+ }${!ptrF}"
        done
        printf "%s" "$outStr"
    fi
}

function configure_ntp_client()
{
    local system=$1
    echo "configure_ntp_client $system"
    ssh -T root@$system <<EOF
    #
    # NTP
    #
    case "$LandscapeSLES" in
        SLE12* | SLES12* )
            echo "server $LandscapeNTPSERVER iburst" >/etc/ntp.conf
            echo "1 SHA1 qOgfwVF3BDbW4zoJJWG5" >/etc/ntp.keys
            systemctl enable ntpd
            systemctl restart ntpd
        ;;
        SLE11* | SLES11* )
            # TODO: SLE11
            chkconfig ntp on
            rcntp restart
        ;;
        SLE15* | SLES15* )
            echo "server $LandscapeNTPSERVER iburst" >/etc/chrony.d/pool-alpen.conf
            systemctl enable chronyd
            systemctl restart chronyd
        ;;
    esac
EOF
}

function configure_dns_client()
{
    local system=$1
    echo "configure_dns_client $system"
    ssh -T root@$system <<EOF
    #
    # DNS
    #
    (
        echo "search $LandscapeDOMAIN"
        echo "nameserver $LandscapeNAMESERVER"
    ) > /etc/resolv.conf
EOF
}

# select hyp,system from LandscapeSystems where ...

for systemDef in "${LandscapeSystems[@]}"
do
    help1=$(filterSystem --fields 'SYSTEM' --system "$systemDef"; printf "\n")
    if [ ${#allSystems[@]} -gt 0 ]; then
        allSystems=( "${allSystems[@]}" "$help1" )
    else 
        allSystems=( "$help1" )
    fi
done

for s in "${allSystems[@]}"; do
    read sys X <<< "$s"
    if [ -n "$sys" ]; then
        configure_ntp_client "$sys"
        configure_dns_client "$sys"
    fi
done
