dnl This program is free software; you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation; either version 2 of the License, or
dnl (at your option) any later version.
dnl
dnl This program is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl GNU General Public License for more details.
dnl
dnl You should have received a copy of the GNU General Public License
dnl along with this program; if not, write to the Free Software
dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
dnl
dnl Process this file with autoconf to produce a configure script.


AC_INIT([squid_kerb_auth],[1.0.5],[markus_moeller@compuserve.com])
AM_INIT_AUTOMAKE(squid_kerb_auth,1.0.5)
AC_CONFIG_SRCDIR([squid_kerb_auth.c])

AC_PROG_CC
AC_PROG_CPP

AC_TRY_COMPILE([int main()
  { 
     return; 
  } 
])

SPARCV9=""
sys=`uname`
case $sys in
         Linux) w_flag="-Wl,-R"
                w_flag_2=""
		;;
         AIX) w_flag="-Wl,-blibpath:"
              w_flag_2=":/usr/lib:/lib"
              ;;
         SunOS) w_flag="-R"
                w_flag_2=""
		rel=`uname -r`
		case $rel in
		         5.10|5.11) AC_DEFINE(HAVE_NEW_SEAM_KERBEROS,1,[Define to 1 if you have New Solaris 10/OpenSolaris Kerberos])
               		   ;;
         		*) ;;
		esac
                  ;;
         FreeBSD) w_flag="-Wl,-R"
                w_flag_2=""
                ;;
         *) w_flag="-Wl,-rpath"
            w_flag_2=""
            ;;
esac


