#!/bin/bash
##
## @brief @(#) Test004 - clusterised version
##
## @file run-dom.sh
##
## -----------------------------------------------------------------------------
## Enduro/X Middleware Platform for Distributed Transaction Processing
## Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
## Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
## This software is released under one of the following licenses:
## AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
## See LICENSE file for full text.
## -----------------------------------------------------------------------------
## AGPL license:
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU Affero General Public License, version 3 as published
## by the Free Software Foundation;
##
## 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 Affero General Public License, version 3
## for more details.
##
## You should have received a copy of the GNU Affero General Public License along 
## with this program; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
## -----------------------------------------------------------------------------
## A commercial use license is available from Mavimax, Ltd
## contact@mavimax.com
## -----------------------------------------------------------------------------
##

export TESTNO="004"
export TESTNAME_SHORT="basicevent"
export TESTNAME="test${TESTNO}_${TESTNAME_SHORT}"

PWD=`pwd`
if [ `echo $PWD | grep $TESTNAME ` ]; then
	# Do nothing 
	echo > /dev/null
else
	# started from parent folder
	pushd .
	echo "Doing cd"
	cd $TESTNAME
fi;

. ../testenv.sh

export TESTDIR="$NDRX_APPHOME/atmitest/$TESTNAME"
export PATH=$PATH:$TESTDIR
# Override timeout!
export NDRX_TOUT=10

#
# Domain 1 - here client will live
#
function set_dom1 {
    echo "Setting domain 1"
    . ../dom1.sh
    export NDRX_CONFIG=$TESTDIR/ndrxconfig-dom1.xml
    export NDRX_DMNLOG=$TESTDIR/ndrxd-dom1.log
    export NDRX_LOG=$TESTDIR/ndrx-dom1.log
    export NDRX_DEBUG_CONF=$TESTDIR/debug-dom1.conf
}

#
# Domain 2 - here server will live
#
function set_dom2 {
    echo "Setting domain 2"
    . ../dom2.sh    
    export NDRX_CONFIG=$TESTDIR/ndrxconfig-dom2.xml
    export NDRX_DMNLOG=$TESTDIR/ndrxd-dom2.log
    export NDRX_LOG=$TESTDIR/ndrx-dom2.log
    export NDRX_DEBUG_CONF=$TESTDIR/debug-dom2.conf
}

#
# Domain 3 - here server will live
#
function set_dom3 {
    echo "Setting domain 3"
    . ../dom3.sh    
    export NDRX_CONFIG=$TESTDIR/ndrxconfig-dom3.xml
    export NDRX_DMNLOG=$TESTDIR/ndrxd-dom3.log
    export NDRX_LOG=$TESTDIR/ndrx-dom3.log
    export NDRX_DEBUG_CONF=$TESTDIR/debug-dom3.conf
}

#
# Generic exit function
#
function go_out {
    echo "Test exiting with: $1"
    
    set_dom1;
    xadmin psc
    xadmin stop -y
    xadmin down -y

    set_dom2;
    xadmin psc
    xadmin stop -y
    xadmin down -y

    set_dom3;
    xadmin psc
    xadmin stop -y
    xadmin down -y

    # If some alive stuff left...
    xadmin killall atmiclt4

    popd 2>/dev/null
    exit $1
}

#
# Print domain statuses...
#
function print_domains {

    set_dom1;
    xadmin ppm
    xadmin psvc
    xadmin psc

    set_dom2;
    xadmin ppm
    xadmin psvc
    xadmin psc

    set_dom3;
    xadmin ppm
    xadmin psvc
    xadmin psc

}

rm *dom*.log

set | grep FLD
set | grep FIELD

set_dom1;
xadmin down -y

set_dom2;
xadmin down -y

set_dom3;
xadmin down -y

set_dom1;
#xadmin down -y
xadmin start -y || go_out 1


set_dom2;
#xadmin down -y
#xadmin start -y || go_out 2
xadmin start -y 

set_dom3;
#xadmin down -y
xadmin start -y || go_out 3

$PSCMD | grep tpev

# Let domains to connect between them selves
sleep 60

print_domains;

# Go to domain 1
set_dom1;

# Run the client test...
echo "Starting to issue calls:"

# Networked mode
(./atmiclt4 Y 2>&1) > ./atmiclt-dom1.log

RET=$?

#echo "*** LSOF ***"
#lsof 2>/dev/null
#echo "*** LSOF ***"

# Catch is there is test error!!!
if [ "X`grep TESTERROR *.log`" != "X" ]; then
	echo "Test error detected!"
	RET=-2
fi

# There should be TPENOENT in tpevsrv-dom1.log, because domain3 have no tpevsrv!
if [ "X`grep TPENOENT tpevsrv-dom1.log`" != "X" ]; then
	echo "Test error detected - no TPENOENT in tpevsrv-dom1.log"
	RET=-3
fi

go_out $RET
# vim: set ts=4 sw=4 et smartindent:
