#**********************************************************************#
#*                                                                    *#
#* Copyright (c) 2001 by Sun Microsystems, Inc.                       *#
#* All rights reserved.                                               *#
#*                                                                    *#
#**********************************************************************#


rm -f /tmp/db2obj.sh /tmp/db2lnk.sh
echo "Determining DB2/6000 libraries and flags... Please wait"
echo "s%RDBMSFLAG=%RDBMSFLAG= -DDBTWO %" >>/tmp/config.current
#----------------------------------------------------------------
# check if running in $PACK/RTSFS or in $UNIKIX/src
#----------------------------------------------------------------
checkpgm()
{
# rdbms directory only used in MTP, MBM does not have rdbms
if [ -f ./rdbms/kxdb2.sqc ]
then
   echo "s%USERSOURCES=%USERSOURCES=	\${USERSRCDIR}/rdbms/kxdb2.sqc%" >>/tmp/config.current
   echo "s%USEROBJECTS=%USEROBJECTS=	\${USERSRCDIR}/rdbms/kxdb2.o%" >>/tmp/config.current
else
   echo "s%USERSOURCES=%USERSOURCES=    \${USERSRCDIR}/kxdb2.sqc%" >>/tmp/config.current
   echo "s%USEROBJECTS=%USEROBJECTS=    \${USERSRCDIR}/kxdb2.o%" >>/tmp/config.current
fi
}

checkpgm_cbl()
{
# rdbms directory only used in MTP, MBM does not have rdbms
if [ -f ./rdbms/KXDB27.sqb ]
then
      echo "s%USERSOURCES=%USERSOURCES=	\${USERSRCDIR}/rdbms/KXDB27.sqb%" >>/tmp/config.current
      echo "s%USEROBJECTS=%USEROBJECTS=	\${USERSRCDIR}/rdbms/KXDB27.o%" >>/tmp/config.current
else
      echo "s%USERSOURCES=%USERSOURCES=	\${USERSRCDIR}/KXDB27.sqb%" >>/tmp/config.current
      echo "s%USEROBJECTS=%USEROBJECTS=	\${USERSRCDIR}/KXDB27.o%" >>/tmp/config.current
fi
}

makefile_change()
{
# If the 64 bit version of DB2 is installed then use the lib32 library
# instead of the lib library because lib is a 64 bit library.
# If the 32 bit version of DB2 is installed then the lib will be a 32 bit
# library and it is OK to use 
 if test -d $INSTHOME/sqllib/lib32; then
    echo "s%LINKLIBDIR=%LINKLIBDIR=-L \$(INSTHOME)/sqllib/lib32 -L/usr/lib%" >>/tmp/config.current
 else
    echo "s%LINKLIBDIR=%LINKLIBDIR=-L \$(INSTHOME)/sqllib/lib -L/usr/lib%" >>/tmp/config.current
 fi
echo "	-ldb2" >>/tmp/db2lnk.sh
#DB2/6000 Link libraries setup

if test $BSD = YES
then
   echo "s%DB2_LINKS=%DB2_LINKS= \\\\%" >>/tmp/config.current
else
   echo 's%DB2_LINKS=%DB2_LINKS= \\\\%' >>/tmp/config.current
fi
echo "/DB2_LINKS=/r /tmp/db2lnk.sh" >>/tmp/config.current
}

#---------------------------------------------------------------------#
#            Check COBDIR for cobdb2 ServerExpress                    #
#---------------------------------------------------------------------#
makefile_change_cbl2()
{
echo "" >>/tmp/db2obj.sh
# If the 64 bit version of DB2 is installed then use the lib32 library
# instead of the lib library because lib is a 64 bit library.
# If the 32 bit version of DB2 is installed then the lib will be a 32 bit
# library and it is OK to use 
 if test -d $INSTHOME/sqllib/lib32; then
    echo "s%LINKLIBDIR=%LINKLIBDIR=-L \$(INSTHOME)/sqllib/lib32 -L/usr/lib%" >>/tmp/config.current
 else
    echo "s%LINKLIBDIR=%LINKLIBDIR=-L \$(INSTHOME)/sqllib/lib -L/usr/lib%" >>/tmp/config.current
 fi
echo "	-ldb2 -ldb2gmf" >>/tmp/db2lnk.sh
#DB2/6000 Objects setup

if test $BSD = YES
then
   echo "s%DB2OBJECTS=%DB2OBJECTS= \\\\%" >>/tmp/config.current
else
   echo 's%DB2OBJECTS=%DB2OBJECTS= \\\\%' >>/tmp/config.current
fi
echo "/DB2OBJECTS=/r /tmp/db2obj.sh" >>/tmp/config.current

#DB2/6000 Link libraries setup
if test $BSD = YES
then
   echo "s%DB2_LINKS=%DB2_LINKS= \\\\%" >>/tmp/config.current
else
   echo 's%DB2_LINKS=%DB2_LINKS= \\\\%' >>/tmp/config.current
fi
echo "/DB2_LINKS=/r /tmp/db2lnk.sh" >>/tmp/config.current
}