enable_arg="no"
check_mit() {
       if test "x$ac_krb5_config" = "xyes" ; then
           ac_heimdal=`krb5-config --version 2>/dev/null | grep heimdal`
           if test "x$ac_heimdal" != "x" ; then
               check_heimdal
               return
           fi   
       fi
       AC_DEFINE(HAVE_MIT_KERBEROS,1,[Define to 1 if you have MIT Kerberos])
       ac_gss_libs="resolv com_err des425 k5crypto krb5 gssapi_krb5"
       ac_includedir=""
       ac_libdir=""
       case $sys in
                Linux) if test "x$enableval" != "xyes" -a "x$enableval" != "x" -a "x$enableval" != "xno" ; then
                         ac_libdir=$enableval/lib
                         ac_includedir=$enableval/include
                       else
                         ac_libdir=`rpm -q -l  krb5 2>/dev/null | grep "/libgssapi_krb5" |  sed -e 's/\/libgssapi_krb5.*//' | head -1`
                         ac_includedir=`rpm -q -l krb5-devel 2>/dev/null | grep /krb5.h$ | sed -e 's/\/krb5.h//' | head -1`
                       fi
                       if test "x$ac_includedir" != "x" ; then
                         CPPFLAGS="$CPPFLAGS -I$ac_includedir"
                       else
                         ac_gssapi_cflags=`krb5-config --cflags gssapi 2>/dev/null`
                         if test "x$ac_gssapi_cflags" != "x" ; then
                           CPPFLAGS="$CPPFLAGS $ac_gssapi_cflags"
                         fi
                       fi
                       AC_CHECK_HEADERS(gssapi.h gssapi/gssapi.h gssapi/gssapi_krb5.h gssapi/gssapi_generic.h)
                       if test "x$ac_libdir" != "x" ; then
                         LDFLAGS="$LDFLAGS -L$ac_libdir $w_flag$ac_libdir$w_flag_2"
                         for lib in $ac_gss_libs; do
                           AC_CHECK_LIB($lib,main)
                         done
                       else
                         ac_gssapi_libs=`krb5-config --libs gssapi 2>/dev/null`
                         if test "x$ac_gssapi_libs" != "x" ; then
                           LDFLAGS="$LDFLAGS $ac_gssapi_libs"
                         else
                           for lib in $ac_gss_libs; do
                             AC_CHECK_LIB($lib,main)
                           done
                         fi
                       fi
                       ;;
                *) if test "x$enableval" != "xyes" -a "x$enableval" != "x" -a "x$enableval" != "xno" ; then
                     ac_libdir=$enableval/lib
                     ac_includedir=$enableval/include
                     CPPFLAGS="$CPPFLAGS -I$ac_includedir"
                     AC_CHECK_HEADERS(gssapi.h gssapi/gssapi.h gssapi/gssapi_krb5.h gssapi/gssapi_generic.h)
                     LDFLAGS="$LDFLAGS -L$ac_libdir $w_flag$ac_libdir$w_flag_2"
                     for lib in $ac_gss_libs; do
                        AC_CHECK_LIB($lib,main)
                     done
                   else
                     ac_gssapi_cflags=`krb5-config --cflags gssapi 2>/dev/null`
                     if test "x$ac_gssapi_cflags" != "x" ; then
                         CPPFLAGS="$CPPFLAGS $ac_gssapi_cflags"
                     fi
                     ac_gssapi_libs=`krb5-config --libs gssapi 2>/dev/null`
                     if test "x$ac_gssapi_libs" != "x" ; then
                         LDFLAGS="$LDFLAGS $ac_gssapi_libs"
                     else
                         for lib in $ac_gss_libs; do
                            AC_CHECK_LIB($lib,main)
                         done
                     fi
                     AC_CHECK_HEADERS(gssapi.h gssapi/gssapi.h gssapi/gssapi_krb5.h gssapi/gssapi_generic.h)
                   fi
                   ;;
       esac
       enable_arg="mit"
}
check_heimdal(){
       if test "x$ac_krb5_config" = "xyes" ; then
           ac_heimdal=`krb5-config --version 2>/dev/null | grep heimdal`
           if test "x$ac_heimdal" = "x" ; then
               check_mit
               return
           fi
       fi
       AC_DEFINE(HAVE_HEIMDAL_KERBEROS,1,[Define to 1 if you have Heimdal Kerberos])
       ac_gss_libs="resolv crypto des crypt roken com_err asn1 krb5 gssapi"
       ac_includedir=""
       ac_libdir=""
       case $sys in
                Linux) if test "x$enableval" != "xyes" -a "x$enableval" != "x" -a "x$enableval" != "xno" ; then
                         ac_libdir=$enableval/lib
                         ac_includedir=$enableval/include
                       else
                         ac_libdir=`rpm -q -l  heimdal-devel 2>/dev/null | grep "/libroken" |  sed -e 's/\/libroken.*//' | head -1`
                         ac_includedir=`rpm -q -l heimdal-devel 2>/dev/null | grep /krb5.h$ | sed -e 's/\/krb5.h//' | head -1`
                       fi
                       if test "x$ac_includedir" != "x" ; then
                          CPPFLAGS="$CPPFLAGS -I$ac_includedir"
                       else
                         ac_gssapi_cflags=`krb5-config --cflags gssapi 2>/dev/null`
                         if test "x$ac_gssapi_cflags" != "x" ; then
                           CPPFLAGS="$CPPFLAGS $ac_gssapi_cflags"
                         fi
                       fi
                       AC_CHECK_HEADERS(gssapi.h gssapi/gssapi.h)
                       if test "x$ac_libdir" != "x" ; then
                         LDFLAGS="$LDFLAGS -L$ac_libdir $w_flag$ac_libdir$w_flag_2"
                         for lib in $ac_gss_libs; do
                           AC_CHECK_LIB($lib,main)
                         done
                       else
                         ac_gssapi_libs=`krb5-config --libs gssapi 2>/dev/null`
                         if test "x$ac_gssapi_libs" != "x" ; then
                           ac_libdir=`echo $ac_gssapi_libs | grep "\-L"`
                           if test "x$ac_libdir" != "x" ; then
                             ac_libdir=`echo $ac_gssapi_libs | sed -e 's/.*-L//' | sed -e 's/ .*//'`  
                             LDFLAGS="$LDFLAGS $w_flag$ac_libdir$w_flag_2"
                           fi
                           LDFLAGS="$LDFLAGS $ac_gssapi_libs"
                         else
                           for lib in $ac_gss_libs; do
                             AC_CHECK_LIB($lib,main)
                           done
                         fi
                       fi
                       ;;
                *) if test "x$enableval" != "xyes" -a "x$enableval" != "x" -a "x$enableval" != "xno" ; then
                     ac_libdir=$enableval/lib
                     ac_includedir=$enableval/include
                     CPPFLAGS="$CPPFLAGS -I$ac_includedir"
                     AC_CHECK_HEADERS(gssapi.h gssapi/gssapi.h)
                     LDFLAGS="$LDFLAGS -L$ac_libdir $w_flag$ac_libdir$w_flag_2"
                     for lib in $ac_gss_libs; do
                        AC_CHECK_LIB($lib,main)
                     done
                   else
                     ac_gssapi_cflags=`krb5-config --cflags gssapi 2>/dev/null`
                     if test "x$ac_gssapi_cflags" != "x" ; then
                         CPPFLAGS="$CPPFLAGS $ac_gssapi_cflags"
                     fi
                     ac_gssapi_libs=`krb5-config --libs gssapi 2>/dev/null`
                     if test "x$ac_gssapi_libs" != "x" ; then
                         ac_libdir=`echo $ac_gssapi_libs | grep "\-L"`
                         if test "x$ac_libdir" != "x" ; then
                           ac_libdir=`echo $ac_gssapi_libs | sed -e 's/.*-L//' | sed -e 's/ .*//'`  
                           LDFLAGS="$LDFLAGS $w_flag$ac_libdir$w_flag_2"
                         fi
                         LDFLAGS="$LDFLAGS $ac_gssapi_libs"
                     else
                         for lib in $ac_gss_libs; do
                            AC_CHECK_LIB($lib,main)
                         done
                     fi
                     AC_CHECK_HEADERS(gssapi.h gssapi/gssapi.h)
                   fi
                   ;;
       esac
       enable_arg="heimdal"
}
check_nas(){
       AC_DEFINE(HAVE_NAS_KERBEROS,1,[Define to 1 if you have NAS Kerberos])
       AC_CHECK_HEADERS(gssapi.h gssapi/gssapi.h gssapi/gssapi_krb5.h gssapi/gssapi_generic.h)
       LDFLAGS="$LDFLAGS -L/usr/lib"
       ac_gss_libs="krb5 gssapi_krb5 ksvc";
       for lib in $ac_gss_libs; do
          AC_CHECK_LIB($lib,main)
       done
       enable_arg="nas"
}

