dnl Process this file with autoconf to produce a configure script.
AC_INIT
AC_CONFIG_SRCDIR([hmakerc.in])

AC_REVISION($Revision: 1.104 $)

AC_PREREQ(2.59)

AC_CANONICAL_TARGET([])

libc_version=
if test `echo $host | grep -c linux` -ne 0; then
   if test -f /bin/sh; then
     changequote(,)
     libc_ldd=`ldd /bin/sh | grep -w libc`
     if test "x$libc_ldd" != "x"; then
       libc=`echo $libc_ldd | sed 's:.*=>[ ]::' | sed 's:[ ].*::'`
       if test "x$libc" != "x"; then
         if test -x "$libc"; then
           version=`$libc | grep -i roland | sed 's:^[^0-9]*::' | sed 's:[^0-9]*$::' | sed 's:[A-Z].*$::' | sed 's:[a-z].*$::' | sed 's:[ ].*::'`
           if test "x$version" = "x"; then
             version=`$libc | grep -i version | grep -i release | grep -i library | sed 's:^[^0-9]*::' | sed 's:[^0-9]*$::' | sed 's:[A-Z].*$::' | sed 's:[a-z].*$::' | sed 's:[ ].*::'`
           fi
         fi
         if test `echo $version | grep -c '[a-z]'` != 0 -o `echo $version | grep -c '[A-Z]'` != 0 -o `echo $version | grep -c '('` != 0 -o `echo $version | grep -c ')'` != 0; then
           version=""
         fi
         if test "x$version" != "x"; then
           libc_version=-libc$version
         fi
       fi
     fi
     changequote([,])
   fi
fi
if test "x$libc_version" != "x"; then
  host=$host$libc_version
  AC_MSG_RESULT(modified Linux system type is $host)
fi

#-------------------------------------------------------------------------------
# Option --with-components allows configuration to be specified on command line.
# Components are selected by the top-level BUILD_DIR/configure.  If configuring
# by hand here, default to all components if no value is given.
AC_ARG_WITH(
  components,
  ,
  h_components=$withval,
  h_components="tcl tk tclreadline xpa pgplot plt pow itcl itk iwidgets220 iwidgets tix extf77perl pgplotperl"
)
# Components whose configure we run:
h_configurable_components="tcl tk tclreadline xpa itcl itk iwidgets220 iwidgets tix"

AC_ARG_WITH(
  heatop,
  ,
  h_heatop=$withval,
  h_heatop=NONE
)

if test "x$h_heatop" = x; then
  h_heatop=NONE
fi

if test $h_heatop != NONE; then
  HD_TOP_PFX=$h_heatop
  HD_TOP_EXEC_PFX=$HD_TOP_PFX/$host
else
  HD_TOP_PFX=
  HD_TOP_EXEC_PFX=
fi

AC_ARG_WITH(
  heacore,
  ,
  h_heacore=$withval,
  h_heacore=no
)

AC_ARG_ENABLE(
  shared,
  [ --disable-shared Do not build shared libraries ],
  h_shared=$enableval,
  h_shared=yes,
  h_shared=no
)
AC_ARG_ENABLE(
  static,
  [ --enable-static Do not build shared libraries ],
  [ if test $enableval = yes; then h_shared=no; fi ]
)
if test $h_shared = yes; then
  HD_LIB_STYLE=shared
else
  HD_LIB_STYLE=static
fi
AC_SUBST(HD_LIB_STYLE)

SYMBOLS=no
AC_ARG_ENABLE(
  symbols,
  [AS_HELP_STRING([--enable-symbols],
   [Turn all optimization off & do not strip debugging symbols from binaries])],
  [ if test $enableval = yes; then SYMBOLS=yes; fi ]
)
AC_SUBST(SYMBOLS)

h_strip=enabled
AC_ARG_ENABLE(
  strip,
  [AS_HELP_STRING([--disable-strip],
   [Do not strip debugging symbols from binaries])],
  [ if test $enableval != yes; then h_strip=no; fi ],
  [ h_strip=yes ]
)

# Check for symbols/strip conflict:
if test $SYMBOLS = yes; then
   if test $h_strip = enabled; then
      AC_MSG_WARN('--enable-symbols' and '--enable-strip' conflict.)
      AC_MSG_WARN(Disabling strip.)
   fi
   h_strip=no
else
   if test $h_strip = enabled; then
      h_strip=yes
   fi
fi

AC_ARG_ENABLE(
  perlmods,
  [ --disable-perlmods Do not build Perl modules ],
  PERLMODS_BLD=$enableval,
  PERLMODS_BLD=yes,
  PERLMODS_BLD=no
)
AC_SUBST(PERLMODS_BLD)

h_hera_build=no
AC_ARG_ENABLE(
  hera,
  [ --enable-hera Build for HERA project (GSFC only) ],
  [ if test $enableval = yes; then h_hera_build=yes; fi ]
)
AC_SUBST(h_hera_build)

fv_build=no
AC_ARG_ENABLE(
  fv-standalone,
  [AS_HELP_STRING([--enable-fv-standalone],
   [Build stand-alone fv GUI (GSFC only)])],
  [ if test $enableval = yes; then fv_build=yes; fi ]
)
if test $fv_build = yes; then
  if test `echo $host | grep -c darwin` -ne 0; then
    CFLAGS="-arch i386 -arch ppc"
    export CFLAGS
  fi
fi
AC_SUBST(fv_build)

TKPGPLOT=""
AC_ARG_ENABLE(
  tkpgplot,
  [AS_HELP_STRING([--enable-tkpgplot],
   [Build PGPLOT Tk library (Used by XIMAGE only)])],
  [ if test $enableval = yes; then TKPGPLOT="libtkpgplot.a"; fi ]
)
AC_SUBST(TKPGPLOT)

# configurable optimization
COPT=
AC_ARG_WITH(
  copt,
  [ --with-copt Specify C compiler optimization e.g. '-O2' ],
  COPT=$withval
)
CXXOPT=
AC_ARG_WITH(
  cxxopt,
  [ --with-cxxopt Specify C++ compiler optimization e.g. '-O2' ],
  CXXOPT=$withval
)
FOPT=
AC_ARG_WITH(
  fopt,
  [ --with-fopt Specify Fortran compiler optimization e.g. '-O2' ],
  FOPT=$withval
)

h_ldopt=yes
AC_ARG_ENABLE(
  ldopt,
  [ --disable-ldopt Disable linker optimization ],
  [ if test $enableval = no; then h_ldopt=no; fi ]
)

# Set optimization to "none" if symbols are enabled for debugging
# (will be reset to "" later):
if test "x$SYMBOLS" = xyes; then
   COPT="none"
   CXXOPT="none"
   FOPT="none"
fi

#-------------------------------------------------------------------------------


# Get details about this script and its location.
h_this_script=`echo $0 | sed 's%.*/%%'`
h_this_dir=`echo $0 | sed "s%$h_this_script\$%%"`
if test "x$h_this_dir" = x; then
  h_this_dir=`pwd`
else
  h_this_dir=`cd $h_this_dir; pwd`
fi

# Name of the utility which scans configuration files for dependencies
hd_scanenv="$h_this_dir/hd_scanenv"

hd_conf_file="$h_this_dir/hd_config_info"

# Find name and version of this component
h_comp=`$hd_scanenv $hd_conf_file COMPONENT`
h_vers=`$hd_scanenv $hd_conf_file VERSION`
AC_SUBST(h_comp)
AC_SUBST(h_vers)

# Set the prefix to use in all recursive configures.
if test $prefix = NONE; then
  if test $h_heatop != NONE; then
    prefix="$h_heatop/$h_comp"
  else
    prefix=`cd $h_this_dir/..; pwd`
  fi
fi

# Set the exec-prefix to use in all recursive configures.
if test $exec_prefix = NONE; then
  exec_prefix="$prefix/$host"
fi

# Get this directory
HD_THIS_DIR=$h_this_dir

# Find the top of the source tree
HD_SRC=`cd ..; pwd | sed "s%/*$h_vers$%%"`
export HD_SRC

# And the top of the headas source tree
h_top=`cd $HD_SRC/..; pwd`

# If heacore was not specified on the cmd line, make a search path for it.
# Otherwise, try to interpret the supplied heacore to determine a directory
# which contains configuration information.
if test "x$h_heacore" = xno -o "x$h_heacore" = x; then
  h_heacore=`$hd_scanenv $hd_conf_file HEACORE`
  h_corepath=`echo $prefix | sed -ne "s%$h_comp/$host$%$h_heacore/$host%p"`
  if test "x$h_corepath" != x; then
    h_corepath="$h_corepath/BUILD_DIR"
  fi
  h_corepath="$h_top/$h_heacore/BUILD_DIR $h_corepath"
elif test -d "$h_top/$h_heacore/BUILD_DIR"; then
  h_corepath="$h_top/$h_heacore/BUILD_DIR"
elif test -d "$h_heacore/BUILD_DIR"; then
  h_corepath="$h_heacore/BUILD_DIR"
fi

# Search the path for a configuration specification.
h_coreBUILD_DIR=
for h_dir in $h_corepath; do
  if test -f "$h_dir/hmakerc"; then
    h_coreBUILD_DIR="$h_dir"
    break
  fi
done

if test "x$h_coreBUILD_DIR" = x; then
  AC_MSG_ERROR(Unable to guess location of heacore/BUILD_DIR directory)
fi

# -----------------------------------------------------------------------------
HC_BLD_EXEC_PFX=`$hd_scanenv $h_coreBUILD_DIR/hmakerc HD_BLD_EXEC_PFX`
HC_EXEC_PFX=`$hd_scanenv $h_coreBUILD_DIR/hmakerc HD_EXEC_PFX`
CFITSIO=`$hd_scanenv $h_coreBUILD_DIR/hmakerc CFITSIO`
PIL=`$hd_scanenv $h_coreBUILD_DIR/hmakerc PIL`
READLINE=`$hd_scanenv $h_coreBUILD_DIR/hmakerc READLINE`
READLINE_DIR=`$hd_scanenv $h_coreBUILD_DIR/hmakerc READLINE_DIR`
WCSLIB=`$hd_scanenv $h_coreBUILD_DIR/hmakerc WCSLIB`
WCSLIB_DIR=`$hd_scanenv $h_coreBUILD_DIR/hmakerc WCSLIB_DIR`
HEAINIT=`$hd_scanenv $h_coreBUILD_DIR/hmakerc HEAINIT`
HEAIO=`$hd_scanenv $h_coreBUILD_DIR/hmakerc HEAIO`
HEAUTILS=`$hd_scanenv $h_coreBUILD_DIR/hmakerc HEAUTILS`
EXTF77PERL=`$hd_scanenv $hd_conf_file EXTF77PERL`
EXTF77PERL_DIR=`$hd_scanenv $hd_conf_file EXTF77PERL_DIR`
PGPLOTPERL=`$hd_scanenv $hd_conf_file PGPLOTPERL`
PGPLOTPERL_DIR=`$hd_scanenv $hd_conf_file PGPLOTPERL_DIR`
  
AC_SUBST(HC_BLD_EXEC_PFX)
AC_SUBST(HC_EXEC_PFX)
AC_SUBST(CFITSIO)
AC_SUBST(PIL)
AC_SUBST(READLINE)
AC_SUBST(WCSLIB)
AC_SUBST(WCSLIB_DIR)
AC_SUBST(HEAINIT)
AC_SUBST(HEAIO)
AC_SUBST(HEAUTILS)
AC_SUBST(HD_TOP_EXEC_PFX)
AC_SUBST(HD_TOP_PFX)
AC_SUBST(HD_THIS_DIR)
AC_SUBST(HD_SRC)

TCL=`$hd_scanenv $hd_conf_file TCL`
TK=`$hd_scanenv $hd_conf_file TK`
ITCL=`$hd_scanenv $hd_conf_file ITCL`
ITK=`$hd_scanenv $hd_conf_file ITK`
IWIDGETS=`$hd_scanenv $hd_conf_file IWIDGETS`
TIX=`$hd_scanenv $hd_conf_file TIX`
TCLREADLINE=`$hd_scanenv $hd_conf_file TCLREADLINE`
XPA=`$hd_scanenv $hd_conf_file XPA`
TCLXPA=`$hd_scanenv $hd_conf_file TCLXPA`
PGPLOT=`$hd_scanenv $hd_conf_file PGPLOT`

