dnl  
dnl Oratcl-2.5 configure.in   Relies heavily on Tcl/Tk/TclX being installed
dnl and finding tclConfig.sh, tkConfig.sh, tclxConfig.sh, tkxConfig.sh
dnl Tom Poindexter
dnl

AC_INIT(../generic/oratcl.c)
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_CHECK_FUNC(bcopy, , AC_DEFINE(NO_BCOPY))

if test -z "$prefix" -o "$prefix" = "NONE" ; then
    prefix=/usr/local
fi

if test -z "$exec_prefix" -o "$exec_prefix" = "NONE" ; then
    exec_prefix=$prefix
fi

prefix_locations="$prefix /usr/local /usr .."
exec_prefix_locations="$exec_prefix /usr/local /usr .."


#----------------------------------------------------------------------------
#       Get oratcl.c version, set sh variable ORATCL_VERSION
#       and set ORATCL_LIBVER
#----------------------------------------------------------------------------

eval `egrep '^#define ORATCL_VERSION' ../generic/oratcl.c | \
      sed -e 's/#define //' -e 's/ *"/="/'`

# set the library version as with . stripped out

ORATCL_LIBVER=`echo $ORATCL_VERSION | sed -e 's/\.//'`

#----------------------------------------------------------------------------
#       Check where Tcl/Tk and maybe TclX is installed 
#       based on prefix and exec_prefix value
#----------------------------------------------------------------------------

# find include/tcl.h
for f in $prefix_locations ; do
  if test -r "$f/include/tcl.h"; then
    TCL_INC_DIR=$f/include
    AC_MSG_RESULT(setting TCL_INC_DIR to $TCL_INC_DIR)
    break
  fi
done
 
# find lib/tclConfig.sh
for f in $exec_prefix_locations ; do
  if test -r "$f/lib/tclConfig.sh"; then
    TCL_LIB_DIR=$f/lib
    AC_MSG_RESULT(setting TCL_LIB_DIR to $TCL_LIB_DIR)
    break
  fi
done
 
if test -z "$TCL_INC_DIR" ; then
  AC_ERROR(could not find include/tcl.h in $prefix_locations)
fi
if test -z "$TCL_LIB_DIR" ; then
  AC_ERROR(could not find lib/tclConfig.sh in $exec_prefix_locations)
fi

SEARCH_DIRS="$TCL_LIB_DIR"

#----------------------------------------------------------------------------
#       Source the tcl/tk config shells
#----------------------------------------------------------------------------

for f in tclConfig.sh tkConfig.sh ; do
  if test -r "$TCL_LIB_DIR/$f" ; then
    . $TCL_LIB_DIR/$f
    AC_MSG_RESULT(using $TCL_LIB_DIR/$f)
  fi
done

if test -z "$TCL_VERSION" ; then
  AC_ERROR(could not find tclConfig.sh in $TCL_LIB_DIR)
else
  AC_MSG_RESULT(tclConfig.sh has TCL_VERSION = $TCL_VERSION)
fi


#----------------------------------------------------------------------------
#       Set defaults for optional packages tk and tclx
#       These may get turned off if --enable-shared is requested
#----------------------------------------------------------------------------

tk=yes
AC_ARG_WITH(tk, [  --without-tk              build wish executable (default --with-tk)], tk=$withval,tk=yes)

tclx=no
AC_ARG_WITH(tclx, [  --with-tclx               build with TclX (default --without-tclx)], tclx=$withval,tclx=no)


#----------------------------------------------------------------------------
#       Check where oracle is installed on this system. Honour the options:
#               --with-oracle-directory=DIR
#               --with-oracle-version=x   x=6 or x=7
#----------------------------------------------------------------------------

AC_ARG_WITH(oracle-directory, [  --with-oracle-directory=DIR  Oracle home directory DIR (default=\$ORACLE_HOME)], oracle_directory=$withval)

oratest1=${ORACLE_HOME:-.}
oratest2=`(egrep ^oracle /etc/passwd | awk -F: '{print $6}' ) 2>/dev/null`
oratest2=${oratest2:-.}
oratest3=`(ypcat passwd | egrep ^oracle | awk -F: '{print $6}' ) 2>/dev/null`
oratest3=${oratest3:-.}
oratest4=`(niscat passwd.org_dir | egrep ^oracle | awk -F: '{print $6}') 2>/dev/null`
oratest4=${oratest4:-.}

