dnl  
dnl configure.in script for Oratcl.  directory checking parts borrowed from
dnl Juergen Schoenwaelder (scotty distribution)
dnl Tom Poindexter
dnl

AC_INIT(oratcl.c)
CC=${CC-cc}
AC_SUBST(CC)
AC_PROG_INSTALL
AC_PROG_RANLIB

#--------------------------------------------------------------------
#       Check for the existence of various libraries.  Pulled out of
#       Tk distribution.
#--------------------------------------------------------------------
 
tk_both=1
AC_FUNC_CHECK(connect, tk_both=0)
AC_FUNC_CHECK(gethostbyname, tk_both=0)
tk_oldLibs=$LIBS
LIBS="$LIBS -lsocket"
AC_CHECK_LIB(nsl, , tk_both=0)
LIBS=$tk_oldLibs
if test "$tk_both" = 1; then
    echo "need both libraries"
    LIBS="$LIBS -lsocket -lnsl"
else
    AC_FUNC_CHECK(connect, , AC_CHECK_LIB(socket, [LIBS="$LIBS -lsocket"]))
    AC_FUNC_CHECK(gethostbyname, , AC_CHECK_LIB(nsl, [LIBS="$LIBS -lnsl"]))
fi
 

#----------------------------------------------------------------------------
#       Check if tcl is installed on this system. Honour the options:
#               --with-tcl-directory=path
#               --with-tcl-library=path
#----------------------------------------------------------------------------

AC_ARG_WITH(tcl-directory, tcl-directory, tcl_directory=$withval)
AC_ARG_WITH(tcl-library, tcl-library, tcl_library=$withval)

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

AC_CHECKING(for Tcl toplevel (tclInt.h))
for f in $tcl_directory ../tcl8* ../tcl7.6* ../tcl7.5* ../tcl7.4* ../tcl* $prefix/include /usr/local/include /usr/local/include/tcl /usr/include /usr/include/tcl ; do
    AC_MSG_RESULT(looking for tclInt.h in $f)
    if test -f "$f/tclInt.h" ; then
        TCL_DIR="$f"
        AC_MSG_RESULT(setting TCL_DIR to $TCL_DIR)
        break
    fi
done

AC_CHECKING(for libtcl)
for f in $tcl_library ../tcl8* ../tcl7.6* ../tcl7.5* ../tcl7.4* ../tcl* $prefix/lib /usr/local/lib /usr/lib ; do
    AC_MSG_RESULT(looking for libtcl in $f)
    if test -f "$f/libtcl.a" ; then
        TCL_LIB="-L$f -ltcl"
        AC_MSG_RESULT(setting TCL_LIB to $TCL_LIB)
        break
    fi
done

if test -z "$TCL_DIR" ; then
    AC_MSG_RESULT(You can adjust the tcl directory path to your directory)
    AC_MSG_RESULT(containing tclInt.h (and libtcl.a) )
    AC_MSG_RESULT(using the following configure options:)
    AC_MSG_RESULT(        --with-tcl-directory=<path>)
    AC_MSG_RESULT(        --with-tcl-library=<path>)
    if test -z "$tcl_directory" ; then
      AC_ERROR(Could not find the tcl source directory files.)
    else
      TCL_DIR=$tcl_directory
      AC_MSG_RESULT(WARNING: Could not find the tcl source directory using )
      AC_MSG_RESULT(         explicit -with-tcl-directory value.  It is)
      AC_MSG_RESULT(         likely that make will fail.  Rerun configure with)
      AC_MSG_RESULT(         the proper -with-tcl-directory value, or edit the Makefile.)
      AC_MSG_RESULT(         setting TCL_DIR to $TCL_DIR)
    fi
fi
if test -z "$TCL_LIB" ; then
    AC_MSG_RESULT(You can adjust the tcl library path to your directory)
    AC_MSG_RESULT(containing libtcl.a (and tcl source) )
    AC_MSG_RESULT(using the following configure options:)
    AC_MSG_RESULT(        --with-tcl-directory=<path>)
    AC_MSG_RESULT(        --with-tcl-library=<path>)
    if test -z "$tcl_library" ; then
      AC_ERROR(Could not find the tcl library archive libtcl.a.)
    else
      TCL_LIB="-L$tcl_library -ltcl"
      AC_MSG_RESULT(WARNING: Could not find the tcl library libtcl.a using )
      AC_MSG_RESULT(         explicit -with-tcl-library value.  It is)
      AC_MSG_RESULT(         likely that make will fail.  Rerun configure with)
      AC_MSG_RESULT(         the proper -with-tcl-library value, or edit the Makefile.)
      AC_MSG_RESULT(         setting TCL_LIB to $TCL_LIB)
    fi