TCL_DIR=`$hd_scanenv $hd_conf_file TCL_DIR`
TK_DIR=`$hd_scanenv $hd_conf_file TK_DIR`
ITCL_DIR=`$hd_scanenv $hd_conf_file ITCL_DIR`
ITK_DIR=`$hd_scanenv $hd_conf_file ITK_DIR`
IWIDGETS_DIR=`$hd_scanenv $hd_conf_file IWIDGETS_DIR`
IWIDGETS220_DIR=`$hd_scanenv $hd_conf_file IWIDGETS220_DIR`
TIX_DIR=`$hd_scanenv $hd_conf_file TIX_DIR`
TCLREADLINE_DIR=`$hd_scanenv $hd_conf_file TCLREADLINE_DIR`
XPA_DIR=`$hd_scanenv $hd_conf_file XPA_DIR`

TCL_VERS_SHORT=`$hd_scanenv $hd_conf_file TCL_VERS_SHORT`
TK_VERS_SHORT=`$hd_scanenv $hd_conf_file TK_VERS_SHORT`
TCLREADLINE_VERS=`$hd_scanenv $hd_conf_file TCLREADLINE_VERS`

AC_SUBST(TCL)
AC_SUBST(TK)
AC_SUBST(ITCL)
AC_SUBST(ITK)
AC_SUBST(IWIDGETS)
AC_SUBST(TIX)
AC_SUBST(TCLREADLINE)
AC_SUBST(XPA)
AC_SUBST(TCLXPA)
AC_SUBST(PGPLOT)
AC_SUBST(TCL_DIR)
AC_SUBST(TK_DIR)
AC_SUBST(ITCL_DIR)
AC_SUBST(ITK_DIR)
AC_SUBST(IWIDGETS_DIR)
AC_SUBST(IWIDGETS220_DIR)
AC_SUBST(TIX_DIR)
AC_SUBST(TCLREADLINE_DIR)
AC_SUBST(XPA_DIR)
AC_SUBST(TCL_VERS_SHORT)
AC_SUBST(TK_VERS_SHORT)
AC_SUBST(TCLREADLINE_VERS)

#-------------------------------------------------------------------------------
UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown

#-------------------------------------------------------------------------------
# Checks for programs.
#-------------------------------------------------------------------------------

# strip (optional)
#-----------------
AC_CHECK_PROG(STRIP, strip, strip, :)
if test ${UNAME_SYSTEM} = Darwin; then
  STRIP="$STRIP -x"
fi
if test $h_strip = no; then
  STRIP=:
fi
AC_SUBST(STRIP)

# ar required
#------------
AC_CHECK_PROG(AR, ar, ar, noar)
if test $AR = noar; then
  AC_MSG_ERROR(ar not found in your \$PATH. See your sysdamin.)
fi
ARCHIVE="$AR rv"
AC_SUBST(ARCHIVE)

# ranlib required
#----------------
AC_PROG_RANLIB
# RANLIB on IRIX is flaky
if test ${UNAME_SYSTEM} = IRIX; then
  RANLIB=:
fi
if test ${UNAME_SYSTEM} = Darwin; then
  RANLIB="$RANLIB -cs"
fi

# MAKE:
#-------------------------------------------------------------------------------
# prefer gmake, but accept any make if set in the with-make option
if test "x$with_make" = x; then
  with_make=yes
fi
if test $with_make = yes; then
  AC_CHECK_PROGS(MAKE, gmake make, nomake)
  if test $MAKE = nomake ; then
    AC_MSG_ERROR(make was not found in your \$PATH. See your sysadmin.)
  else
    AC_MSG_CHECKING(whether $MAKE is GNU make)
    if test `$MAKE -f non-existent-Makefile --version 2> /dev/null | grep -c GNU` -eq 0 ; then
      AC_MSG_RESULT(no)
      AC_MSG_ERROR(HEADAS software requires GNU make)
    else
      AC_MSG_RESULT(yes)
    fi
  fi
elif test $with_make = no; then
  AC_MSG_ERROR(UNIX make is required to install any part of HEADAS!)
else
  MAKE=$with_make; export MAKE
  AC_SUBST(MAKE)
fi

# C:
#-------------------------------------------------------------------------------
# Try first to find a proprietary C compiler, then gcc
if test "x${UNAME_SYSTEM}" = xOSF1 -o "x${UNAME_SYSTEM}" = xSunOS -o "x${UNAME_SYSTEM}" = xIRIX; then
  if test "x$CC" = x; then
    AC_CHECK_PROGS(CC, cc)
  fi
fi
AC_PROG_CC

if test "x$CC" = x; then
  AC_MSG_ERROR(Could not find a C compiler in $PATH)
