dnl psqlodbc standalone configuration.
dnl Process this file with autoconf to produce a configure script.

dnl Confirm that this is being run from the ODBC source directory,
dnl and halt if not...
AC_INIT(bind.c)
AC_PREFIX_DEFAULT(/usr/local)

echo "*** configuring psqlodbc ***"

if test -d ../../interfaces
then
	echo "psqlodbc can be configured either standalone or integrated"
	echo "into the Postgres main distribution."
	echo "It appears that you are running the standalone configure"
	echo "from within the main distribution. The recommended (and required)"
	echo "procedure is to configure ODBC using the integrated configuration"
	echo "procedure using the --with-odbc option."
	exit
fi

TEMPLATEDIR=./template
if test "$prefix" = "NONE"
then
	if test -d "/share"
	then
		ODBCINST=/share/odbcinst.ini
	else
		ODBCINST=/etc/odbcinst.ini
	fi
else
	ODBCINST=$prefix/odbcinst.ini
fi

AC_CANONICAL_HOST

case "$host_os" in
 solaris*)
        case "$host_cpu" in
          sparc) os=solaris_sparc ;;
           i386) os=solaris_i386 ;;
        esac ;;
   sunos*) os=sunos4 ;;
     aux*) os=aux ;;
   linux*) os=linux ;;
    bsdi*) os=bsdi ;;
 freebsd*|netbsd*|openbsd*) os=bsd ;;
    dgux*) os=dgux ;;
     aix*) os=aix ;;
nextstep*) os=nextstep ;;
  ultrix*) os=ultrix4 ;;
    irix*) os=irix5  ;;
    hpux*) os=hpux ;;
     osf*) os=alpha ;;
     sco*) os=sco ;;
 machten*) os=machten ;;
  cygwin*) os=win ;;
 sysv4.2*)
       case "$host_vendor" in
               univel) os=univel ;;
                    *) os=unknown ;;
       esac ;;
   sysv4*) os=svr4 ;;
*) echo ""
   echo "*************************************************************"
   echo "configure does not currently recognize your operating system,"
   echo "therefore you must do a manual configuration of:"
   echo "$host_os"
   echo "Please contact scrappy@hub.org to see about rectifying this, "
   echo "including the above 'checking host system type...' line "
   echo "*************************************************************"
   echo ""
   exit;;
esac


PORTNAME=${os}
AC_LINK_FILES(makefiles/Makefile.${os}, Makefile.port)

echo "checking echo setting..."
if echo '\c' | grep -s c >/dev/null 2>&1
then
	ECHO_N="echo -n"
	ECHO_C=""
else
	ECHO_N="echo"
	ECHO_C='\c'
fi

dnl cat <<EOT
dnl **************************************************************
dnl 	Postodbc v0.0250 Installation Program
dnl 
dnl Welcome to the new improved Postodbc installation program.
dnl This configuration program is for version 0.0250 of the
dnl Postodbc software.
dnl 
dnl EOT

dnl this part selects the template from the one in the
dnl template directory. 

dnl if test "X$with_template" != "X"
dnl then
dnl 	TEMPLATE=template/$with_template
dnl else
dnl 	TEMPLATE=DO_NOT_CHANGE_THIS_INVALID_FILENAME
dnl fi

