#!/usr/bin/bash

function run() {
    local node="$1" testID="$2" test_prop="$3" repeat="$4" sleep="$5"
    logFile="testLog$(date +"%Y-%m-%d").txt"
    logLink="testLog.curr.txt"
    if [ ! -f "$logFile" ]; then
        touch "$logFile"
    fi
    if [ -L "$logLink" ]; then
        rl=$(readlink "$logLink")
        if [[ "$rl" != "$logFile" ]]; then
            ln -s -f "$logFile" "$logLink"
        fi
    else
        ln -s "$logFile" "$logLink"
    fi
    SAPHanaSR-testCluster-multiNode --testFile="$test_dir/${testID}.json" \
                            --remoteNodes $nodes \
                            --repeat="$repeat" \
                            --dumpFailures \
                            --defaultChecksFile="$test_dir/defaultChecks.json" \
                            --properties="$test_dir/$test_prop.json" \
                            --logFile "$local_dir/$logFile"
    # ln -s -f testLog2023-03-31.txt testLog.curr.txt
    sleep "$sleep"; 
    return 0
}

function usage() {
     echo "usage: $0 [--test_scenario=...] --nodes=... [--test_case=...] [--properties=...] | --help"
}

test_scenario="angi-ScaleUp"
test_case="nop"
test_prop="properties"
local_dir="$PWD"
nodes=""

while [ $# -gt 0 ]; do
    case "$1" in
        --test_scenario=* )
                test_scenario=${1#*=}
                echo "param: test_scenario=$test_scenario"
                shift
                ;;
        --nodes )
                shift
                while [ $# -gt 0 ]; do
                    case "$1" in
                        --* ) break;;
                        * )
                            nodes="$nodes ${1#*=}";;
                    esac
                    shift
                done
                echo "param: nodes=$nodes"
                ;;
        --test_case=* )
                test_case=${1#*=}
                echo "param: test_case=$test_case"
                shift
                ;;
        --properties=* )
                test_prop=${1#*=}
                echo "param: properties=$test_prop"
                shift
                ;;
        --help* )
                usage
                exit 2
                ;;
    esac
done

test_dir="/usr/share/SAPHanaSR-tester/json/$test_scenario"

echo "nodes: $nodes, test_scenario=$test_scenario, test_case=$test_case"
run "$node" "$test_case" "$test_prop" 1 10