fi
if test "$cross_compiling" = yes; then
  AC_MSG_WARN(Cannot run a simple C executable on your system:)
  AC_MSG_WARN(There may be something wrong with your compiler,)
  AC_MSG_WARN(or perhaps you're trying to cross-compile?)
  AC_MSG_WARN(Cross-compiling is not supported within HEADAS.)
  AC_MSG_WARN(Please make sure your compiler is working.)
  AC_MSG_WARN(Contact the FTOOLS help desk for further assistance.)
  AC_MSG_ERROR(Cross-compiling is not allowed.)
fi
if test "x$GCC" = x; then
  GCC=no
fi
if test "x$GCC" = xyes; then
   GCCVERSION="`$CC -dumpversion 2>&1`"
fi
AC_SUBST(GCC)
CC_BASENAME=`basename $CC`
export CC

# FORTRAN:
#-------------------------------------------------------------------------------
if test "x$GCC" = xyes; then
  # Prefer gfortran with GCC 4.x:
  changequote(,)
  gcc4_test=`echo $GCCVERSION | grep -c '^[4]\.[0123456789]'`
  changequote([,])
  if test $gcc4_test -ne 0; then
    f_search_order="gfortran g95 g77 f77"
  else
    f_search_order="g77 f77 g95 gfortran"
  fi
else
  f_search_order="f95 f90 f77 gfortran g95 g77"
fi
if test "x$FC" != x; then F77=$FC; fi
AC_PROG_F77([$f_search_order ifort])

if test "x$F77" = x; then
  AC_MSG_ERROR(Could not find a Fortran compiler in $PATH)
fi
if test "$cross_compiling" = yes; then
  AC_MSG_WARN(Cannot run a simple Fortran executable on your system:)
  AC_MSG_WARN(There may be something wrong with your compiler,)
  AC_MSG_WARN(or perhaps you're trying to cross-compile?)
  AC_MSG_WARN(Cross-compiling is not supported within HEADAS.)
  AC_MSG_WARN(Please make sure your compiler is working.)
  AC_MSG_WARN(Contact the FTOOLS help desk for further assistance.)
  AC_MSG_ERROR(Cross-compiling is not allowed.)
fi
if test "x$G77" = x; then
  G77=no
fi

FC=$F77
FCFLAGS=$FFLAGS
FC_BASENAME=`basename $FC`
FC_BASENAME_F77=`echo ${FC_BASENAME} | grep -c f77`
FC_BASENAME_F95=`echo ${FC_BASENAME} | grep -c f95`
FC_BASENAME_G77=`echo ${FC_BASENAME} | grep -c g77`
FC_BASENAME_G95=`echo ${FC_BASENAME} | grep -c g95`
FC_BASENAME_GFORTRAN=`echo ${FC_BASENAME} | grep -c gfortran`
FC_BASENAME_IFORT=`echo ${FC_BASENAME} | grep -c ifort`

# Additional G77 test for e.g. SuSE 10.x Linux, where the C preprocessor
# needed for the AC_PROG_F77 test is not packaged with the g77 compiler:
if test "${FC_BASENAME_G77}" -gt 0 -a $G77 = no; then
   G77VSTRING=`g77 --version | head -1 | grep -i 'gnu fortran'`
   if test -z "$G77VSTRING"; then
      G77=no
   else
      G77=yes
   fi
fi

# Check whether f77/g77 is actually gfortran:
if test "${FC_BASENAME_F77}" -gt 0 -o "${FC_BASENAME_G77}" -gt 0; then
   changequote(,)
   FCVSTRING=`$FC --version | head -1 | grep -i 'gnu fortran' | sed 's:^[^0-9]*::' | sed 's:[^0-9]*$::' | sed 's:[A-Z].*$::' | sed 's:[a-z].*$::' | sed 's:[ ].*::' | grep -c '^4'`
   changequote([,])
   if test "$FCVSTRING" -gt 0; then
      FC_BASENAME=gfortran
      FC_BASENAME_GFORTRAN=1
   fi
fi

AC_SUBST(FC)
AC_SUBST(FC_BASENAME)
AC_SUBST(G77)
export FC

# C++:
#-------------------------------------------------------------------------------
if test "x${UNAME_SYSTEM}" = xSunOS; then
  cxx_search_order="CC g++ c++ icpc icc cxx"
else
  cxx_search_order="g++ c++ icpc icc cxx"
fi
AC_PROG_CXX([$cxx_search_order])

if test "x$CXX" = x; then
  AC_MSG_ERROR(Could not find a C++ compiler in $PATH)
fi
if test "$cross_compiling" = yes; then
  AC_MSG_WARN(Cannot run a simple G++ executable on your system:)
  AC_MSG_WARN(There may be something wrong with your compiler,)
  AC_MSG_WARN(or perhaps you're trying to cross-compile?)
  AC_MSG_WARN(Cross-compiling is not supported within HEADAS.)
  AC_MSG_WARN(Please make sure your compiler is working.)
  AC_MSG_WARN(Contact the FTOOLS help desk for further assistance.)
  AC_MSG_ERROR(Cross-compiling is not allowed.)
fi
if test "x$GXX" = x; then
  GXX=no
fi
AC_SUBST(GXX)

CXX_BASENAME=`basename $CXX`
# Use specialized command when archiving C++ code with Solaris CC:
if test "x${UNAME_SYSTEM}" = "xSunOS" -a "x${CXX_BASENAME}" = "xCC"; then
  ARCHIVE_CXX="CC -xar -o"
else
  ARCHIVE_CXX="$ARCHIVE"
fi
AC_SUBST(ARCHIVE_CXX)
export CXX


# PERL:
#-------------------------------------------------------------------------------
AC_PATH_PROGS(PERL, perl perl5)
if test "x$PERL" = x; then
  AC_MSG_ERROR(Perl is required!)
fi
AC_SUBST(PERL)

# Capture default optimization for use if COPT/CXXOPT/FOPT were not specified:
#-------------------------------------------------------------------------------
changequote(,)
COPT_DEFAULT=`echo $CFLAGS | tr " " "\012" | grep "^\-O[0-9]"`
CXXOPT_DEFAULT=`echo $CXXFLAGS | tr " " "\012" | grep "^\-O[0-9]"`
FOPT_DEFAULT=`echo $FFLAGS | tr " " "\012" | grep "^\-O[0-9]"`
# Remove existing/default optimization (use *OPT macros instead):
CFLAGS=`echo $CFLAGS | sed 's:-O[0-9]* *::g'`
CXXFLAGS=`echo $CXXFLAGS | sed 's:-O[0-9]* *::g'`
FFLAGS=`echo $FFLAGS | sed 's:-O[0-9]* *::g'`
changequote([,])

#-------------------------------------------------------------------------------
# Checks for header files.
#-------------------------------------------------------------------------------
AC_HEADER_STDC
AC_CHECK_HEADERS(
  dirent.h fcntl.h limits.h malloc.h string.h sys/time.h unistd.h
)
AC_HEADER_TIME
AC_FUNC_ALLOCA

#-------------------------------------------------------------------------------
# Checks for typedefs, structures, and compiler characteristics.
#-------------------------------------------------------------------------------
AC_C_CONST
AC_TYPE_MODE_T
AC_TYPE_SIZE_T
AC_STRUCT_TM

AC_CHECK_SIZEOF(long,4)
SIZEOF_LONG="$ac_cv_sizeof_long"
AC_SUBST(SIZEOF_LONG)

#-------------------------------------------------------------------------------
# Checks for libraries.
#-------------------------------------------------------------------------------

# X
XLIBS=
XLIBPTH=
XINCLUDES=

# socket and nsl libraries -- only if needed
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, gethostbyname))
AC_CHECK_FUNCS(
  connect accept,
  ,
  AC_CHECK_LIB(socket, main, , , [ $XLIBS ])
)

# X11:
# Prepend the standard location X11 bin directory (if it exists) to PATH
# to assist AC_PATH_X in finding headers & libraries:
if test -d /usr/X11R6/bin; then
  if test "x$PATH" != x; then
    PATH=$PATH:/usr/X11R6/bin
  else
    PATH=/usr/X11R6/bin
  fi
  export PATH
fi
AC_PATH_X
AC_CHECK_HEADER([X11/Xlib.h],[],[AC_MSG_ERROR(X11 Development package is required in order to build HEASOFT!)],[])
if test `echo $x_includes | grep -c /` -ne 0; then
  XINCLUDES="-I$x_includes"
fi
if test `echo $x_libraries | grep -c /` -ne 0; then
  XLIBPTH="-L$x_libraries "
fi
XLIBS="$XLIBPTH-lX11"
AC_SUBST(XINCLUDES)
AC_SUBST(XLIBPTH)
AC_SUBST(XLIBS)

# math stuff
AC_CHECK_LIB(m, frexp)

# dl
AC_CHECK_LIB(dl, dlopen)
if test `echo $LIBS | grep -c '\-ldl'` -eq 0; then
  AC_CHECK_LIB(dld, dlopen)
fi

# termcap/ncurses/curses
AC_CHECK_LIB(ncurses, tgoto)
if test `echo $LIBS | grep -c '\-lncurses'` -eq 0; then
  AC_CHECK_LIB(curses, tgoto)
  if test `echo $LIBS | grep -c '\-lcurses'` -eq 0; then
    AC_CHECK_LIB(termcap, tgoto)
    if test `echo $LIBS | grep -c '\-ltermcap'` -eq 0; then
      AC_MSG_ERROR(libncurses or libcurses or libtermcap is required!)
    fi
  fi
fi

# special case system library additions:
case ${UNAME_SYSTEM} in
  Darwin*)
      AC_CHECK_LIB(cc_dynamic, main, LIBS="$LIBS -lcc_dynamic")
    ;;
  OSF*)
      AC_CHECK_LIB(db, vsnprintf, LIBS="$LIBS -ldb")
    ;;
  SunOS*)
      AC_CHECK_LIB(mvec, main, LIBS="$LIBS -lmvec")
      AC_CHECK_LIB(sunmath, main, LIBS="$LIBS -lsunmath")
    ;;
