#
# $Id$
#
AC_INIT(Dist/xe.ads)
# AM_CONFIG_HEADER(Garlic/config.h)
AC_CONFIG_AUX_DIR(support)
#
# Package description
#
AC_CANONICAL_SYSTEM
AM_INIT_AUTOMAKE(GLADE,dev)
#
# Various programs
#
AC_CHECK_PROGS(AWK, [gnuawk gawk awk])
AC_PROG_CC
AM_PROG_GNAT_FOR_HOST
AM_PROG_GNAT_FOR_TARGET
AC_EXEEXT
AC_PROG_RANLIB
AC_CHECK_PROGS(SED, [gnused gsed sed])
AC_CHECK_PROGS(TAR, [gtar tar])
AC_CHECK_PROG(AR, ar, ar)
AC_CHECK_PROG(RM, rm, rm)
AC_CHECK_PROG(UNZIP, gunzip, gunzip)
AC_CHECK_PROG(GNATMAKE, gnatmake, gnatmake)
AC_SUBST(GNAT_FOR_HOST)
AC_SUBST(GNAT_FOR_TARGET)
#
# Check for maintainer mode
#
AC_ARG_ENABLE(debug,
[  --enable-debug          Turn on debugging options],
[CFLAGS="-g -O2"
 GNATFLAGS="${CFLAGS} -gnata  -gnatwu -gnatwe -gnatyI"
 LINKTEST="linktest"],
[CFLAGS="-O2"
 GNATFLAGS="${CFLAGS} -gnatpn -gnatwu -gnatwe -gnatyI"
 LINKTEST=""])
AC_SUBST(GNATFLAGS)
AC_SUBST(CFLAGS)
AC_SUBST(LINKTEST)
#
# Check for pcs name
#
AC_MSG_CHECKING(for pcs name)
AC_ARG_WITH(pcs,
[  --with-pcs=X            Use X as default pcs for gnatdist (garlic)],
[PCSNAME="$withval"],
[PCSNAME="garlic"])
[
  case ${PCSNAME} in
    garlic)
      PCSFLAGS="-I${prefix}/lib/garlic -largs -lgarlic";
      GLADEDIRS="Dist Garlic";;
    polyorb)
      which polyorb-config 1>/dev/null 2>/dev/null;
      if test "$?" = "0"; then
        PCSFLAGS="`polyorb-config`";
        GLADEDIRS="Dist";
      else
        PCSNAME="";
      fi;;
    *)
      PCSNAME="garlic";
      PCSFLAGS="-I${prefix}/lib/garlic -largs -lgarlic";
      GLADEDIRS="Dist Garlic";;
  esac;
]
if test "x${PCSNAME}" = "x"; then
  AC_MSG_RESULT("unknown pcs")
  exit 1;
else
  AC_MSG_RESULT(${PCSNAME})