AC_CHECKING(for Oracle toplevel (libocic.a or libclient.a))
for f in $oracle_directory $oratest1 $oratest2 $oratest3 $oracle4 /opt/oracle /u/oracle /usr/oracle /usr/local/oracle ; do
    AC_MSG_RESULT(looking for rdbms/lib/libocic.a in $f)
    if test -r "$f/rdbms/lib/libocic.a" ; then
        ORA="$f/rdbms/lib"
	ORACLE_HOME=$f
        AC_MSG_RESULT(setting ORA to $ORA)
        break
    fi
    AC_MSG_RESULT(looking for lib/libocic.a in $f)
    if test -r "$f/lib/libocic.a" ; then
        ORA="$f/lib"
	ORACLE_HOME=$f
        AC_MSG_RESULT(setting ORA to $ORA)
        break
    fi
    AC_MSG_RESULT(looking for lib/libclient.a in $f)
    if test -r "$f/lib/libclient8.a" ; then
        ORA="$f/lib"
	ORACLE_HOME=$f
        AC_MSG_RESULT(setting ORA to $ORA)
	testver=7
        break
    fi
done

if test -z "$ORA" ; then
    AC_MSG_RESULT(you can adjust the oracle path to your directory with)
    AC_MSG_RESULT(your oracle distribution:)
    AC_MSG_RESULT(        --with-oracle-directory=DIR)
    AC_ERROR(Could not find the oracle home header/library files.)
fi

SEARCH_DIRS="${SEARCH_DIRS}:${ORA}"

AC_CHECKING(for Oracle version)

if test -z "$testver" ; then
  # test for function odescr(), it is in V7, not in V6

  testver=`(nm $ORA/libocic.a | grep odescr) 2>/dev/null`
  if test -z "$testver" ; then
      testver=6
  else
      testver=7
  fi
fi

AC_MSG_RESULT(found oracle version = $testver)

# check if --with-oracle-version= was specified, otherwise use guess

AC_ARG_WITH(oracle-version, [  --with-oracle-version=x      set Oracle version 6 or 7 (default auto)], oracle_version=$withval, oracle_version=0)

case $oracle_version in
    6)
	AC_MSG_RESULT(using --with-oracle-version = $oracle_version)
        ORAVER="-DVERSION6"
	;;
    7)
	AC_MSG_RESULT(using --with-oracle-version = $oracle_version)
	ORAVER="-DVERSION7"
	;;
esac
		
if test -z "$ORAVER" ; then
  case $testver in
      6)
	  ORAVER="-DVERSION6"
	  ;;
      7)
	  ORAVER="-DVERSION7"
	  ;;
  esac
fi

ORA72FEATURES=""

# get the other libs oracle might need to link
OTHER_ORA_LIBS=`cat -s $ORACLE_HOME/rdbms/lib/sysliblist`
# list of unwanted oracle objects
OCIIGNORE=""