esac

#-------------------------------------------------------------------------------
# Tweak compiler flags as needed
#-------------------------------------------------------------------------------
LDFLAGS_C="${LDFLAGS}"
LDFLAGS_CXX="${LDFLAGS}"
LDFLAGS_F="${LDFLAGS}"

# Make C & Fortran play nice together on newer Mac hardware:
if test `echo $host | grep -c 86-apple-darwin` -ne 0; then

  # Test to see whether the C compiler is x86_64 by default:
  echo "int main(){return(0);}" > /tmp/$$.c
  $CC -v -o /tmp/$$.out /tmp/$$.c 2> /tmp/$$.log
  if test `cat /tmp/$$.log | grep -c 'arch x86_64'` -ne 0; then
    AC_MSG_WARN(x86_64 C compiler detected...)
    # Test to see whether the C compiler accepts "-arch i386":
    SAVE_CFLAGS="$CFLAGS"
    C32SWITCH="-arch i386"
    CFLAGS="$CFLAGS $C32SWITCH"
    AC_MSG_CHECKING([whether $CC accepts $C32SWITCH])
    AC_LANG_PUSH([C])
    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],
                      [c_has_option=yes],[c_has_option=no])
    AC_MSG_RESULT($c_has_option)
    if test "$c_has_option" = no; then
      C32SWITCH="-m32"
      CFLAGS="$SAVE_CFLAGS $C32SWITCH"
      AC_MSG_CHECKING([whether $CC accepts $C32SWITCH])
      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],
                        [c_has_option=yes],[c_has_option=no])
      AC_MSG_RESULT($c_has_option)
    fi
    AC_LANG_POP([])
    CFLAGS="$SAVE_CFLAGS"
    if test "$c_has_option" = yes; then
      # Force 32-bit compatibility:
      CFLAGS="$CFLAGS $C32SWITCH"
      export CFLAGS
      LDFLAGS_C="$LDFLAGS_C $C32SWITCH"
      export LDFLAGS_C
      CXXFLAGS="$CXXFLAGS $C32SWITCH"
      export CXXFLAGS
      LDFLAGS_CXX="$LDFLAGS_CXX $C32SWITCH"
      export LDFLAGS_CXX
      # For ITcl/ITk/Tix:
      LDFLAGS="$LDFLAGS $C32SWITCH"
      export LDFLAGS
    else
      AC_MSG_ERROR(This x86_64 C compiler is not currently supported!)
    fi
  fi
  rm -f /tmp/$$.*

  # Test to see whether the Fortran compiler is x86_64 by default:
  echo "      program fortrantest" > /tmp/$$.f
  echo "      end" >> /tmp/$$.f
  $FC -v -o /tmp/$$.out /tmp/$$.f 2> /tmp/$$.log
  if test `cat /tmp/$$.log | grep -c 'x86_64'` -ne 0; then
    AC_MSG_WARN(x86_64 Fortran compiler detected...)
    # Test to see whether the Fortran compiler accepts "-m32":
    SAVE_FFLAGS="$FFLAGS"
    F32SWITCH="-m32"
    FFLAGS="$FFLAGS $F32SWITCH"
    SAVE_LDFLAGS="$LDFLAGS"
    LDFLAGS=""
    SAVE_LIBS="$LIBS"
    LIBS="-lgfortranbegin -lgfortran -lgcc"
    AC_MSG_CHECKING([whether $FC accepts $F32SWITCH])
    AC_LANG_PUSH([Fortran 77])
    AC_LINK_IFELSE([AC_LANG_PROGRAM([])],[f_has_option=yes],[f_has_option=no])
    AC_MSG_RESULT($f_has_option)
    AC_LANG_POP([])
    FFLAGS="$SAVE_FFLAGS"
    LDFLAGS="$SAVE_LDFLAGS"
    LIBS="$SAVE_LIBS"
    if test "$f_has_option" = yes; then
      # Force 32-bit compatibility:
      FFLAGS="$FFLAGS $F32SWITCH"
      export FFLAGS
      LDFLAGS_F="$LDFLAGS_F $F32SWITCH"
      export LDFLAGS_F
    else
      AC_MSG_ERROR(This x86_64 Fortran compiler is not currently supported!)
    fi
  fi
  rm -f /tmp/$$.*