fi;
AC_SUBST(PCSNAME)
AC_SUBST(PCSFLAGS)
AC_SUBST(GLADEDIRS)
#
# Check for optimization flags
#
AC_ARG_WITH(optimization,
[  --with-optimization=X   Use X as default optimization for gnatdist (O0)],
[GNATDISTOPT="$withval"],
[GNATDISTOPT="O0"])
AC_SUBST(GNATDISTOPT)
#
# Check that the GNAT version is recent enough for this version of GLADE
#
AM_GNAT_PREREQ(20041007, 3.16)
#
# Platform specific
#
AC_MSG_CHECKING([which platform-specific variant to use])
AC_ARG_WITH(platform,
[  --with-platform=letter  Select another platform's specific files (debug)],
[
  PLATFORM_SPECIFIC="${withval}"
],
[
  case "${target}" in
    i?86-*-cygwin32*)  PLATFORM_SPECIFIC=w;;
    i?86-*-mingw32*)   PLATFORM_SPECIFIC=w;;
    alpha*-*-osf*)     PLATFORM_SPECIFIC=a;;
    powerpc-*-aix4*)   PLATFORM_SPECIFIC=b;;
    mips*-sgi-irix*)   PLATFORM_SPECIFIC=g;;
    hppa*-hp-hpux*)    PLATFORM_SPECIFIC=h;;
    *-linux*)          PLATFORM_SPECIFIC=l;;
    *-*-solaris2*)     PLATFORM_SPECIFIC=s;;
    sparc-sun-sunos4*) PLATFORM_SPECIFIC=u;;
    *)                 ;;
  esac
])
AC_SUBST(PLATFORM_SPECIFIC)
[
  case "${PLATFORM_SPECIFIC}" in
    s) PSNAME="Solaris";;
    u) PSNAME="SunOS";;
    a) PSNAME="OSF";;
    l) PSNAME="Linux";;
    w) PSNAME="Windows/NT";;
    g) PSNAME="SGI";;
    h) PSNAME="HP";;
    b) PSNAME="AIX";;
    e) PSNAME="Empty";;
    *) PSNAME="Unknown target (${target})";;
  esac
]
AC_SUBST(PSNAME)
AC_MSG_RESULT($PSNAME)
#
# Select blocking IO policy (process or thread blocking IO)
# WARNING : This does not work for cross environment
#
AC_MSG_CHECKING([whether to emulate thread blocking I/O])
AC_ARG_ENABLE(non-blocking-io,
[  --enable-nonblockingio  Force emulation of thread blocking I/O],
[
  PROCESS_BLOCKING_IO="yes"],
[
  PROCESS_BLOCKING_IO="no";
  case ${PLATFORM_SPECIFIC} in
  s|l)
    ${GNATMAKE} -q -o ./isthrbio ${srcdir}/Garlic/isthrbio.adb>/dev/null 2>&1;
    if ./isthrbio; then
      PROCESS_BLOCKING_IO="yes";
    fi;
    /bin/rm -f ./isthrbio*;
    ;;
  *)
    ;;
  esac;
])
AC_MSG_RESULT([${PROCESS_BLOCKING_IO}])
AC_SUBST(PROCESS_BLOCKING_IO)
if test "${PROCESS_BLOCKING_IO}" = "yes"; then
  PROCESS_BLOCKING_IO=True;
else
  PROCESS_BLOCKING_IO=False;
fi
#
# Flag to use to copy flags without time modification
#
AC_MSG_CHECKING(for flag to preserve the original file properties)
[
  case "${PLATFORM_SPECIFIC}" in
    w) CP_PRESERVE="-lf";;
    *) CP_PRESERVE="-p";;
  esac
]
AC_SUBST(CP_PRESERVE)
AC_MSG_RESULT($CP_PRESERVE)
#
# Local launcher. Linux does not support local launching of programs with
# LinuxThreads with libc1. This crashes the machine.
#
AC_MSG_CHECKING(if platform supports local launching)
case "${target}" in
  *-linux*libc1*)	SUPPORTS_LOCAL_LAUNCH=False; AC_MSG_RESULT(no);;
  *)            	SUPPORTS_LOCAL_LAUNCH=True; AC_MSG_RESULT(yes);;
