dnl standard setup for vic/vat/etc. autoconf scripts.
dnl $Header: /cvsroot/nsnam/conf/configure.in.head,v 1.30 2008/11/03 05:34:48 tom_henderson Exp $ (LBL)


AC_ARG_WITH(defaultoptions, -with-defaultoptions[=filename]	use <filename> as default options file, , with_defaultoptions=".configure")

if test "$with_defaultoptions" = "yes" ; then
	with_defaultoptions=".configure"
elif test "$with_defaultoptions" = "no" ; then
	with_defaultoptions=""
fi

if test -n "$with_defaultoptions" ; then
	if test -f "$with_defaultoptions" ; then
		read arglist < $with_defaultoptions
	
		if test -n "$arglist" ; then 
			arguments="$0 $arglist $* --without-defaultoptions"
			echo "Restarting: $arguments" 
			exec $arguments
		fi
	else
		if test "$with_defaultoptions" = ".configure" ; then
			echo No .configure file found in current directory
			echo Continuing with default options...
		else
			echo Cannot find file $with_defaultoptions
			echo Aborting configure...
			exit 1
		fi
	fi
fi


AC_CANONICAL_SYSTEM

AC_PROG_CC
AC_PROG_CXX
AC_STDC_HEADERS
AC_HAVE_HEADERS(string.h)

V_INCLUDE=""
V_LIB=""
V_OBJ=""
V_BROKEN_OBJ="strtol.o strtoul.o"

V_SHELL=""
V_TARCMD="tar cfh"
V_SIGRET="void"

AC_CHECK_LIB(Xbsd, main, [V_LIB="$V_LIB -lXbsd"])
AC_CHECK_LIB(socket, socket, [V_LIB="$V_LIB -lsocket"])
AC_CHECK_LIB(nsl, gethostbyname, [V_LIB="$V_LIB -lnsl"])
AC_CHECK_LIB(intl, dcgettext, [V_LIB="$V_LIB -lintl"])
AC_CHECK_LIB(dnet_stub, getnodebyname, [V_LIB="$V_LIB -ldnet_stub"])

V_TAR_EXTRA=""
V_DEFINE=""

V_RANLIB=ranlib
V_AR="ar cr"

#XXX
V_SHM="-DUSE_SHM"

AC_ARG_ENABLE(release,   --enable-release	do a release build, , enable_release="no")
AC_ARG_ENABLE(debug,   --enable-debug		build with debugging enabled, , enable_debug="no")
AC_ARG_ENABLE(devel,   --enable-devel		do a development build, , enable_devel="no")

if test "$enable_devel" = "yes" ; then
	enable_debug="yes"
fi

if test -f .devel -o "$enable_devel" = "yes"; then
	OonS=""
else
    if test "$CC" = gcc ; then
	    AC_MSG_CHECKING(that $CXX can handle -O2)
	    AC_TRY_COMPILE(,
		#if __GNUC__ < 2  || __GNUC_MINOR__ < 8
		/* gcc */
		error
		#endif
		#if __GNUC_MINOR__ < 92
		/* egcs */
		int error;
		#endif
	    ,AC_MSG_RESULT(yes)
	    OonS="-O2",			# Optimize on Steroids
	    AC_MSG_RESULT(no))
    fi
fi

if test "$enable_debug" = "yes" ; then
	V_CCOPT="-g"
	if test "$CC" = gcc ; then
		V_CCOPT="$V_CCOPT -Wall -Wno-write-strings -Wno-parentheses -Werror"
		V_DEFINE="$V_DEFINE -fsigned-char -fno-inline"
	fi
else
	V_CCOPT="$OonS"
	V_DEFINE="$V_DEFINE -DNDEBUG"
	if test "$CC" = gcc ; then
		V_CCOPT="$V_CCOPT -Wall -Wno-write-strings"
	fi
fi

# XXX Some stupid sh on solaris does not set PWD correctly, i.e.,
# after chdir $PWD remains at the parent directory. :( 
# We'll just do this every time. Doesn't hurt anyway.
PWD=`pwd`

solaris=""
if test `echo "$target_os" | sed 's/\..*//'` = solaris2 ; then
	solaris="yes"
fi

#places="Tcl \
#	/usr/src/local/Tcl \
#	../../Tcl \
#	../Tcl"
#for d in $places; do
#	if test -f $d/tclcl.h ; then
#		V_LIB="$V_LIB $d/libTcl.a"
#		V_INCLUDE="$V_INCLUDE -I$d"
#    		break
#	fi
#done


# we now default to non-static linking, although if the "magic" file
# .devel exists in the current directory, we try for a static link
# under the assumption we are trying to produce re-distributable
# binaries.
#
# Yatin: Moved this code here from configure.in.tail, since the mash 
# configure.in file needs to set this variable appropriately before invoking
# configure.in.tail and the presence of the .devel file can mess things up for
# the linux release build
#
AC_ARG_ENABLE(static,    --enable-static	enable/disable static building, , enable_static="")

if test -f .devel -o "$enable_devel" = "yes"; then
	echo -n "Development version: considering static"
	dnl default to static on in development versions
	if test "$enable_static" != no; then
		echo ", and static enabled"
		V_STATIC="-static"
	else
		echo ", but static disabled anyway"
	fi
else
	V_STATIC=""
fi


# This can be extended to support compilation-time module selection
V_STLOBJ=""
V_LSSCRIPT=""

# This is required by configure.in.tcl to provide absolute pathnames for 
# tclsh, and configure.in.tail to absolutize V_INCLUDES and V_LIBS.
absolutize() {
    case $1 in 
    -L*) p=`echo $1 | sed 's/^-L//'` ;;
    -I*) p=`echo $1 | sed 's/^-I//'` ;;
    *) p=$1 ;;
    esac
    d=`dirname $p`
    f=`basename $p`
    ad=`(
	cd $d
	pwd
    )`
    case $1 in
    -L*) echo -L$ad/$f ;;
    -I*) echo -I$ad/$f ;;
    *) echo $ad/$f ;;
    esac
}