check_seam_64(){
       SPARCV9s="/sparcv9"
       check_seam
       enable_arg="seam64"
}

check_seam(){
       AC_DEFINE(HAVE_SEAM_KERBEROS,1,[Define to 1 if you have SEAM Kerberos])
       AC_CHECK_HEADERS(gssapi.h gssapi/gssapi.h gssapi/gssapi_ext.h)
       ac_sol_libs="nsl socket resolv gss"
       for lib in $ac_sol_libs; do
          AC_CHECK_LIB($lib,main)
       done
       enable_arg="seam"
}

dnl Define MIT libraries
AC_ARG_ENABLE(mit,
  [  --enable-mit[=DIR]	  enable use of MIT package (default=yes) ],
  [
    if test "x$enableval" != "xno" ; then
       check_mit
    fi ])

dnl Define Heimdal libraries
AC_ARG_ENABLE(heimdal,
  [  --enable-heimdal[=DIR]	  enable use of Heimdal package (default=no) ],
  [
    if test "x$enableval" != "xno" ; then
       check_heimdal
    fi ])

dnl Define NAS libraries
AC_ARG_ENABLE(nas,
  [  --enable-nas 		  enable use of NAS(AIX) package (default=no) ],
  [
    if test "x$enableval" != "xno" ; then
       check_nas
    fi ])

dnl Define SEAM libraries
AC_ARG_ENABLE(seam,
  [  --enable-seam[=SRC]	  enable use of SEAM(Solaris) package (default=no) ],
  [
    if test "x$enableval" != "xno" ; then
       check_seam
    fi ])

dnl Define SEAM libraries
AC_ARG_ENABLE(seam-64,
  [  --enable-seam-64[=SRC]	  enable use of 64bit SEAM(Solaris) package (default=no) ],
  [
    if test "x$enableval" != "xno" ; then
       check_seam_64
    fi ])

dnl Define system default
if test "$enable_arg" = "no"; then
  dnl Autodetect system
  dnl Check krb5-config first
  AC_CHECK_PROG(ac_krb5_config,krb5-config,yes,no)
  case $sys in
           Linux) rpm -q heimdal-lib >/dev/null 2>&1
                  if test $? = 0 ; then
                    check_heimdal
		  else 
		    check_mit
                  fi 
                  ;;
           AIX)   lslpp -L krb5.client.rte >/dev/null 2>&1
                  if test $? = 0 ; then
                    check_nas
		  else 
		    check_mit
                  fi
                  ;;
           SunOS) pkginfo SUNWgss >/dev/null 2>&1
                  if test $? = 0 ; then
                    check_seam
		  else 
		    check_mit
                  fi
                  ;;
           FreeBSD) check_heimdal
                    ;;
           *) check_mit
              ;;
  esac
fi

AC_C_BIGENDIAN

AC_CHECK_HEADERS( \
	errno.h \
	netdb.h \
	stdio.h \
	stdlib.h \
	string.h \
	sys/time.h\
	time.h \
	unistd.h \
)

AC_CONFIG_HEADER(ska_config.h)

AH_TOP([/*
 * -----------------------------------------------------------------------------
 *
 * Author: Markus Moeller (|MAIL|)
 *
 * Copyright (C) 2007 Markus Moeller. All rights reserved.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *   As a special exemption, M Moeller gives permission to link this program
 *   with MIT, Heimdal or other GSS/Kerberos libraries, and distribute
 *   the resulting executable, without including the source code for
 *   the Libraries in the source distribution.
 *
 * -----------------------------------------------------------------------------
 */
])

squid_dir=
AC_ARG_WITH([squid],
  AC_HELP_STRING([--with-squid=PATH],
	[Special option for building bundled inside Squid. Do not define manually.]),
  [ squid_dir=$withval ]
)