fi

FV_CFLAGS=
FV_LDFLAGS=
case ${UNAME_SYSTEM} in
  CYGWIN*)
    ;;
  Darwin*)
    CFLAGS="$CFLAGS -Dunix"
    # Flags for stand-alone fv universal binary:
    FV_CFLAGS="-arch i386 -arch ppc"
    FV_LDFLAGS="-framework CoreFoundation -flat_namespace -undefined suppress"
    ;;
  Linux*)
    ;;
  OSF*)
    if test $GCC = yes; then
      # Remove optimization on DEC systems
      COPT=none
    else
      # Standard DEC cc behavior is *STILL* K&R -- force ANSI compliance
      CFLAGS="$CFLAGS -Dunix"
    fi
    ;;
  IRIX*)
    AC_DEFINE(HAVE_POSIX_SIGNALS)
    ;;
  SunOS*)
    AC_DEFINE(HAVE_POSIX_SIGNALS)
    # Set SunOS C & C++ optimization to "-fast -xdepend=no" for non-GNU
    # compilers by default (unless symbols are enabled), and turn FOPT
    # off (unless overridden on the command line). Remove default -g flag
    # if optimizing.
    if test "x$COPT" != xnone; then
      if test $GCC = no; then
        if test "x$COPT" = x; then COPT="-fast -xdepend=no"; fi
        changequote(,)
        CFLAGS=`echo $CFLAGS | sed 's:-g[ 	][ 	]*::g' | sed 's:-g$::'`
        changequote([,])
      fi
    fi
    if test "x$CXXOPT" != xnone; then
      if test $GXX = no; then
        if test "x$CXXOPT" = x; then CXXOPT="-fast -xdepend=no"; fi
        changequote(,)
        CXXFLAGS=`echo $CXXFLAGS | sed 's:-g[ 	][ 	]*::g' | sed 's:-g$::'`
        changequote([,])
      fi
    fi
    if test "x$FOPT" != xnone; then
      if test $G77 = no; then
        if test "x$FOPT" = x; then FOPT="none"; fi
        changequote(,)
        FFLAGS=`echo $FFLAGS | sed 's:-g[ 	][ 	]*::g' | sed 's:-g$::'`
        changequote([,])
      fi
    fi
    ;;
  *)
    ;;
esac

# GNU: use Wall / pedantic / uninitialized / no-second-underscore / fno-f2c
if test $G77 = yes; then
      # Turn off --pedantic flag for tcltk component until further notice.
      # pgplot chokes on pedantic, and is currently the only fortran here.
      # -fno-f2c is not available for g95/gfortran.
      if test "${FC_BASENAME_G95}" -gt 0 -o "${FC_BASENAME_GFORTRAN}" -gt 0; then
         FFLAGS="-Wall -fno-second-underscore $FFLAGS"
      else
         FFLAGS="-Wall -fno-second-underscore -fno-f2c $FFLAGS"
      fi
fi
if test $GCC = yes; then
      if test $CC_BASENAME = icc; then
         CFLAGS="-Wall $CFLAGS"
      else
         CFLAGS="-Wall --pedantic -Wno-long-long $CFLAGS"
      fi
fi
if test $GXX = yes; then
      if test $CXX_BASENAME = icpc; then
         CXXFLAGS="-Wall $CXXFLAGS"
      else
         CXXFLAGS="-Wall --pedantic -Wno-long-long $CXXFLAGS"
      fi
fi

# Remove optimization on all systems for all older gcc
if test $GCC = yes; then
  changequote(,)
  if test `$CC -v 2> /dev/null | grep -c 'version 2\.[45678]'` -ne 0; then
    COPT=none
  fi
  changequote([,])
fi

# If optimizations have not yet been set, use the defaults:
if test "x$COPT" = x; then COPT="$COPT_DEFAULT"; fi
if test "x$CXXOPT" = x; then CXXOPT="$CXXOPT_DEFAULT"; fi
if test "x$FOPT" = x; then FOPT="$FOPT_DEFAULT"; fi

# If optimization is "none" (either given on command line or because symbols
# are enabled), remove optimization:
if test "x$COPT" = xnone; then COPT=; fi
if test "x$CXXOPT" = xnone; then CXXOPT=; fi
if test "x$FOPT" = xnone; then FOPT=; fi

#-------------------------------------------------------------------------------
# F77LIBS4C:
#-------------------------------------------------------------------------------
# Get paths to C++ libraries to add to F77LIBS4C when mixing GNU versions:
echo "int main(){return(0);}" > /tmp/$$.c
$CXX $CXXFLAGS -v -o /tmp/$$.out /tmp/$$.c 2> /tmp/$$.log
if test `cat /tmp/$$.log | grep -c collect2` -ne 0; then
    cxx_lib_paths=`grep collect2 /tmp/$$.log | tr " " "\012" | grep "^\-L" | tr "\012" " "`
else
    cxx_lib_paths=`grep ld /tmp/$$.log | tr " " "\012" | grep "^\-L" | tr "\012" " "`
fi
cp /tmp/$$.log cxxtest.out
rm -rf /tmp/$$.*

# Fortran system libraries which must be supplied when linking with C/C++
echo "      program spud" > /tmp/$$.f
echo "      end" >> /tmp/$$.f
$FC $FFLAGS -v -o /tmp/$$.out /tmp/$$.f 2> /tmp/$$.log
F77LIBS4C=$cxx_lib_paths
if test `cat /tmp/$$.log | grep -c collect2` -ne 0; then
    # Force linker to find the right libgcc & libg2c on Darwin:
    case "${host_os}" in
      darwin6*|darwin7*)
        if test "$FC_BASENAME_GFORTRAN" -gt 0; then
          F77LIBS4C="-L/sw/lib/gcc4.2/lib -L/usr/local/gfortran/lib"
        else
          F77LIBS4C="-L/sw/lib "
        fi
        ;;
      *)
        lhea_libfiles=`grep collect2 /tmp/$$.log | tr " " "\012" | grep "\.a$" | tr "\012" " "`
        if test "x$lhea_libfiles" != x; then
          F77LIBS4C=$lhea_libfiles
        fi
        lhea_paths=`grep collect2 /tmp/$$.log | tr " " "\012" | grep "^\-L" | tr "\012" " "`
        if test "x$lhea_paths" != x; then
          F77LIBS4C="$F77LIBS4C$lhea_paths"
        fi
        ;;
    esac
    # All platforms:
    lhea_libs=`grep collect2 /tmp/$$.log | tr " " "\012" | grep "^\-l" | tr "\012" " "`
    if test "x$lhea_libs" != x; then
      F77LIBS4C="$F77LIBS4C$lhea_libs"
    fi