fi

AC_SUBST(TCL_DIR)
AC_SUBST(TCL_LIB)


#----------------------------------------------------------------------------
#       Check if tk is installed on this system. Honour the options:
#               --with-tk-directory=path
#               --with-tk-library=path
#----------------------------------------------------------------------------

AC_ARG_WITH(tk-directory, tk-directory, tk_directory=$withval)
AC_ARG_WITH(tk-library, tk-library, tk_library=$withval)

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

AC_CHECKING(for Tk toplevel (tkInt.h))
for f in $tk_directory ../tk5* ../tk4.3* ../tk4.2* ../tk4.1* ../tk4* ../tk* $prefix/include /usr/local/include /usr/local/include/tcl /usr/include /usr/include/tcl ; do
    AC_MSG_RESULT(looking for tkInt.h in $f)
    if test -f "$f/tkInt.h" ; then
        TK_DIR="$f"
        AC_MSG_RESULT(setting TK_DIR to $TK_DIR)
        break
    fi
done

AC_CHECKING(for libtk)
for f in $tk_library .../tk5* ../tk4.3* ../tk4.2* ../tk4.1* ../tk4* ../tk* $prefix/lib /usr/local/lib /usr/lib ; do
    AC_MSG_RESULT(looking for libtk in $f)
    if test -f "$f/libtk.a" ; then
        TK_LIB="-L$f -ltk"
        AC_MSG_RESULT(setting TK_LIB to $TK_LIB)
        break
    fi
done

if test -z "$TK_DIR" ; then
    AC_MSG_RESULT(You can adjust the tk directory path to your directory)
    AC_MSG_RESULT(containing tkInt.h (and libtk.a) )
    AC_MSG_RESULT(using the following configure options:)
    AC_MSG_RESULT(        --with-tk-directory=<path>)
    AC_MSG_RESULT(        --with-tk-library=<path>)
    if test -z "$tk_directory" ; then
      AC_ERROR(Could not find the tk source directory files.)
    else
      TK_DIR=$tk_directory
      AC_MSG_RESULT(WARNING: Could not find the tk source directory using )
      AC_MSG_RESULT(         explicit -with-tk-directory value.  It is)
      AC_MSG_RESULT(         likely that make will fail.  Rerun configure with)
      AC_MSG_RESULT(         the proper -with-tk-directory value, or edit the Makefile.)
      AC_MSG_RESULT(         setting TK_DIR to $TK_DIR)
    fi
fi
if test -z "$TK_LIB" ; then
    AC_MSG_RESULT(You can adjust the tk library path to your directory)
    AC_MSG_RESULT(containing libtk.a (and tk source) )
    AC_MSG_RESULT(using the following configure options:)
    AC_MSG_RESULT(        --with-tk-directory=<path>)
    AC_MSG_RESULT(        --with-tk-library=<path>)
    if test -z "$tk_library" ; then
      AC_ERROR(Could not find the tk library archive libtk.a.)
    else
      TK_LIB="-L$tk_library -ltk"
      AC_MSG_RESULT(WARNING: Could not find the tk library libtk.a using )
      AC_MSG_RESULT(         explicit -with-tk-library value.  It is)
      AC_MSG_RESULT(         likely that make will fail.  Rerun configure with)
      AC_MSG_RESULT(         the proper -with-tk-library value, or edit the Makefile.)
      AC_MSG_RESULT(         setting TK_LIB to $TK_LIB)
    fi
fi

AC_SUBST(TK_DIR)
AC_SUBST(TK_LIB)



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

AC_ARG_WITH(tclx, tclx, tclx=$withval,tclx=yes)
AC_ARG_WITH(tclx-directory, tclx-directory, tclx_directory=$withval)
AC_ARG_WITH(tclx-library, tclx-library, tclx_library=$withval)