CPPFLAGS="$CPPFLAGS -I../../../ -I../../../include/ -I$squid_dir/include -I$squid_dir/src -I$squid_dir"
AC_CACHE_CHECK([for SQUID at '$squid_dir' ],ac_cv_have_squid,[
AC_TRY_RUN([
#include <config.h>
int main(int argc, char *argv[]) {
#ifdef SQUID_CONFIG_H
return 0;
#else
return 1;
#endif
}],
  ac_cv_have_squid=yes,
  ac_cv_have_squid=no)
])
LDFLAGS="$LDFLAGS -L../../../lib"
if test "x$ac_cv_have_squid" = "xyes"; then
  AC_DEFINE(HAVE_SQUID,1, [Define to 1 if you have SQUID])
  AC_CHECK_HEADERS(getaddrinfo.h getnameinfo.h util.h)
  AC_CHECK_DECLS([xgetaddrinfo], [], [], [[#include <getaddrinfo.h>]])
  AC_CHECK_DECLS([xfreeaddrinfo], [], [], [[#include <getaddrinfo.h>]])
  AC_CHECK_DECLS([xgai_strerror], [], [], [[#include <getaddrinfo.h>]])
  AC_CHECK_DECLS([xgetnameinfo], [], [], [[#include <getnameinfo.h>]])
  AC_CHECK_DECLS([xstrdup], [], [], [[#include <util.h>]])
  AC_CHECK_DECLS([xmalloc], [], [], [[#include <util.h>]])
  AC_CHECK_DECLS([xfree], [], [], [[#include <util.h>]])
  AC_CHECK_LIB(m,main)
  AC_CHECK_LIB(mw,main)
  LIBS="-lmiscutil $LIBS"
AH_TOP([ /* This is building inside Squid. We need their config as well. */
/* bit of autoconf magic hack */
#undef HAVE_SQUID

#if HAVE_SQUID
#include "squid_compat.h"
#endif /* HAVE_SQUID */
])
AH_BOTTOM([
/* Squid provides a few compat libraries */
#ifdef HAVE_SQUID
#ifdef HAVE_GETADDRINFO_H
#include "getaddrinfo.h"
#endif
#ifdef HAVE_GETNAMEINFO_H
#include "getnameinfo.h"
#endif
#ifdef HAVE_UTIL_H
#include "util.h"
#endif
#endif /* HAVE_SQUID */
])

fi


AH_BOTTOM([
#ifdef HAVE_HEIMDAL_KERBEROS
#ifdef HAVE_GSSAPI_GSSAPI_H
#include <gssapi/gssapi.h>
#elif defined(HAVE_GSSAPI_H)
#include <gssapi.h>
#else
#error "GSSAPI header required"
#endif
#define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
#else
#ifdef HAVE_SEAM_KERBEROS
#ifdef HAVE_GSSAPI_GSSAPI_H
#include <gssapi/gssapi.h>
#elif defined(HAVE_GSSAPI_H)
#include <gssapi.h>
#else
#error "GSSAPI header required"
#endif
#ifdef HAVE_GSSAPI_GSSAPI_EXT_H
#include <gssapi/gssapi_ext.h>
#endif
#define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
#else /*MIT*/
#ifdef HAVE_GSSAPI_GSSAPI_H
#include <gssapi/gssapi.h>
#elif defined(HAVE_GSSAPI_H)
#include <gssapi.h>
#else
#error "GSSAPI header required"
#endif
#ifdef HAVE_GSSAPI_GSSAPI_KRB5_H
#include <gssapi/gssapi_krb5.h>
#endif
#ifdef HAVE_GSSAPI_GSSAPI_GENERIC_H
#include <gssapi/gssapi_generic.h>
#endif
#endif
#endif

])

echo "configure: ## -----------------------------##"
echo "configure: ##"
echo "configure: ## $enable_arg has been selected"
echo "configure: ##"
echo "configure: ## -----------------------------##"

MY_CFLAGS="-Wall -Wextra -Werror -Wcomment -Wpointer-arith -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wdeclaration-after-statement -Wshadow"
for ac_cv_my_cflag in $MY_CFLAGS; do
echo "int main()
  {
     return 0;
  };" > conftest.c
${CC} $ac_cv_my_cflag -c conftest.c 2>/dev/null
res=$?
rm -f conftest.*
if test "$res" = "0"; then
 CFLAGS="$CFLAGS $ac_cv_my_cflag"
fi
done

AC_OUTPUT(Makefile)

echo "configure: updating ska_config.h"
sed -e "s/|MAIL|/"$PACKAGE_BUGREPORT"/" ska_config.h > .ska_config.h.tmp
mv .ska_config.h.tmp ska_config.h
