#! /bin/sh
#
# Smoke test XA support.

msg()
{
	echo "========"
	echo "======== $1"
	echo "========"
}

func_clean()
{
	rm -rf run
}

# Build the configuration file --
#	We do this work in the shell script because we have to fill in
#	lots of shell variables.
func_ubbinit()
{
	MACHINE_NAME=`uname -n`
	cat > $RUN/config/ubb.cfg << END_OF_UBB_FILE
*RESOURCES
IPCKEY		200103
DOMAINID	domain3
MASTER		cluster3
MAXACCESSERS	10
MAXSERVERS	5
MAXSERVICES	10
MODEL		SHM
LDBAL		N

*MACHINES
DEFAULT:
		APPDIR="$RUN/bin"
		TUXCONFIG="$TUXCONFIG"
		TLOGDEVICE="$TLOGDEVICE"
		TUXDIR="$TUXDIR"
# Machine name is 30 characters max
$MACHINE_NAME		LMID=cluster3

*GROUPS
# Group name is 30 characters max
group_tm	LMID=cluster3 GRPNO=1 TMSNAME=DBRM TMSCOUNT=2 OPENINFO="BERKELEY-DB:$RUN/data"

*SERVERS
DEFAULT:
		CLOPT="-A"

# Server name is 78 characters max (same for any pathname)
server1		SRVGRP=group_tm SRVID=1 MAXGEN=3 RESTART=Y
server2		SRVGRP=group_tm SRVID=2 MAXGEN=3 RESTART=Y

*SERVICES
# Service name is 15 characters max
# server1
TestTxn1
# server2
TestTxn2
END_OF_UBB_FILE
	tmloadcf -y $RUN/config/ubb.cfg
}

init_tmadmin()
{
tmadmin << END_OF_TMADMIN
	crdl -z $TLOGDEVICE -b 500
	crlog -m cluster3
END_OF_TMADMIN
}

# Run a test.
run()
{
	msg "CLEANING UP FROM THE LAST RUN."
	rm -rf run
	mkdir -p run/bin run/config run/data

	msg "BUILDING THE CONFIGURATION FILE."
	func_ubbinit

	# Everything else is done in run/bin.
	cd $RUN/bin

	# The CFLAGS variable defines the pre-processor defines -- start with
	# whatever the user set, and add our own stuff.
	#
	# For debugging output, add -DVERBOSE to COMPILE_FLAGS, by uncommenting
	# the following line.
	#
	# Verbose output from the client appears in this script's stdout,
	# (which you can re-direct below, when the client is run).
	# Verbose output from the server appears in the file run/bin/stdout.
	#
	# COMPILE_FLAGS="-DVERBOSE"
	COMPILE_FLAGS="$CFLAGS $COMPILE_FLAGS -g -I../../.."

	msg "BUILDING CLIENT"
	CFLAGS="$COMPILE_FLAGS"; export CFLAGS
	buildclient -v -r BERKELEY-DB -o client \
	    -f ../../src/htimestampxa.c -f ../../src/client.c
	test "$?" -eq 0 || {
		echo "FAIL: buildclient failed."
		exit 1
	}

	msg "BUILDING SERVER #1"
	CFLAGS="$COMPILE_FLAGS -DSERVER1"; export CFLAGS
	buildserver -v -r BERKELEY-DB -o server1 \
	    -s TestTxn1:TestTxn1 \
	    -f ../../src/htimestampxa.c -f ../../src/server.c
	test "$?" -eq 0 || {
		echo "FAIL: buildserver failed."
		exit 1
	}

	msg "BUILDING SERVER #2"
	CFLAGS="$COMPILE_FLAGS -DSERVER2"; export CFLAGS
	buildserver -v -r BERKELEY-DB -o server2 \
	    -s TestTxn2:TestTxn2 \
	    -f ../../src/htimestampxa.c -f ../../src/server.c
	test "$?" -eq 0 || {
		echo "FAIL: buildserver failed."
		exit 1
	}

	msg "BUILDING THE RESOURCE MANAGER."
        buildtms -v -o DBRM -r BERKELEY-DB

	msg "BUILDING THE LOG DEVICE."
	init_tmadmin

	# Boot Tuxedo.
	# You should see something like:
	#
	# INFO: BEA Tuxedo, Version 8.1
	# INFO: Serial #: 650522264138-1510597376252,
	#	Expiration 2005-02-15, Maxusers 100
	# INFO: Licensed to: BEA Evaluation Customer
	#
	# Booting admin processes ...
	#
	# exec BBL -A :
	#         process id=13845 ... Started.
	#
	# Booting server processes ...
	#
	# exec DBRM -A :
	#         process id=13846 ... Started.
	# exec DBRM -A :
	#         process id=13847 ... Started.
	# exec server1 -A :
	#         process id=13848 ... Started.
	# exec server2 -A :
	#         process id=13849 ... Started.
	# 5 processes started.
	msg "BOOTING TUXEDO."
	tmboot -y

	# Run the client with 10, 100 and 1000 transactions.
	exitval=0
	for i in 10 100 1000; do
		msg "RUN THE CLIENT WITH $i TRANSACTIONS."
		# You can get debugging output on just the client by
		# adding -v to the command line.
		#
		# ./client -v -n $i
		./client -n $i
		test "$?" -ne 0 && {
			echo "FAIL: client failed"
			exitval=1
			break;
		}
	done

	msg "SHUTTING DOWN THE TRANSACTION MANAGER."
	echo 'y' | tmshutdown

	# Copy out Tuxedo's logging.
	msg "ULOG FILES:"
	cat ULOG*

	# Copy out any server output.
	msg "STDOUT:"
	cat stdout

	# Copy out any server errors.
	msg "STDERR:"
	cat stderr
	test -s stderr && {
		echo "FAIL: stderr file not empty"
		exitval=1
	}

	# We never checkpointed, run recovery to make sure it all works.
	msg "RECOVERY:"
	../../../db_recover -h ../data -v
	test "$?" -ne 0 && {
		echo "FAIL: recovery failed"
		exitval=1
	}

	return $exitval
}