else
    lhea_libfiles=`grep ld /tmp/$$.log | tr " " "\012" | grep "\.a$" | tr "\012" " "`
    if test "x$lhea_libfiles" != x; then
      F77LIBS4C=$lhea_libfiles
    fi
    lhea_paths=`grep ld /tmp/$$.log | tr " " "\012" | grep "^\-L" | tr "\012" " "`
    if test "x$lhea_paths" != x; then
      F77LIBS4C="$F77LIBS4C$lhea_paths"
    fi
    lhea_libs=`grep ld /tmp/$$.log | tr " " "\012" | grep "^\-l" | tr "\012" " "`
    if test "x$lhea_libs" != x; then
      F77LIBS4C="$F77LIBS4C$lhea_libs"
    fi
fi
#
# special-case additions to Fortran libraries on link line:
case ${UNAME_SYSTEM} in
  Darwin*)
      # Remove crt* & libgcc* references. They are implictly included.
      F77LIBS4C=`echo $F77LIBS4C | tr " " "\012" | grep -v lcrt | grep -v lgcc | tr "\012" " "`
      # When using Intel Fortran, remove irc libs which cause multiple
      # definition linker errors:
      if test "${FC_BASENAME_IFORT}" -gt 0; then
         F77LIBS4C=`echo $F77LIBS4C | tr " " "\012" | grep -v "libirc" | tr "\012" " "`
      fi
    ;;
  OSF*)
      F77LIBS4C=`echo $F77LIBS4C | sed 's:-lc *$::' | sed 's:-lc  *::'`
    ;;
  SunOS*)
    AC_CHECK_LIB(f77compat, main, F77LIBS4C="$F77LIBS4C -lf77compat")
    AC_CHECK_LIB(fsu, main, F77LIBS4C="$F77LIBS4C -lfsu")
    # Some f95 compilers (identified by presence of libompstubs) need
    # libdbxFintr paired with the Fortran libraries:
    if test `echo $F77LIBS4C | grep -c ompstubs` -ne 0; then
      AC_CHECK_LIB(dbxFintr, main, F77LIBS4C="$F77LIBS4C -ldbxFintr")
    fi
    # if mixing proprietary Sun Fortran with GNU C or C++,
    # add path specified by -Y P, flag
    if test $G77 = no -a \( $GCC = yes -o $GXX = yes \); then
      lhea_paths=`grep ld /tmp/$$.log | sed 's/.*-Y P,/-L/' | sed 's/ .*/ /' | sed 's/:/ -L/g'`
      F77LIBS4C="$lhea_paths$F77LIBS4C"
    fi
    ;;
  *)
    ;;
esac
cp /tmp/$$.log fortest.out
rm -f /tmp/$$.*
AC_SUBST(F77LIBS4C)

#-------------------------------------------------------------------------------
# Shared library section
#-------------------------------------------------------------------------------
HD_LIB_STYLE_F77=$HD_LIB_STYLE
SHLIB_SUFFIX=".so"
DYNPERL_SUFFIX=".so"
hd_shlib_cflags=
hd_shlib_cxxflags=
hd_shlib_fflags=
case ${UNAME_SYSTEM} in
  CYGWIN*)
    SHLIB_LD="${CC} -shared"
    SHLIB_LD_CXX="${CXX} -shared"
    SHLIB_SUFFIX=".dll"
    DYNPERL_SUFFIX=".dll"
    # Dropped use of HD_LIB_STYLE_CYGWIN since HD_LIB_STYLE_F77 is
    # starting to trump it.
    HD_LIB_STYLE_F77=static
    ;;
  Darwin)
    SHLIB_LD="${CC} -dynamiclib -single_module -flat_namespace -undefined suppress"
    SHLIB_LD_CXX="${CXX} -dynamiclib -flat_namespace -undefined suppress"
    SHLIB_SUFFIX=".dylib"
    DYNPERL_SUFFIX=".bundle"
    hd_shlib_cflags='-fPIC -fno-common'
    if test $FC_BASENAME_IFORT -gt 0; then
      hd_shlib_fflags='-fPIC -common-args'
    else
      hd_shlib_fflags='-fPIC -fno-common'
    fi
    # Fortran libraries with common blocks cannot be built shared under Darwin.
    # Use HD_LIB_STYLE = ${HD_LIB_STYLE_F77} in library Makefiles to override
    # HD_LIB_STYLE if this condition applies.
    HD_LIB_STYLE_F77=static
    ;;
  hpu)
    SHLIB_LD="ld -b"
    SHLIB_LD_CXX="ld -b"
    SHLIB_SUFFIX=".sl"
    ;;
  Linux)
    SHLIB_LD=":"
    SHLIB_LD_CXX=":"
    if test "x${CC_BASENAME}" = xicc; then
      SHLIB_LD="icc -shared"
    fi
    if test "x${CXX_BASENAME}" = xicc; then
      SHLIB_LD_CXX="icc -shared"
    fi
    ;;
  OSF1)
    SHLIB_LD="ld -shared -expect_unresolved '*'"
    SHLIB_LD_CXX="ld -shared -expect_unresolved '*'"
    LDFLAGS_C="$LDFLAGS_C -taso"
    LDFLAGS_CXX="$LDFLAGS_CXX -taso"
    LDFLAGS_F="$LDFLAGS_F -taso"
    ;;
  SunOS)
    SHLIB_LD='${CC} -G'
    SHLIB_LD_CXX='${CXX} -G'
    hd_shlib_cflags="-KPIC"
    hd_shlib_cxxflags="-KPIC"
    hd_shlib_fflags="-KPIC"
    ;;
  IRIX)
    SHLIB_LD="ld -shared -rdata_shared"
    SHLIB_LD_CXX="ld -shared -rdata_shared"
    ;;
  *)
    AC_MSG_WARN(Unable to determine how to make a shared library)
    ;;