esac
AC_SUBST(SUPPORTS_LOCAL_LAUNCH)
#
# Remote shell command
#
AC_MSG_CHECKING(for remote shell command to use)
AC_ARG_WITH(rshcmd,
[  --with-rshcmd=command   Set alternate remote shell command],
[
  RSH_CMD="${withval}"
],
[
  case "${PLATFORM_SPECIFIC}" in
    h) RSH_CMD="remsh";;
    b) RSH_CMD="rsh";;
    *) RSH_CMD="rsh -n";;
  esac
])
AC_SUBST(RSH_CMD)
AC_MSG_RESULT([${RSH_CMD}])
#
# Remote shell flags
#
AC_MSG_CHECKING(for remote shell options to use)
AC_ARG_WITH(rshopt,
[  --with-rshopt=command   Set remote shell options],
[
  RSH_OPT="${withval}"
],
[
  case "${PLATFORM_SPECIFIC}" in
    h) RSH_OPT="-n";;
    b) RSH_OPT="-n";;
    *) RSH_OPT="";;
  esac
])
AC_SUBST(RSH_OPT)
AC_MSG_RESULT([${RSH_OPT}])
#
# Protocol list
#
AC_ARG_WITH(protocols,
[  --with-protocols=list   Enumerate protocol list],
[
  AC_MSG_CHECKING([for default protocols])
  LIST="";
  PROTOCOLSSOURCES="";
  PROTOCOLSOBJECTS="";
  DEFPROTOCOLNAME="";
  DEFPROTOCOLDATA="";
  for P in ${withval}; do
    if test -z "${DEFPROTOCOLNAME}"; then
      DEFPROTOCOLNAME=${P};
    fi
    case ${P} in
    TCP|tcp) 
      LIST="${LIST} tcp";
      PROTOCOLSSOURCES="${PROTOCOLSSOURCES} \$(TCPSOURCES)";
      PROTOCOLSOBJECTS="${PROTOCOLSOBJECTS} \$(TCPOBJECTS)";;
    none)
      LIST="none";
      ;;
    *) 
      AC_MSG_ERROR([unknown protocol ${P}])
      ;;
    esac;
  done
  AC_MSG_RESULT([${LIST}])
],
[
  AC_MSG_CHECKING([for default protocols])
  PROTOCOLSSOURCES="\$(TCPSOURCES)";
  PROTOCOLSOBJECTS="\$(TCPOBJECTS)";
  DEFPROTOCOLNAME="tcp";
  DEFPROTOCOLDATA="";
  AC_MSG_RESULT([tcp])
])
AC_SUBST(PROTOCOLSSOURCES)
AC_SUBST(PROTOCOLSOBJECTS)
#
# Default storage location
#
AC_ARG_WITH(data-location,
[  --with-data-location=x  Default data location (x=<name>://<data>)],
[
  DEFSTORAGENAME=`echo ${withval} | ${SED} 's,^\(.*\)://.*$,\1,'`
  DEFSTORAGEDATA=`echo ${withval} | ${SED} 's,^.*://\(.*\)$,\1,'`
],
[
  DEFSTORAGENAME="dfs"
  DEFSTORAGEDATA=""
])
AC_MSG_CHECKING([for default data location])
AC_MSG_RESULT([${DEFSTORAGENAME}://${DEFSTORAGEDATA}])
AC_SUBST(DEFSTORAGENAME)
AC_SUBST(DEFSTORAGEDATA)
#
# Default boot location
#
AC_ARG_WITH(boot-location,
[  --with-boot-location=x  Default boot location (x=<name>://<data>)],
[
  DEFPROTOCOLNAME=`echo ${withval} | ${SED} 's,^\(.*\)://.*$,\1,'`
  DEFPROTOCOLDATA=`echo ${withval} | ${SED} 's,^.*://\(.*\)$,\1,'`
],
[
  DEFPROTOCOLDATA=`echo 000$$ | ${SED} 's,^.*\(...\)$,localhost:5\1,'`
])
AC_MSG_CHECKING([for default boot location])
AC_MSG_RESULT([${DEFPROTOCOLNAME}://${DEFPROTOCOLDATA}])
AC_SUBST(DEFPROTOCOLNAME)
AC_SUBST(DEFPROTOCOLDATA)
#
# Filter list
#
AC_ARG_WITH(filters,
[  --with-filters=list     Enumerate filter list (default=zip)],
[
  FILTERSLIST="${withval}";
],
[
  FILTERSLIST="zip"
])
AC_MSG_CHECKING([for default filters])
[
if test -d Garlic; then
  :
else
  mkdir Garlic
fi
FILTERSMAKEFILE=Garlic/Makefile.fil;
FILTERSOURCES=""
FILTEROBJECTS=""
FILTERINSTALL=""
FILTEREXTRALIBS=""
rm -f ${FILTERSMAKEFILE}
for F in ${FILTERSLIST}; do
  if test -f ${srcdir}/Garlic/Makefile.${F}; then
    cat ${srcdir}/Garlic/Makefile.${F} >>${FILTERSMAKEFILE}
    echo "" >>${FILTERSMAKEFILE}
    FILTERSSOURCES="${FILTERSSOURCES} \$(${F}SOURCES)"
    FILTERSOBJECTS="${FILTERSOBJECTS} \$(${F}OBJECTS)"
    FILTERSINSTALL="${FILTERSINSTALL} \$(${F}INSTALL)"
    FILTERSEXTRALIBS="${FILTERSEXTRALIBS} \$(${F}EXTRAS)"
  else
    ]AC_MSG_ERROR([unknown filter ${F}])[
  fi;
done
]
AC_MSG_RESULT([${FILTERSLIST}])
#
# Force zip lib installation when zip filter included
#
AC_ARG_ENABLE(glade-zlib,
[  --enable-glade-zlib     Force installation of glade zip library],
[INSTALLZIPLIB="yes"],
[INSTALLZIPLIB="no"])
#
# Libraries used for filters
#
case ${FILTERSLIST} in
  *zip*)
    if test $INSTALLZIPLIB = no; then
      AC_CHECK_LIB(z,zlibVersion)
      if test $ac_cv_lib_z_zlibVersion = no; then
        INSTALLZIPLIB=yes
      fi
    fi
    AC_MSG_CHECKING([whether zip lib installation is needed])
    if test ${INSTALLZIPLIB} = yes; then
      FILTERSINSTALL="${FILTERSINSTALL} install-zlib";
    fi
    AC_MSG_RESULT([${INSTALLZIPLIB}])
    ;;
esac
AC_SUBST_FILE(FILTERSMAKEFILE)
AC_SUBST(FILTERSSOURCES)
AC_SUBST(FILTERSOBJECTS)
AC_SUBST(FILTERSEXTRALIBS)
AC_SUBST(FILTERSINSTALL)
#
# Extra libraries
#
AC_ARG_WITH(extra-libs,
[  --with-extra-libs=libs  Add extra libraries when building a program],
 [EXTRA_LIBS="${withval}"])
AC_SUBST(EXTRA_LIBS)
#
# Check whether we remove support for RPC abortion
#
AC_ARG_ENABLE(rpc-abortion,
[  --enable-rpc-abortion   Enable RPC abortion even when not supported],
[SUPPORT_RPC_ABORTION="True"],
[SUPPORT_RPC_ABORTION="Default"])
AM_SUPPORT_RPC_ABORTION
AC_MSG_CHECKING(whether we support RPC abortion)
if test $SUPPORT_RPC_ABORTION = "True"; then
  AC_MSG_RESULT([true])
else
  AC_MSG_RESULT([false])
fi
AC_MSG_CHECKING(for exception model to use)
AC_MSG_RESULT($EXCEPTION_MODEL)
AC_SUBST(GNAT_RTS_FLAG)
AC_SUBST(SUPPORT_RPC_ABORTION)
#
# Header files, only when not cross-compiling
#
#if test $target = $host; then
# AC_CHECK_HEADERS([stdio.h sys/types.h sys/socket.h errno.h netdb.h string.h])
# AC_CHECK_HEADERS([netinet/in.h signal.h fcntl.h termio.h termios.h])
# AC_CHECK_HEADERS([sys/file.h sys/ioctl.h netinet/tcp.h sys/systeminfo.h])
# AC_CHECK_HEADERS([poll.h stropts.h sys/conf.h sys/param.h time.h])
#fi
#
# Output
#
AC_OUTPUT([Makefile
           Dist/Makefile
           Dist/xe_defs-defaults.ads
           Garlic/Makefile
           Garlic/s-gaplsp.ads
           Examples/check-config
           Examples/Makefile
           Examples/Bank/Makefile
           Examples/Eratho/Makefile
           Examples/Eratho/absolute/Makefile
           Examples/Eratho/cycle/Makefile
           Examples/Eratho/dynamic/Makefile
           Examples/Eratho/spiral/Makefile
           Examples/Filters/Makefile
           Examples/Messages/Makefile
           Examples/MultiPro/Makefile
           Examples/DSMStorage/Makefile
           Examples/MultiSto/Makefile
           Examples/Reconnection/Makefile
           Examples/LightBank/Makefile])