# Debug the shell script.
# set -x

# Check to make sure we have a Tuxedo build we understand.
test -z "$TUXDIR" && {
	echo "FAIL: the TUXDIR environment variable NOT set"
	echo \
    "FAIL: TUXDIR must be set to the absolute path of the Tuxedo install"
	echo "FAIL: immediately above the subdirectories bin, include and lib"
	exit 1
}
dlist="include lib"
for i in $dlist; do
	test -d $TUXDIR/$i || {
		echo "FAIL: check the Tuxedo install"
		echo "FAIL: the required directory $TUXDIR/$i does not exist"
		exit 1
	}
done
flist="bin/buildclient bin/buildserver bin/buildtms bin/tmadmin bin/tmboot
       bin/tmloadcf bin/tmshutdown udataobj/RM"
for i in $flist; do
	test -f $TUXDIR/$i || {
		echo "FAIL: check the Tuxedo install"
		echo "FAIL: the required file $TUXDIR/$i does not exist"
		exit 1
	}
done
msg "Using Tuxedo $TUXDIR installation"

# Set the location of the Berkeley DB libraries -- allow the user to override.
# Check to make sure we have a Berkeley DB installation.  (I'd like to use the
# local DB installation, but I've never been able to make Tuxedo load shared
# libraries from the .libs directory.)
REL=../../dist/RELEASE
test -z "$DB_INSTALL" && test -f $REL && {
	. $REL
	DB_INSTALL=/usr/local/BerkeleyDB.${DB_VERSION_MAJOR}.${DB_VERSION_MINOR}
	export DB_INSTALL
}
if test -f "$DB_INSTALL/lib/libdb.so"; then
	msg "Using Berkeley DB $DB_INSTALL/lib/ installation"
else
	echo "FAIL: $DB_INSTALL/lib/libdb.so not found"
	echo \
  "FAIL: DB_INSTALL must be set to the absolute path of the Berkeley DB install"
	exit 1
fi

# You may need to update the Tuxedo resource manager file.  It should be in:
#
#	$TUXDIR/udataobj/RM
#
# Solaris requires a line something like the following:
#
#	BERKELEY-DB:db_xa_switch:-L${DB_INSTALL}/lib -ldb
#
# where DB_INSTALL is a Berkeley DB install, and /lib contains DB libraries.
egrep "^BERKELEY-DB:db_xa_switch:" $TUXDIR/udataobj/RM > /dev/null || {
	echo "FAIL: $TUXDIR/udataobj/RM does not list DB as one of its RMs"
	echo "FAIL: Try adding:"
	echo "FAIL:	BERKELEY-DB:db_xa_switch:-L\${DB_INSTALL}/lib -ldb"
	exit 1
}

# Everything is built in and run from the "run" subdirectory.
RUN=`pwd`/run; export RUN

FIELDTBLS32=datafml.fml;	export FIELDTBLS32
FLDTBLDIR32=$RUN/config;	export FLDTBLDIR32
TLOGDEVICE=$RUN/data/dlog;	export TLOGDEVICE
TUXCONFIG=$RUN/config/tuxconfig;export TUXCONFIG

PATH="$PATH:$RUN/bin:$TUXDIR/bin"
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DB_INSTALL/lib:$TUXDIR/lib;
export LD_LIBRARY_PATH PATH

if test $# -eq 1; then
	case "$1" in
	clean)				# Clean up from previous runs.
		func_clean;;
	shutdown)			# Shutdown Tuxedo from previous runs.
		echo 'y' | tmshutdown -w 5;;
	*)
		echo 'usage: chk.xa [clean | shutdown]'
		exit 1;;
	esac
else
	run				# Run the XA test.
	if test "$?" -ne 0; then
		exit 1
	fi
fi

exit 0
