#!/bin/bash
# hpc-testing
# Copyright (C) 2025 SUSE LLC
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

test_dapl(){
    local host1=$1
    local ib1=$2
    local host2=$3
    local ib2=$4
    local ip2=$5
    local dapl_name1 dapl_name2

    case $(get_suse_version $host1) in
        16.*)
            return 0
            ;;
        *)
            ;;
    esac

    dapl_name1=$(tpq $host1 "grep '$ib1' /etc/dat.conf | head -n 1 | awk  '{print \$1}'")
    dapl_name2=$(tpq $host2 "grep '$ib2' /etc/dat.conf | head -n 1 | awk  '{print \$1}'")

    if [ "$dapl_name1" == "" -o "$dapl_name2" == "" ]; then
        echo "Failed to get DAPl parameters"
        exit 1
    fi

    # Start test server on host2
    tp $host2 'killall -9 dapl-test || true; dapl-test -T S -d -D '$dapl_name2' >/dev/null'  &
    # Wait for the server to start
    tp $host2 "sleep 2"

    tp $host1 "dapl-test -T T -s $ip2 -D $dapl_name1 -i 100 -t 1 -w 1 -R BE client SR 256 1 server SR 256 1"
    tp $host1 "dapl-test -T T -s $ip2 -D $dapl_name1 -i 100 -t 1 -w 1 -V -P -R BE client SR 1024 -f server SR 1536 2 -f
"
    tp $host1 "dapl-test -T T -s $ip2 -D $dapl_name1 -i 100 -t 1 -w 1 -V -P -R BE client SR 1024 1 server SR 1024 1"
    tp $host1 "dapl-test -T T -s $ip2 -D $dapl_name1 -i 100 -t 1 -w 10 -V -P -R BE client SR 1024 3 server SR 1536 2"
    tp $host1 "dapl-test -T T -s $ip2 -D $dapl_name1 -i 100 -t 1 -w 1 -V -P -R BE client SR 256 1 server RW 4096 1 server SR 256 1"
    tp $host1 "dapl-test -T T -s $ip2 -D $dapl_name1 -i 100 -t 1 -w 1 -V -P -R BE client SR 256 1 server RR 4096 1 server SR 256 1"
    tp $host1 "dapl-test -T T -s $ip2 -D $dapl_name1 -i 100 -t 4 -w 8 -V -P -R BE client SR 256 1 server RR 4096 1 server SR 256 1 client SR 256 1 server RW 4096 1 server SR 256 1"
    tp $host1 "dapl-test -T P -s $ip2 -D $dapl_name1 -i 1024 -p 64 -m p RW 8192 2"
    tp $host1 "dapl-test -T P -s $ip2 -D $dapl_name1 -i 1024 -p 64 -m p RR 4096"
    tp $host1 "dapl-test -T T -s $ip2 -D $dapl_name1 -i 100 -t 1 -w 10 -V -P -R BE client SR 1024 3 server SR 1536 2"
    # Ask server to quit
    tp $host2 "dapl-test -T Q -s $ip2 -D $dapl_name1"
    wait
}