AC_MSG_CHECKING(setting template to)
AC_ARG_WITH(template,
	[  --with-template=TEMPLATE
	                           use operating system template file; see template directory],
	[  TEMPLATE=$withval ],
	[  host_no_ver=`echo "$host" | sed 's/[[0-9.]]*$//'`
		GUESS=`grep "$host_no_ver" $TEMPLATEDIR/.similar | sed 's/.*=//' | tail -1`
		if test "$GUESS"
		then   TEMPLATE="$GUESS"
		else   TEMPLATE=`uname -s | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
		fi
	])
AC_MSG_RESULT($TEMPLATE)

if test ! -d "$TEMPLATEDIR"
then
	cat << EOT
**************************************************************
You seem to be missing the template directory needed by
configure. If you just copied the source from the Postgres
distribution you should instead have issued the command
'make standalone' which would have built a distribution
containing the necessary template directory. The other
option is that your trying to build from the source
which is meant to be placed in the Postgres distribution
directory 'interfaces/odbc'. Depending on your case either get the
standalone source from the following Web site:

	www.insightdist.com/psqlodbc/

or issue the 'make standalone' command from the
'interfaces/odbc' directory of your Postgres Distribution,
or copy the distribution file you have to your Postgres
distribution's 'interfaces/odbc' directory and try
again.

If neither of these is the case than please complain
kindly to the maintainers; their e-mail addresses can
be found in the Readme files.
*************************************************************
EOT

exit
else

if test ! -f "$TEMPLATEDIR/$TEMPLATE"
then
	cat <<EOT
Please select a template from the ones listed below.  If no
template is available, then select the 'generic' one and
consider emailling scrappy@hub.org with the above line which
starts 'checking host system type...'
**************************************************************
EOT
	TEMPLATE=generic
	GUESS=`grep "^$host_no_ver=" $TEMPLATEDIR/.similar 2>/dev/null`
	if test ! "$GUESS"
	then	host_no_ver=`echo "$host" | sed 's/[[0-9.]]*$//'`
		GUESS=`grep "$host_no_ver" $TEMPLATEDIR/.similar 2>/dev/null`
	fi
	if test "$GUESS"
	then
		TEMPLATE=`echo $GUESS | sed 's/.*=//'`
	fi
	export TEMPLATE
	ls $TEMPLATEDIR
	echo "**************************************************************"
	$ECHO_N "Appropriate template file { $TEMPLATE }: $ECHO_C"
	read a
	if test "$a." != "."
	then
		TEMPLATE=$a
	fi
	if test ! -f $TEMPLATEDIR/$TEMPLATE
	then
		echo "You must choose an appropriate template file."
		exit
	fi
fi
fi

TEMPLATE=$TEMPLATEDIR/$TEMPLATE 
export TEMPLATE
echo ""

AROPT=`grep '^AROPT:' $TEMPLATE | awk -F: '{print $2}'`
SHARED_LIB=`grep '^SHARED_LIB:' $TEMPLATE | awk -F: '{print $2}'`
CFLAGS=`grep '^CFLAGS:' $TEMPLATE | awk -F: '{print $2}'`
SRCH_INC=`grep '^SRCH_INC:' $TEMPLATE | awk -F: '{print $2}'`
SRCH_LIB=`grep '^SRCH_LIB:' $TEMPLATE | awk -F: '{print $2}'`
DLSUFFIX=`grep '^DLSUFFIX:' $TEMPLATE | awk -F: '{print $2}'`
DL_LIB=`grep '^DL_LIB:' $TEMPLATE | awk -F: '{print $2}'`
CC=`grep '^CC:' $TEMPLATE | awk -F: '{print $2}'`
LIBS=`grep '^LIBS:' $TEMPLATE | awk -F: '{print $2}'`


a=$SRCH_INC
CPPFLAGS=`echo "$a" | sed 's@  *@ @g; s@^\([[^ ]]\)@-I\1@; s@ \([[^ ]]\)@ -I\1@g'`

export CPPFLAGS
echo "- setting CPPFLAGS=$CPPFLAGS"

a=$SRCH_LIB
LDFLAGS=`echo "$a" | sed 's@  *@ @g; s@^\([[^ ]]\)@-L\1@; s@ \([[^ ]]\)@ -L\1@g'`

export LDFLAGS
echo "- setting LDFLAGS=$LDFLAGS"

dnl Allow for overriding the default location of the odbcinst.ini
dnl file which is normally ${prefix}/share or ${prefix} if this is
dnl being compiled inside the postgres distribution.
AC_MSG_CHECKING(setting ODBCINST)
AC_ARG_WITH(
	odbcinst,
	[  --with-odbcinst=<pathname>
                               change default directory for odbcinst.ini ],
   AC_DEFINE_UNQUOTED(ODBCINST, ${with_odbcinst}) AC_MSG_RESULT($with_odbcinst),
   AC_DEFINE_UNQUOTED(ODBCINST, /etc/odbcinst.ini) AC_MSG_RESULT(${ODBCINST})
)

AC_SUBST(ODBCINST)

if test "X$with_compiler" != "X"
then
        CC=$with_compiler
else
        AC_PROG_CC
fi

AC_CONFIG_HEADER(config.h)

dnl Checks for programs.
AC_PROG_CPP

AC_SUBST(PORTNAME)
AC_SUBST(LDFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(AROPT)
AC_SUBST(SHARED_LIB)
AC_SUBST(CFLAGS)
AC_SUBST(DLSUFFIX)
AC_SUBST(DL_LIB)

INSTALLPATH="`echo /usr/ucb:$PATH | sed 's/:/ /g'`"
AC_PATH_PROGS(INSTALL, ginstall installbsd bsdinst scoinst install, NONE, $INSTALLPATH )
if test $INSTALL = "NONE"
then
   echo "- No Install Script found - aborting."
   exit 0;
fi

INSTLOPTS="-m 444"
INSTL_EXE_OPTS="-m 555"
INSTL_LIB_OPTS="-m 644"
INSTL_SHLIB_OPTS="-m 644"

dnl HPUX wants shared libs to be mode 555.
case "$host_os" in
 hpux*)
	INSTL_SHLIB_OPTS="-m 555" ;;
esac

dnl These flavors of install need -c to install by copy rather than move.
dnl install by move is fatal because it removes stuff from the source tree!
case "`basename $INSTALL`" in
 install|installbsd|scoinst|install-sh)
	INSTLOPTS="-c $INSTLOPTS"
	INSTL_EXE_OPTS="-c $INSTL_EXE_OPTS"
	INSTL_LIB_OPTS="-c $INSTL_LIB_OPTS"
	INSTL_SHLIB_OPTS="-c $INSTL_SHLIB_OPTS";;
esac

echo "- Using $INSTALL"
AC_SUBST(INSTALL)
AC_SUBST(INSTLOPTS)
AC_SUBST(INSTL_LIB_OPTS)
AC_SUBST(INSTL_SHLIB_OPTS)
AC_SUBST(INSTL_EXE_OPTS)

dnl Check the option to echo to inhibit newlines.
ECHO_N_OUT=`echo -n "" | wc -c`
ECHO_C_OUT=`echo "\c" | wc -c`
if test "$ECHO_N_OUT" -eq 0; then
  DASH_N='-n'
  BACKSLASH_C=
else
  if test "ECHO_C_OUT" -eq 0; then
    DASH_N=
    BACKSLASH_C='\\\\c'
  else
    AC_MSG_ERROR("echo behaviour undetermined")
  fi
fi
AC_SUBST(DASH_N)
AC_SUBST(BACKSLASH_C)

AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB
AC_PATH_PROG(find, find)
AC_PATH_PROG(tar, tar)
AC_PATH_PROG(split,split)

AC_CHECK_LIB(c,        main)
AC_CHECK_LIB(m,        main)
AC_CHECK_LIB(dl,       main)

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/param.h pwd.h)

dnl Checks for typedefs, structures, and compiler characteristics.

dnl Check for any "odd" conditions

dnl Checks for library functions.
AC_CHECK_FUNCS(stricmp)

dnl Check for X libraries

dnl Check for X library

AC_OUTPUT(GNUmakefile Makefile.global) 