#---------------------------------------------------------------------#
#            Check COBDIR for sqlinit.o                               #
#---------------------------------------------------------------------#
makefile_change_cbl()
{
echo 's%COBOPT="%COBOPT="SQL(IBM) %' >>/tmp/config.current
if test -d ${COBDIR}/src/sql; then
   echo "	\$(COBDIR)/src/sql/sqlinit.o " >>/tmp/db2obj.sh
else
   echo "Valid DB2/6000 objects not found. Check if COBOL is installed and $COBDIR variable set"
   sleep 10
fi
echo "	\`cat \$(INSTHOME)//sqllib/lib/db2mkrts.args\`" >>/tmp/db2lnk.sh

#DB2/6000 Objects setup

if test $BSD = YES
then
   echo "s%DB2OBJECTS=%DB2OBJECTS= \\\\%" >>/tmp/config.current
else
   echo 's%DB2OBJECTS=%DB2OBJECTS= \\\\%' >>/tmp/config.current
fi
echo "/DB2OBJECTS=/r /tmp/db2obj.sh" >>/tmp/config.current

#DB2/6000 Link libraries setup

if test $BSD = YES
then
   echo "s%DB2_LINKS=%DB2_LINKS= \\\\%" >>/tmp/config.current
else
   echo 's%DB2_LINKS=%DB2_LINKS= \\\\%' >>/tmp/config.current
fi
echo "/DB2_LINKS=/r /tmp/db2lnk.sh" >>/tmp/config.current

#DB2/6000 for RS6 specific flags

if test $BSD = YES
then
   echo "s%DB2TRAN=U%DB2TRAN=%" >>/tmp/config.current
   echo "s%DB2VSAM=-e \"\"%DB2VSAM=%" >>/tmp/config.current
else
   echo 's%DB2TRAN=U%DB2TRAN=%' >>/tmp/config.current
   echo 's%DB2VSAM=-e \"\"%DB2VSAM=%' >>/tmp/config.current
fi
}

#---------------------------------------------------------------------#
#  check to see which version of DB2 is installed, 32 or 64 bit 
#---------------------------------------------------------------------#
checkDB2lib()
{

$KIXCLS

	echo "                   DB2 Database Configuration              $DATEVAR"
	echo " "
	echo " "
	echo "--------------------------------------------------------------------------------"
	echo " "
#---------------------------------------------------------------------#
#		Insure 32 Bit lib usage
#---------------------------------------------------------------------#
if test -d $INSTHOME/sqllib/lib32; 
   then
    echo "Please note that 32 and 64 bit DB2 libraries have been detected"
    case `uname -s` in
     SunOS)
      echo "  Insure LD_LIBRARY_PATH is set to include $INSTHOME/sqllib/lib32"
      ;;
     AIX)
      echo "  Insure LIBPATH is set to include $INSTHOME/sqllib/lib32"
      ;;
    esac
    echo "  instead of $INSTHOME/sqllib/lib (64 bit) when starting MTP/MBM"
    echo " "
fi
    echo "   "
    echo "   "
}

checkDB2lib
if [ "$1" = "CBL" ]
then
    echo "Defaulting to COBOL Userexits"
#   Only server express supported, No checks for Object Cobol
# This is ServerExpress Cobol, so use the cobol program with shared objects.
    checkpgm_cbl
    makefile_change_cbl2
else
    echo "Defaulting to 'C' Userexits"
    checkpgm
    makefile_change
fi
sleep 2
echo $N ".done"