esac
# Darwin uses gcc, but uses -dynamiclib flag
if test $GCC = yes -a ${UNAME_SYSTEM} != Darwin; then
  SHLIB_LD="${CC} -shared"
  hd_shlib_cflags='-fPIC'
fi
if test $GXX = yes -a ${UNAME_SYSTEM} != Darwin; then
  SHLIB_LD_CXX="${CXX} -shared"
  hd_shlib_cxxflags='-fPIC'
fi
if test $G77 = yes -a ${UNAME_SYSTEM} != Darwin; then
  hd_shlib_fflags='-fPIC'
fi
# When using Intel Fortran (ifort) G77=no, but we still need fPIC:
if test $FC_BASENAME_IFORT -gt 0 -a ${UNAME_SYSTEM} != Darwin; then
  hd_shlib_fflags='-fPIC'
fi
if test "x$hd_shlib_cflags" != x; then
  CFLAGS="$CFLAGS $hd_shlib_cflags"
fi
if test "x$hd_shlib_cxxflags" != x; then
  CXXFLAGS="$CXXFLAGS $hd_shlib_cxxflags"
fi
if test "x$hd_shlib_fflags" != x; then
  FFLAGS="$FFLAGS $hd_shlib_fflags"
fi
# Linker flags:
if test $h_ldopt = yes; then
   LDFLAGS_C="$COPT $LDFLAGS_C"
   LDFLAGS_CXX="$CXXOPT $LDFLAGS_CXX"
   LDFLAGS_F="$FOPT $LDFLAGS_F"
fi

AC_SUBST(HD_LIB_STYLE_F77)
AC_SUBST(LDFLAGS_C)
AC_SUBST(LDFLAGS_CXX)
AC_SUBST(LDFLAGS_F)
AC_SUBST(SHLIB_LD)
AC_SUBST(SHLIB_LD_CXX)
AC_SUBST(SHLIB_SUFFIX)
AC_SUBST(DYNPERL_SUFFIX)

#-------------------------------------------------------------------------------
# Recursively configure all selected components.
#-------------------------------------------------------------------------------
# Handle the --without-components case.
if test "$h_components" = no; then
  h_components=""
fi
AC_SUBST(h_components)

for h_component in $h_components; do
  if test `echo $h_configurable_components | grep -c $h_component` -ne 0; then
    h_config_cmd=`$hd_scanenv $hd_conf_file ${h_component}_CONFIGURE_CMD`
    if test $? -ne 0 -o "x$h_config_cmd" = x; then
      AC_MSG_ERROR(Unable to determine how to run configure for $h_component)
    fi
    AC_MSG_RESULT(--------------------------------------------------------------------------------)
    eval echo \"$h_config_cmd\"
    (eval $h_config_cmd)
    if test $? -ne 0; then
       AC_MSG_ERROR(Configure failed for tcltk component $h_component!)
    fi
    AC_MSG_RESULT(--------------------------------------------------------------------------------)
  fi
done

#-------------------------------------------------------------------------------
# begin HEADAS-specific macros
#-------------------------------------------------------------------------------
#AC_DEFINE(printf, pil_printf)
BSUF=
HD_LN_SYM="ln -s"
case ${UNAME_SYSTEM} in
  CYGWIN*)
    BSUF=.exe
    HD_LN_SYM="ln"
    EXT=win
    PGPLOT_SYS=cygwin
    PGPLOT_CONFIG=g77_gcc
    WCSFLAV="Linux"
    ;;
  Darwin*)
    EXT=darwin
    PGPLOT_SYS=darwin
    PGPLOT_CONFIG=g77_gcc_X
#   PGPLOT_CONFIG=g77_gcc_noX
#   PGPLOT_CONFIG=g77_elf
    WCSFLAV="Linux"
    ;;
  IRIX*)
    EXT=irix
    PGPLOT_SYS=irix
    PGPLOT_CONFIG=f77_cc
    ;;
  Linux*)
    EXT=lnx
    if test "x$SIZEOF_LONG" = x8; then
       PGPLOT_SYS=linux64
    else
       PGPLOT_SYS=linux
    fi
    PGPLOT_CONFIG=g77_gcc
#   PGPLOT_CONFIG=g77_elf
    WCSFLAV="Linux"
    ;;
  OSF*)
    EXT=osf
    PGPLOT_SYS=osf1
    PGPLOT_CONFIG=f77_cc
    ;;
  SunOS*)
    EXT=sol
    PGPLOT_SYS=sol2
    pgcc=`echo ${CC} | sed 's:.*/::'`
    if test "x$G77" = xyes; then
       PGPLOT_CONFIG="g77_${pgcc}"
    else
       PGPLOT_CONFIG="f77_${pgcc}"
    fi
    if test $GCC = yes; then
       WCSFLAV="SUN/GNU"
    else
       WCSFLAV="SUN/ANSI"
    fi
    ;;
esac
OSUF="o"
V_CF=
#-------------------------------------------------------------------------------
# end HEADAS-specific macros
#-------------------------------------------------------------------------------

#AC_SUBST(CC)
AC_SUBST(UNAME_SYSTEM)
AC_SUBST(EXT)
AC_SUBST(WCSFLAV)
AC_SUBST(CFLAGS)
AC_SUBST(CXXFLAGS)
AC_SUBST(FFLAGS)
AC_SUBST(V_CF)
AC_SUBST(COPT)
AC_SUBST(CXXOPT)
AC_SUBST(FOPT)
AC_SUBST(BSUF)
AC_SUBST(HD_LN_SYM)
AC_SUBST(OSUF)
AC_SUBST(FV_CFLAGS)
AC_SUBST(FV_LDFLAGS)
AC_SUBST(PGPLOT_CONFIG)
AC_SUBST(PGPLOT_SYS)
AC_SUBST(EXTF77PERL)
AC_SUBST(EXTF77PERL_DIR)
AC_SUBST(PGPLOTPERL)
AC_SUBST(PGPLOTPERL_DIR)
AC_CONFIG_FILES([hmakerc headas-setup Makefile])
AC_OUTPUT

if test -f ./headas-setup; then
  chmod +x ./headas-setup
fi