if test "$tclx" = "yes" ; then

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

  AC_CHECKING(for TclX toplevel (src/tclExtdInt.h))
  for f in $tclx_directory ../tclX8* ../tclX7.6* ../tclX7.5* ../tclX7.4* ../tclX* $prefix/include /usr/local/include /usr/local/include/tcl /usr/include /usr/include/tcl ; do
      AC_MSG_RESULT(looking for src/tclExtdInt.h in $f)
      if test -f "$f/src/tclExtdInt.h" ; then
	  TCLX_DIR="$f"
	  AC_MSG_RESULT(setting TCLX_DIR to $TCLX_DIR)
	  break
      fi
  done

  AC_CHECKING(for libtclx)
  for f in $tclx_library ../tclX8* ../tclX7.6* ../tclX7.5* ../tclX7.4* ../tclX* $prefix/lib /usr/local/lib /usr/lib ; do
      AC_MSG_RESULT(looking for src/libtclx in $f)
      if test -f "$f/src/libtclx.a" ; then
	  TCLX_LIB="-L$f/src -ltclx"
	  AC_MSG_RESULT(setting TCLX_LIB to $TCLX_LIB)
	  break
      fi
      AC_MSG_RESULT(looking for libtclx in $f)
      if test -f "$f/libtclx.a" ; then
	  TCLX_LIB="-L$f -ltclx"
	  AC_MSG_RESULT(setting TCLX_LIB to $TCLX_LIB)
	  break
      fi
  done

  AC_CHECKING(for libtkx)
  for f in $tclx_library ../tclX8* ../tclX7.6* ../tclX7.5* ../tclX7.4* ../tclX* $prefix/lib /usr/local/lib /usr/lib ; do
      AC_MSG_RESULT(looking for tksrc/libtkx in $f)
      if test -f "$f/tksrc/libtkx.a" ; then
	  TKX_LIB="-L$f/tksrc -ltkx"
	  AC_MSG_RESULT(setting TKX_LIB to $TKX_LIB)
	  break
      fi
      AC_MSG_RESULT(looking for libtkx in $f)
      if test -f "$f/libtkx.a" ; then
	  TKX_LIB="-L$f -ltkx"
	  AC_MSG_RESULT(setting TKX_LIB to $TKX_LIB)
	  break
      fi
  done

  if test -z "$TCLX_DIR" ; then
      AC_MSG_RESULT(You can adjust the tclx directory path to your directory)
      AC_MSG_RESULT(containing src/tclExtndInt.h (and libtclx.a) )
      AC_MSG_RESULT(using the following configure options:)
      AC_MSG_RESULT(        --with-tclx-directory=<path>)
      AC_MSG_RESULT(        --with-tclx-library=<path>)
      if test -z "$tclx_directory" ; then
	AC_ERROR(Could not find the tclx source directory files.)
      else
	TCLX_DIR=$tcl_directory
	AC_MSG_RESULT(WARNING: Could not find the tclx source directory using )
	AC_MSG_RESULT(         explicit -with-tclx-directory value.  It is)
	AC_MSG_RESULT(         likely that make will fail.  Rerun configure with)
	AC_MSG_RESULT(         the proper -with-tclx-directory value, or edit the Makefile.)
	AC_MSG_RESULT(         setting TCLX_DIR to $TCLX_DIR)
      fi
  fi
  if test -z "$TCLX_LIB" ; then
      AC_MSG_RESULT(You can adjust the tclx library path to your directory)
      AC_MSG_RESULT(containing src/libtclx.a (and tclx source) )
      AC_MSG_RESULT(using the following configure options:)
      AC_MSG_RESULT(        --with-tclx-directory=<path>)
      AC_MSG_RESULT(        --with-tclx-library=<path>)
      if test -z "$tclx_library" ; then
	AC_ERROR(Could not find the tclx library archive libtclx.a.)
      else
	TCLX_LIB="-L$tclx_library -ltclx"
	AC_MSG_RESULT(WARNING: Could not find the tclx library libtclx.a using )
	AC_MSG_RESULT(         explicit -with-tclx-library value.  It is)
	AC_MSG_RESULT(         likely that make will fail.  Rerun configure with)
	AC_MSG_RESULT(         the proper -with-tclx-library value, or edit the Makefile.)
	AC_MSG_RESULT(         setting TCLX_LIB to $TCLX_LIB)
      fi
  fi
  if test -z "$TKX_LIB" ; then
      AC_MSG_RESULT(You can adjust the tkx library path to your directory)
      AC_MSG_RESULT(containing tksrc/libtkx.a (and tclx source) )
      AC_MSG_RESULT(using the following configure options:)
      AC_MSG_RESULT(        --with-tclx-directory=<path>)
      AC_MSG_RESULT(        --with-tclx-library=<path>)
      if test -z "$tclx_library" ; then
	AC_ERROR(Could not find the tclx library archive libtkx.a.)
      else
	TKX_LIB="-L$tclx_library -ltkx"
	AC_MSG_RESULT(WARNING: Could not find the tkx library libtkx.a using )
	AC_MSG_RESULT(         explicit -with-tkx-library value.  It is)
	AC_MSG_RESULT(         likely that make will fail.  Rerun configure with)
	AC_MSG_RESULT(         the proper -with-tclx-library value, or edit the Makefile.)
	AC_MSG_RESULT(         setting TKX_LIB to $TKX_LIB)
      fi
  fi


  T_EXEC='${TCLX_EXEC}'
  W_EXEC='${WISHX_EXEC}'
  PROGS='${T_EXEC} ${W_EXEC}'
  TCLX_SYSLIBS="include $TCLX_DIR/src/tclxlibs.mk"
  TKX_SYSLIBS="include $TCLX_DIR/tksrc/tkxlibs.mk"

