#!/bin/ksh

#  Build script for Micro Focus XA switch modules
#  (C) Copyright 2003-2006 Micro Focus (IP) Limited
#  All Rights Reserved

#  Usage :
#  build [ db2 | ora ]

# @(#)build 1.15

function usage
{
	echo "usage :"
	echo "build [ db2 | ora | ora1pc ]"
	echo "build db21pc db2_database [ db2_uid db2_pwd]"
	return 1
}

function builderror
{
	echo "Error linking switch load module. Please verify that your"
	echo "COBOL and Database environment is set up correctly."
	return 2
}

function builderror3
{
	echo "Oracle switch load module has not been built correctly."
	echo "Please contact Micro Focus SupportLine, having logged the output of"
	echo "\nksh -x build ora"
	return 3
}

case x.$1 in
x.db21pc)
	if test $# -le 1
	then
		usage
		exit 1
	else
		DB2ALIAS=$2
		DB2UID=$3
		DB2PWD=$4
		if test x.$DB2UID != x. -a x.$DB2PWD = x.
		then
			usage
			exit 1
		fi
	fi
	;;
*)
	if test $# != 1
	then
		usage
		exit 1
	fi
	;;
esac

if test x.$COBDIR = x.
then
	builderror
	exit 2
fi

test -f $COBDIR/bin/cobmode && cm=`cobmode | awk '{print $5}'`

if test x.$cm = x.64 -o x.$COBMODE = x.64 -o ! -d $COBDIR/dynload
then
	echo "XA Switch modules are only functional in a 32-bit environment"
	exit 3
fi

param=`echo $1 | tr "[a-z]" "[A-Z]"`

case $param in
DB2*)
	if test x.$DB2INSTANCE = x.
	then
		builderror
		exit 2
	fi
	case $param in
	DB2)
		DB2OPC_OPTS="-d esdb2opc_1pc_commit@ESDB2OPC.so -d esdb2opc_1pc_connect@ESDB2OPC.so -d esdb2opc_1pc_disconnect@ESDB2OPC.so -d esdb2opc_1pc_rollback@ESDB2OPC.so"
		cob -z,sys,nounload ESDB2XA.CBL -d db2xacic@libdb2.so.1 $DB2OPC_OPTS
		bldrtn=$?
		;;
	DB21PC)
		if test x.$DB2UID = x.
		then
			COBCPY=$COBDIR/cpylib cob -z,sys,nounload ESDB2OPC.CBL -C "db2(DB==$DB2ALIAS)" -d cobdb2
		else
			COBCPY=$COBDIR/cpylib cob -z,sys,nounload ESDB2OPC.CBL -C "db2(DB==$DB2ALIAS PASS==$DB2UID.$DB2PWD)" -d cobdb2
		fi
		bldrtn=$?
		;;
	esac

	# Verify that the switch module has been built correctly

	if test $bldrtn -ne 0
	then
		builderror
		exit 2
	fi
	rm -f ESDB2*.i?? ESDB2*.o
	;;

ORA*)
	if test x.$ORACLE_HOME = x.
	then
		builderror
		exit 2
	fi


	if test -d $ORACLE_HOME/lib32
	then
		libdir=lib32
	else
		libdir=lib
	fi

	case $param in
	ORA)
		ORAOPC_OPTS="-d esoraopc_1pc_commit@ESORAOPC.so -d esoraopc_1pc_connect@ESORAOPC.so -d esoraopc_1pc_disconnect@ESORAOPC.so -d esoraopc_1pc_rollback@ESORAOPC.so"
# Build flags for x86 and zSeries running Linux
		COBFLAGS="`cat $ORACLE_HOME/$libdir/ldflags`   `cat $ORACLE_HOME/$libdir/sysliblist` -ldl -lm  "

		cob -z,sys,nounload ESORAXA.CBL -to ESORAXA.so -e "" -L/bin/unix -L$ORACLE_HOME/$libdir/ $ORACLE_HOME/precomp/$libdir/cobsqlintf.o -lclntsh $COBFLAGS $ORAOPC_OPTS
		bldrtn=$?
		;;
	ORA1PC)
		COBCPY=$COBDIR/cpylib cob -ztk ESORAOPC.pco
		bldrtn=$?
		;;
	esac
		
	# Verify that the switch module has been built correctly

	if test $bldrtn -ne 0
	then
		builderror3
		exit 2
	fi

	rm -f ESORA*.i?? ESORA*.o
	;;
*)
	usage
	exit 1
	;;
esac

exit 0