case $ORAVER in
  -DVERSION6)
    OCILIB='${ORA}/libocic.a ${ORA}/osntab.o ${ORA}/libsqlnet.a ${ORA}/libora.a'
    OCIMILI=''
  ;;
  -DVERSION7)
    # V7.1 might have even more libs; solaris needs __fstd.o, aix needs mili.exp
   for oraxlib in libnlsrtl.a libnlsrtl3.a __fstd.o mili.exp libosn.a libasync.a
    do 
      AC_CHECKING(for Oracle extra library/object $oraxlib)
      varname=`basename $oraxlib .a`
      varname=`basename $varname .o`
      varname=`basename $varname .exp`
      if test -r "$ORA/$oraxlib" ; then
        eval $varname='\${ORA}/$oraxlib'
        AC_MSG_RESULT(  found)
      else
  	eval $varname=''
        AC_MSG_RESULT(  none)
      fi
    done
    if test "$mili"  ; then
      OCIMILI="-bI:$ORA/mili.exp"
    else
      OCIMILI=''
    fi

    # Oracle 7 has different libs for 7.0.x & 7.1.x, 7.2.x
    # and now 7.3.x   sheesh!!!  can't Oracle make up their mind!!

    if test -r "$ORA/libclient8.a" ; then
      # it's Oracle 7.3.2
        OCIIGNORE="snmspu.o snmsp.o snmd.o snmitcl.o"
        AC_MSG_RESULT(using oracle libraries for 7.3.2+)
        ORA72FEATURES="-DNONBLOCK_CURSOR"
        OCILIB="$mili \${ORA}/libclient8.a \${ORA}/libcommon.a \${ORA}/libgeneric.a  \${ORA}/libsqlnet.a \${ORA}/libncr.a \${ORA}/libepc.a  \${ORA}/libcore3.a $libnlsrtl3 \${ORA}/libc3v6.a $__fstd $libosn $libasync"
    else
      # it's either 7.2 or 7.0/7.1
      testfeat=`(nm $ORA/libocic.a | grep onbclr) 2>/dev/null`
      if test -z "$testfeat" ; then
        # looks like Oracle 7.0.x or 7.1.x
        ORA72FEATURES=""
        AC_MSG_RESULT(using oracle libraries for 7.0.x/7.1.x)
        OCILIB="$mili \${ORA}/libocic.a \${ORA}/osntab.o \${ORA}/libsqlnet.a \${ORA}/libora.a   \${ORA}/libcore.a $libnlsrtl \${ORA}/libcv6.a \${ORA}/libnetwork.a $__fstd $libosn $libasync"
      else
        # looks like Oracle 7.2
        ORA72FEATURES="-DNONBLOCK_CURSOR"
        AC_MSG_RESULT(using oracle libraries for 7.2.x)
        OCILIB="$mili \${ORA}/libocic.a \${ORA}/osntab.o \${ORA}/libsqlnet.a \${ORA}/libora.a   \${ORA}/libcore3.a $libnlsrtl3 \${ORA}/libc3v6.a $__fstd $libosn $libasync"
      fi

    fi

  ;;
esac

if test "$ORA72FEATURES" ; then
  AC_MSG_RESULT(using oracle 7.2+ features: nonblocking and cursor variables)
fi

#----------------------------------------------------------------------------
#       Check for libraries need by sybase on some systems
#       borrowed from Tcl 7.6 configure.in
#----------------------------------------------------------------------------
 
tcl_checkBoth=0
AC_CHECK_FUNC(connect, tcl_checkSocket=0, tcl_checkSocket=1)
if test "$tcl_checkSocket" = 1; then
    AC_CHECK_LIB(socket, main, ORA_SYS_LIBS="$ORA_SYS_LIBS -lsocket", tcl_checkBoth=1)
fi
if test "$tcl_checkBoth" = 1; then
    tk_oldLibs=$ORA_SYS_LIBS
    ORA_SYS_LIBS="$ORA_SYS_LIBS -lsocket -lnsl"
    AC_CHECK_FUNC(accept, tcl_checkNsl=0, [ORA_SYS_LIBS=$tk_oldLibs])
fi
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, main, [ORA_SYS_LIBS="$ORA_SYS_LIBS -lnsl"]))

AC_CHECK_LIB(dnet_stub, main, [ORA_SYS_LIBS="$ORA_SYS_LIBS -ldnet_stub"])


#----------------------------------------------------------------------------
#       Check whether shared library should be built
#----------------------------------------------------------------------------

ORA_SH_LIB=""
shared=yes
AC_ARG_ENABLE(shared, [  --disable-shared             build static lib & exe (default --enable-shared)], [shared=$enableval], [shared=yes])

if test "$shared" = "yes" -a "${TCL_SHLIB_SUFFIX}" != ""; then
  if test -r "$ORA/liboracle${TCL_SHLIB_SUFFIX}" -o -r "$ORA/libclntsh${TCL_SHLIB_SUFFIX}" ; then
    RANLIB=':'
    AC_MSG_RESULT(will build shared library)
    if test -r "$ORA/liboracle${TCL_SHLIB_SUFFIX}" ; then
      # 7.1 shared library name
      ORA_SH_LIB="-loracle"
    else
      # 7.3 shared library name
      ORA_SH_LIB="-lclntsh"
    fi
  else
    AC_MSG_RESULT(--enable-shared requested but Oracle shared library not found as)
    AC_MSG_RESULT($ORA/liboracle${TCL_SHLIB_SUFFIX} or)
    AC_MSG_RESULT($ORA/libclntsh${TCL_SHLIB_SUFFIX})
    AC_MSG_RESULT(Building a dynamic shared library for Oratcl is not possible.)
    AC_MSG_RESULT(Check the INSTALL file for more information,)
    AC_MSG_ERROR(halted.)
  fi