else

  # no tclx, set defines for standard tcl/tk

  T_EXEC='${TCL_EXEC}'
  W_EXEC='${WISH_EXEC}'
  PROGS='${T_EXEC} ${W_EXEC}'
  TCLX_DIR=.
  TCLX_LIB=.
  TCLX_SYSLIBS='TCLXLIBS= '
  TKX_SYSLIBS='TKXLIBS= '
fi


AC_SUBST(TCLX_DIR)
AC_SUBST(TCLX_LIB)
AC_SUBST(TKX_LIB)
AC_SUBST(TCLX_SYSLIBS)
AC_SUBST(TKX_SYSLIBS)
AC_SUBST(T_EXEC)
AC_SUBST(W_EXEC)
AC_SUBST(PROGS)

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

AC_ARG_WITH(oracle-directory, oracle-directory, 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 -f "$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 -f "$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 -f "$f/lib/libclient.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=<path>)
    AC_ERROR(Could not find the oracle home header/library files.)
fi

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, oracle-version, 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
    do 
      AC_CHECKING(for Oracle extra library/object $oraxlib)
      varname=`basename $oraxlib .a`
      varname=`basename $varname .o`
      varname=`basename $varname .exp`
      if test -f "$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 less than 7.1.x, 7.2.x
    # and now 7.3.x   sheesh!!!  can't Oracle make up their mind!!

    if test -f "$ORA/libclient.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}/libclient.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"
    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"
      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"
      fi

    fi
  ;;
esac

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


AC_SUBST(ORATCL_VERSION)
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)


#----------------------------------------------------------------------------
#       Create initial tclflags.mk
#----------------------------------------------------------------------------

egrep '^MATH_LIBS'         $TCL_DIR/Makefile    >tclflags.mk
egrep '^[A][C]_FLAGS'      $TCL_DIR/Makefile   >>tclflags.mk
egrep '^PROTO_FLAGS'       $TCL_DIR/Makefile   >>tclflags.mk
egrep '^MEM_DEBUG_FLAGS'   $TCL_DIR/Makefile   >>tclflags.mk
egrep '^CFLAGS'            $TCL_DIR/Makefile   >>tclflags.mk
egrep '^X11_INCLUDES'      $TK_DIR/Makefile    >>tclflags.mk
egrep '^X11_LIB_SWITCHES'  $TK_DIR/Makefile    >>tclflags.mk
egrep '^LIBS'              $TK_DIR/Makefile | \
    sed -e 's@libtk.a@${TK_DIR}/libtk.a@p'     >>tclflags.mk



AC_OUTPUT(Makefile)

