#!/bin/bash
#
#
# GPL
# (c) 2015-2018 Fabian Herschel
#
Version="2.0.1.2016.11.24.1"
source /usr/share/Landscape/LandscapeCore/bin/get_values
export PATH="$PATH:$LandscapeIn/bin"
hard_reset=0
allSystems=""

# TODO: pxeRoot and bootLoader should come from landscape Repo in the future
pxeRoot="/srv/tftpboot/pxelinux.cfg"
bootLoader="SLES15/boot/x86_64/loader"

while [ $# -gt 0 ]; do
   case "$1" in
       --force )
           hard_reset=1
           ;;
       * )
           allSystems="$allSystems $1"
           ;;
   esac
   shift
done

function reset_single()
{
    local thesystem="$1"
    local SystemBootString=""
    
    for system in "${LandscapeSystems[@]}"
    do
        local -a cleanUp
        cleanUp=( "" )
        for systemKV in ${system}; do
            # name=xxxx  ===> export NET_NAME=xxxx
            cmd=$(awk -F= '{ printf( "export NET_%s=%s\n",toupper($1),$2) }' <<< $systemKV)
            sKey="${systemKV%=*}"; sKey=${sKey^^}
            sValue=${systemKV#*=}
            #echo "cmd: $cmd"
            #echo "Key: $sKey, Value: $sValue"
            cleanUp=( "${cleanUp[@]}" $sKey )
            #set $sKey=$Value
            local SYSTEM_$sKey=$sValue
        done
        #echo "to cleanup: ${cleanUp[@]}"
        #echo SYSTEM_STAR: ${!SYSTEM_*}
       if [ $SYSTEM_SYSTEM = "$thesystem" ]; then
          thehypervisor=$SYSTEM_HYP
       fi
    done
    installSrv="$LandscapeNFSSERVER"
echo "LandscapeBootString=${LandscapeBootString}"
    SystemBootString=${LandscapeBootString:-autoyast=nfs://@@INSTSRV@@/@@LSAYDIR@@/@@SYSTEM@@-ay.xml netdevice=eth0 netsetup=dhcp instmode=cd}
echo "SystemBootString=${SystemBootString}"
    # TODO: add more replace instructions like for IP, netmask and so
    SystemBootString=$(echo $SystemBootString | sed \
                              -e "s/@@INSTSRV@@/$installSrv/g" \
                              -e "s|@@LSAYDIR@@|$LandscapeNFS4AY|g" \
                              -e "s/@@SYSTEM@@/$thesystem/g" \
                         )
echo "SystemBootString=${SystemBootString}"
    if [ "$thehypervisor" = "$HOSTNAME" ]; then
        ssh_or_local="bash" # local run
    else
        ssh_or_local="ssh -T root@$thehypervisor"
    fi
    #
    # optionally destroy system, if already running
    #
    if [ $hard_reset -eq 1 ]; then
        echo "========= hard reset $thesystem ========="
        $ssh_or_local <<EOF
            if virsh list --state-running --name | grep -q $thesystem 1>/dev/null 2>/dev/null; then 
                virsh destroy $thesystem; 
                sleep 2
            fi
EOF
    fi

    send_key_script=$($LandscapeMain/bin/tool_string_to_virsh_sendkey --listonly --domain $thesystem --string="$SystemBootString" )
  
#echo $send_key_script 

    $ssh_or_local <<EOF
        #
        # start system
        #
        virsh start $thesystem; sleep 2 ;
        #
        # jump over the F12 Boot menu message
        #
        virsh send-key $thesystem --codeset linux KEY_ENTER; sleep 5;
        #
        # select "Install SLES 4 SAP"
        #
        virsh send-key $thesystem --codeset linux KEY_DOWN;  sleep 1;
        #
        # send the boot command line to install
        #
        $send_key_script
        sleep 1
        #
        # commit the line
        #
        virsh send-key $thesystem --codeset linux KEY_ENTER;
EOF
}

function reset_single_pxe()
{
    local thesystem="$1"
    local SystemBootString=""
    
    for system in "${LandscapeSystems[@]}"
    do
        local -a cleanUp
        cleanUp=( "" )
        for systemKV in ${system}; do
            # name=xxxx  ===> export NET_NAME=xxxx
            cmd=$(awk -F= '{ printf( "export NET_%s=%s\n",toupper($1),$2) }' <<< $systemKV)
            sKey="${systemKV%=*}"; sKey=${sKey^^}
            sValue=${systemKV#*=}
            #echo "cmd: $cmd"
            #echo "Key: $sKey, Value: $sValue"
            cleanUp=( "${cleanUp[@]}" $sKey )
            #set $sKey=$Value
            local SYSTEM_$sKey=$sValue
        done
        #echo "to cleanup: ${cleanUp[@]}"
        #echo SYSTEM_STAR: ${!SYSTEM_*}
       if [ $SYSTEM_SYSTEM = "$thesystem" ]; then
          thehypervisor=$SYSTEM_HYP
          SYSTEM_PXECFG=$(printf "%02X" $(echo "${SYSTEM_IP}" | tr '.' ' ' ))
       fi
    done
#
######## TODO: maybe to add this (again later, if this script should create the pxe cfg files #####
#
#    installSrv="$LandscapeNFSSERVER"
#    SystemBootString=${LandscapeBootString:-autoyast=nfs://@@INSTSRV@@/@@LSAYDIR@@/@@SYSTEM@@-ay.xml netdevice=eth0 netsetup=dhcp instmode=cd}
#    # TODO: add more replace instructions like for IP, netmask and so
#    SystemBootString=$(echo $SystemBootString | sed \
#                              -e "s/@@INSTSRV@@/$installSrv/g" \
#                              -e "s|@@LSAYDIR@@|$LandscapeAYDirectory|g" \
#                              -e "s/@@SYSTEM@@/$thesystem/g" \
#                         )
    if [ "$thehypervisor" = "$HOSTNAME" ]; then
        ssh_or_local="bash" # local run
    else
        ssh_or_local="ssh -T root@$thehypervisor"
    fi
    #
    # optionally destroy system, if already running
    #
    if [ $hard_reset -eq 1 ]; then
        echo "========= hard reset $thesystem ========="
        $ssh_or_local <<EOF
            if virsh list --state-running --name | grep -q $thesystem 1>/dev/null 2>/dev/null; then 
                virsh destroy $thesystem; 
                sleep 2
            fi
EOF
    fi

    if [ -e "${pxeRoot}/off.${SYSTEM_PXECFG}" ]; then
        mv "${pxeRoot}/off.${SYSTEM_PXECFG}" "${pxeRoot}/${SYSTEM_PXECFG}"
    fi

    $ssh_or_local <<EOF
        #
        # start system
        #
        virsh start $thesystem; sleep 2 ;
EOF
    #
    # move pxe file after boot to avoid install loop
    #
    (sleep 30; mv "${pxeRoot}/${SYSTEM_PXECFG}" "${pxeRoot}/off.${SYSTEM_PXECFG}") &
    

}

for thesystem in $allSystems; do
   reset_single_pxe $thesystem
done