else
  if test "$shared" = "yes" ; then
    AC_MSG_RESULT(--enable-shared requested but Tcl is not configure for dynamic loading.)
    AC_MSG_RESULT(Building a dynamic shared library for Oratcl is not possible.)
    AC_MSG_RESULT(reconfigure Oratcl with --disable-shared or)
    AC_MSG_RESULT(reconfigure Tcl with --enable-shared.)
    AC_MSG_ERROR(halted.)
  else
    AC_MSG_RESULT(will build static library and executables)
  fi
fi

if test "$shared" = "yes" ; then
  PROGS="libOratcl${ORATCL_LIBVER}${TCL_SHLIB_SUFFIX}"
  INSTALL_TARGET=install-shared
  tk=no
  tclx=no
else
  INSTALL_TARGET=install-static
fi


#----------------------------------------------------------------------------
#       Check if tk is installed.  Honour the options:
#               --with-tk=yes|no  --without-tk  (default to yes)
#----------------------------------------------------------------------------

if test "$tk" = "yes" ; then
  if test -z "$TK_VERSION" ; then
    AC_ERROR(could not find tkConfig.sh in $TCL_LIB_DIR)
  else
    AC_MSG_RESULT(tkConfig.sh has TK_VERSION = $TK_VERSION)
    X11_LIB_DIR=`echo ${TK_XLIBSW} |sed -e "s|-L||" -e "s/ .*$//g" -e "s/-l.*$//g"`
    if test "x${X11_LIB_DIR}" != "x" ; then
      SEARCH_DIRS="${SEARCH_DIRS}:${X11_LIB_DIR}"
    fi
  fi
else
  tk=''
fi


#----------------------------------------------------------------------------
#       Check if tclx is installed on this system. Honour the options:
#               --with-tclx=yes|no  --without-tclx  (default to yes)
#----------------------------------------------------------------------------

TCLX_LIB_SPEC=''
TCLX_LIBS=''
TKX_LIB_SPEC=''
TKX_LIBS=''
TCLX_FULL_VERSION=''
TKX_FULL_VERSION=''
TCLX_LIB_LOC=""
TCLX_LIB_SEP="/"

if test "$tclx" = "yes" ; then
  for f in tclxConfig.sh tkxConfig.sh ; do
    if test -r "$TCL_LIB_DIR/$f" ; then
      . $TCL_LIB_DIR/$f
    fi
  done

  if test -z "$TCLX_FULL_VERSION" ; then
    AC_ERROR(could not find tclxConfig.sh in $TCL_LIB_DIR)
  else
    AC_MSG_RESULT(tclxConfig.sh has TCLX_FULL_VERSION = $TCLX_FULL_VERSION)
 
    # check if TCLX_LIB_SPEC defined in tclxConfig.sh; if not, build our own
    if test "$TCLX_LIB_SPEC" = "" ; then
      TCLX_LIB_SPEC='-ltclx'
    fi
  fi

  if test -z "$TKX_FULL_VERSION" -a "$tk" = "yes" ; then
    AC_ERROR(could not find tkxConfig.sh in $TCL_LIB_DIR)
  else
    AC_MSG_RESULT(tkxConfig.sh has TKX_FULL_VERSION = $TKX_FULL_VERSION)
 
    # check if TKX_LIB_SPEC defined in tkxConfig.sh; if not, build our own
    if test "$TKX_LIB_SPEC" = "" ; then
      TKX_LIB_SPEC='-ltkx'
    fi
  fi

  # tclx installs its appinit.c in lib/ tree for tclx7.6.0b2+
  if test -r "$TCL_PREFIX/lib/tclX$TCLX_FULL_VERSION/tclXAppInit.c" ; then
    TCLX_LIB_LOC="lib/"
    TCLX_LIB_SEP=""
  else
    # tclx7.6.0b1 slightly different
    if test -r "$TCL_PREFIX/lib/tclX/$TCLX_FULL_VERSION/tclXAppInit.c" ; then
      TCLX_LIB_LOC="lib/"
      TCLX_LIB_SEP="/"
    fi
    #else it must be tclx7.5.2-, defaults should work
  fi

  T_EXEC='${TCLX_EXEC}'
  if test "$tk" ; then
    W_EXEC='${WISHX_EXEC}'
  else
    W_EXEC=''
  fi

