# -*- Autoconf -*-
#
# Copyright (C) 2014-2021 ABINIT Group (Yann Pouillon)
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#

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

#
# IMPORTANT NOTE
#
# Please DO NOT EDIT this file unless you REALLY know what you are doing.
# Everything is important, in particular the order of the various commands
# executed here. YOU HAVE BEEN WARNED!
#

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

#
# Autoconf & Automake startup
#

# Initialize Autoconf
AC_PREREQ(2.62)
AC_INIT([ABINIT-Bindings],[9.6.2],
  [https://bugs.launchpad.net/abinit/],[abinit-bindings])
AC_REVISION([Autotools support for the ABINIT Bindings])
AC_CONFIG_AUX_DIR([config/gnu])
AC_CONFIG_MACRO_DIR([config/m4])
AC_CONFIG_SRCDIR([libpaw/README])
_AC_SRCDIRS(["."])

# Initial setup
AC_CANONICAL_TARGET
AM_INIT_AUTOMAKE(1.10)

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

#
# Environment variables
#

AC_ARG_VAR([CC_LDFLAGS],
  [C link flags])
AC_SUBST(CC_LDFLAGS)

AC_ARG_VAR([XPP],
  [C++ preprocessor])
AC_SUBST(XPP)

AC_ARG_VAR([XPPFLAGS],
  [C++ preprocessor flags])
AC_SUBST(XPPFLAGS)

AC_ARG_VAR([CXX_LDFLAGS],
  [C++ link flags])
AC_SUBST(CXX_LDFLAGS)

AC_ARG_VAR([FPP],
  [Fortran preprocessor])
AC_SUBST(FPP)

AC_ARG_VAR([FPPFLAGS],
  [Fortran preprocessor flags])
AC_SUBST(FPPFLAGS)

AC_ARG_VAR([FC_LDFLAGS],
  [Fortran link flags])
AC_SUBST(FC_LDFLAGS)

AC_ARG_VAR([PYTHON],
  [Path to the Python interpreter])
AC_SUBST(PYTHON)

AC_ARG_VAR([PYFLAGS],
  [Python interpreter flags])
AC_SUBST(PYFLAGS)

AC_ARG_VAR([PYTHON_CPPFLAGS],
  [Python-related C preprocessing flags])
AC_SUBST(PYTHON_CPPFLAGS)

AC_ARG_VAR([AR],
  [Library archiver command])
AC_SUBST(AR)

AC_ARG_VAR([ARFLAGS],
  [Library archiver flags])
AC_SUBST(ARFLAGS)

AC_ARG_VAR([RANLIB],
  [Library indexer command])
AC_SUBST(RANLIB)

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

#
# Optional arguments
#

AC_ARG_WITH([abinit-builddir],
  AS_HELP_STRING([--with-abinit-builddir],
    [Top build directory of Abinit]))

AC_ARG_WITH([abinit-srcdir],
  AS_HELP_STRING([--with-abinit-srcdir],
    [Top source directory of Abinit]))

AC_ARG_WITH(abinit-config,
  AS_HELP_STRING([--with-abinit-config],
    [Full path to the config.sh file produced by Abinit]))
AC_SUBST(with_abinit_config)

AC_ARG_WITH(fc-vendor,
  AS_HELP_STRING([--with-fc-vendor],
    [Fortran compiler vendor, as defined for Abinit]))
AC_SUBST(with_fc_vendor)

AC_ARG_WITH(fc-version,
  AS_HELP_STRING([--with-fc-version],
    [Fortran compiler version, as defined for Abinit]))
AC_SUBST(with_fc_version)

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

#
# Import Abinit configuration
#

bnd_config_sh=""
bnd_config_sh_cmd="${with_abinit_config}"
bnd_config_sh_build="${ac_abs_top_builddir}/config.sh"
bnd_config_sh_src="${ac_abs_top_srcdir}/config.sh"

if test "${bnd_config_sh_cmd}" != ""; then
  if test -s "${bnd_config_sh_cmd}"; then
    AC_MSG_NOTICE([reading "${bnd_config_sh_cmd}"])
    . "${bnd_config_sh_cmd}"
    bnd_config_sh="${bnd_config_sh_cmd}"
  else
    AC_MSG_ERROR([user-defined Abinit config file not found:
        ${bnd_config_sh_cmd}])
  fi
fi

if test "${bnd_config_sh}" = ""; then
  if test -s "${bnd_config_sh_build}"; then
    AC_MSG_NOTICE([reading "${bnd_config_sh_build}"])
    . "${bnd_config_sh_build}"
    bnd_config_sh="${bnd_config_sh_build}"
  fi
fi
if test "${bnd_config_sh}" = ""; then
  if test -s "${bnd_config_sh_src}"; then
    AC_MSG_NOTICE([reading "${bnd_config_sh_src}"])
    . "${bnd_config_sh_src}"
    bnd_config_sh="${bnd_config_sh_src}"
  fi
fi

if test "${bnd_config_sh}" != ""; then
 bnd_ac_distcheck="${bnd_ac_distcheck} --with-abinit-config=\"${bnd_config_sh}\""
fi

AC_SUBST(bnd_ac_distcheck)

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

#
# Check option consistency
#

abinit_builddir="${with_abinit_builddir}"
if test "${abinit_builddir}" = "" -o \
        "${abinit_builddir}" = "no" -o \
        "${abinit_builddir}" = "yes" -o \
        ! -d "${abinit_builddir}"; then
  AC_MSG_ERROR([Invalid or blank Abinit top build directory])
fi

abinit_srcdir="${with_abinit_srcdir}"
if test "${abinit_srcdir}" = "" -o \
        "${abinit_srcdir}" = "no" -o \
        "${abinit_srcdir}" = "yes" -o \
        ! -d "${abinit_srcdir}"; then
  AC_MSG_ERROR([Invalid or blank Abinit top build directory])
fi

AC_SUBST(abinit_builddir)
AC_SUBST(abinit_srcdir)

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

#
# Look for binding prerequisites
#

ABIBND_CHECK_PREREQS

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

#
# Look for utilities
#

# Check for common programs
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_SED
AC_PROG_AWK
AC_PROG_GREP
AC_CHECK_PROGS([TAR],[tar])
AC_CHECK_PROGS([FIND],[find])

# Workaround for the "grep -e" issue on Solaris systems
AC_PROG_EGREP

# Workaround for the wrong path to install-sh on Mac systems
AX_PROG_MKDIR_P

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

#
# C support
#

AC_PROG_CC
AC_PROG_CPP

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

#
# Fortran support
#

AC_PROG_FC

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

#
# Python support
#

# Look for programs
if test "${PYTHON}" = ""; then
  AC_CHECK_PROGS(PYTHON,[python python2.7 python2.6 python2.5 python2.4])
fi

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

#
# Final adjustments
#

libpaw_version=`cat "${ac_top_srcdir}/libpaw/libpaw-utils/VERSION"`
AC_SUBST(libpaw_version)

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

#
# Output configuration
#

ABIBND_OUTPUT