else

  # no tclx, set defines for standard tcl/tk

  T_EXEC='${TCL_EXEC}'
  if test "$tk" ; then
    W_EXEC='${WISH_EXEC}'
  else
    W_EXEC=''
  fi
fi

# change PROGS target if not building shared lib
 
if test "$shared" != "yes" ; then
  PROGS="${T_EXEC} ${W_EXEC}"
fi


#--------------------------------------------------------------------
#       sigh. TCL_LD_SEARCH_FLAGS is built for cc, we need one built for
#       ld.  try to coerce '-Wl,<flag>,<parm>' into just '<flag> <parm>'
#       other flags are left as is.
#--------------------------------------------------------------------
 
OTHER_LD_SEARCH_FLAGS=''
SPACE=''
for arg in $TCL_LD_SEARCH_FLAGS
do
  newarg=''
  case $arg in
    -Wl*)
      IFS_SAVE=$IFS
      IFS=,
      for p in $arg
      do
        case $p in
          -Wl)
            ;;
          *)
            newarg="$newarg $p"
            ;;
        esac
      done
      IFS=$IFS_SAVE
      ;;
    *)
      newarg=$arg
      ;;
  esac
  OTHER_LD_SEARCH_FLAGS="${OTHER_LD_SEARCH_FLAGS}${SPACE}${newarg}"
  SPACE=' '
done

#--------------------------------------------------------------------
#  	Substitute all variables for Makefile
#--------------------------------------------------------------------

# fix TCL_LIB_DIR to platform independent lib dir
TCL_LIB_DIR=$TCL_PREFIX/lib

AC_SUBST(T_EXEC)
AC_SUBST(W_EXEC)
AC_SUBST(PROGS)
AC_SUBST(INSTALL_TARGET)
AC_SUBST(ORATCL_VERSION)
AC_SUBST(ORATCL_LIBVER)
AC_SUBST(ORACLE_HOME)
AC_SUBST(ORA)
AC_SUBST(ORAVER)
AC_SUBST(ORA72FEATURES)
AC_SUBST(OCILIB)
AC_SUBST(OCIMILI)
AC_SUBST(OCIIGNORE)
AC_SUBST(ORA_SH_LIB)
AC_SUBST(OTHER_ORA_LIBS)
AC_SUBST(TCL_VERSION)
AC_SUBST(TK_VERSION)
AC_SUBST(TCL_PREFIX)
AC_SUBST(TCL_EXEC_PREFIX)
AC_SUBST(TCL_INC_DIR)
AC_SUBST(TCL_LIB_DIR)
AC_SUBST(TCL_LIB_SPEC)
AC_SUBST(TCL_LIBS)
AC_SUBST(TK_LIB_SPEC)
AC_SUBST(TK_LIBS)
AC_SUBST(TK_XINCLUDES)
AC_SUBST(TK_XLIBSW)
AC_SUBST(TCLX_LIB_SPEC)
AC_SUBST(TCLX_LIBS)
AC_SUBST(TCLX_LIB_LOC)
AC_SUBST(TCLX_LIB_SEP)
AC_SUBST(TKX_LIB_SPEC)
AC_SUBST(TKX_LIBS)
AC_SUBST(TCLX_FULL_VERSION)
AC_SUBST(TKX_FULL_VERSION)
AC_SUBST(TCL_CC)
AC_SUBST(TCL_DEFS)
AC_SUBST(TCL_LD_FLAGS)
AC_SUBST(TCL_LD_SEARCH_FLAGS)
AC_SUBST(TCL_SHLIB_SUFFIX)
AC_SUBST(TCL_SHLIB_CFLAGS)
AC_SUBST(TCL_SHLIB_LD)
AC_SUBST(TCL_SHLIB_LD_LIBS)
AC_SUBST(SEARCH_DIRS)
AC_SUBST(OTHER_LD_SEARCH_FLAGS)
AC_SUBST(ORA_SYS_LIBS)

AC_OUTPUT(Makefile pkgIndex.tcl)

