#!/bin/sh
#
# Copyright (C) 2005-2009 ABINIT Group (Yann Pouillon)
# All rights reserved.
#
# 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.
#

# Stop at first error encountered
set -e

# Check that we are in the right directory
if test ! -s "./configure.ac" -o ! -s "src/main/abinit.F90"; then
 echo "makemake: This is not an ABINIT source tree - aborting now"
 exit 1
fi

# Import functions
. ./config/scripts/functions.sh

# Go!
start_time=`date '+%s'`

# Report header
cat <<EOF
Makemake report
===============

EOF

# Update source tree
cat <<EOF
Source tree update
------------------

BEGIN
EOF

echon " * Updating routines inside 13iovars..."
perl ./util/subs/iovars/make-sub-is_input_variable.pl
echo "done."
echon " * Generating build examples..."
./config/scripts/make-build-examples
echo "done."
echon " * Updating Fortran interfaces..."
with_abilint="yes"
for arg in ${@} ; do
  if test "${arg}" = "--without-abilint" ; then
    with_abilint="no"
  fi
done
if test "${with_abilint}" = "yes" ; then
  ./config/scripts/abilint --dependencies --dump_dtset bindings/parser/dtset.pickle . . 1> abilint.out 2>&1
  abilint_exit_code="${?}"
  if test "${abilint_exit_code}" = "0"; then
   echo "done (output in abilint.out)."
   rm -f abilint.log
  else
   echo "FAILED (output in abilint.[out|log])!"
  fi
else
  abilint_exit_code="0"
  echo "skipped."
fi

echo "END"
echo ""

# Fix build system
cat <<EOF
Build system update
-------------------

BEGIN
EOF

echon " * Resetting configuration dumper..."
rm -f config.dump.in
echo "done."
echon " * Generating macros for the Autotools..."
./config/scripts/make-macros-autotools
echo "done."
echon " * Generating macros for debugging..."
./config/scripts/make-macros-debug
echo "done."
echon " * Generating macros for environment variables..."
./config/scripts/make-macros-env
echo "done."
echon " * Generating macros for command-line options..."
./config/scripts/make-macros-options
echo "done."
echon " * Generating macros for per-directory optimization..."
./config/scripts/make-macros-optflags
echo "done."
echon " * Generating macros for core libraries..."
./config/scripts/make-macros-corelibs
echo "done."
echon " * Generating macros for external libraries..."
./config/scripts/make-macros-plugins
echo "done."
echon " * Generating macros for optimized linear algebra libraries..."
./config/scripts/make-macros-linalg
echo "done."
echon " * Generating macros for configure output..."
./config/scripts/make-macros-output
echo "done."

echo "END"
echo ""

# Generate makefiles
cat <<EOF
Makefiles (for Automake)
------------------------

BEGIN
EOF

echon " * Generating makefiles for core libraries..."
./config/scripts/make-makefiles-corelibs
echo "done."
echon " * Generating makefiles for required external libraries..."
./config/scripts/make-makefiles-prereqs
echo "done."
echon " * Generating makefiles for plug-ins..."
./config/scripts/make-makefiles-plugins
echo "done."
echon " * Generating makefiles for binaries..."
./config/scripts/make-makefiles-binaries
echo "done."
echon " * Generating makefiles for exports..."
./config/scripts/make-makefiles-exports
echo "done."
echon " * Generating makefiles for bindings..."
./config/scripts/make-makefiles-bindings
echo "done."
echon " * Generating makefiles for tests..."
./config/scripts/make-makefiles-tests
echo "done."
echon " * Generating documentation makefiles..."
./config/scripts/make-makefiles-doc
echo "done."
echon " * Generating intermediate makefiles..."
./config/scripts/make-makefiles-inter
echo "done."
echon " * Generating top makefile..."
./config/scripts/make-makefiles-top
./config/scripts/add-targets-binaries
./config/scripts/add-targets-libraries
echo "done."

echo "END"
echo ""

# Build Autotools framework
cat <<EOF
Autotools framework (for Autoconf)
----------------------------------

BEGIN
EOF

./config/scripts/build-autotools-framework

echo "END"
echo ""

# Display warnings
if test -s "./config/local/autotools.sh"; then
 . ./config/local/autotools.sh
else
 echo "WARNING: could not get Autotools version information" >&2
 abi_m4_version=0
 abi_ac_version=0
 abi_am_version=0
 abi_lt_version=0
fi

if test "${abi_m4_version}" -lt "10411"; then
 echo ""
 echo " ****************************************************************"
 echo " ***                        WARNING!                          ***"
 echo " ****************************************************************"
 echo " ***                                                          ***"
 echo " *** Versions of M4 prior to 1.4.11 are known to crash the    ***"
 echo " *** configure script in some situations and have a few       ***"
 echo " *** security issues.                                         ***"
 echo " ***                                                          ***"
 echo " *** Use at your own risks.                                   ***"
 echo " ***                                                          ***"
 echo " ****************************************************************"
fi

if test "${abi_ac_version}" -lt "26100"; then
 echo ""
 echo " ****************************************************************"
 echo " ***                        WARNING!                          ***"
 echo " ****************************************************************"
 echo " ***                                                          ***"
 echo " *** Autoconf 2.59 has been released in 2003 and contains a   ***"
 echo " *** lot of bugs and flaws regarding Fortran 90 support.      ***"
 echo " ***                                                          ***"
 echo " *** Support for versions of Autoconf prior to 2.61 will be   ***"
 echo " *** frozen in 2008. You are thus strongly advised to update  ***"
 echo " *** your version of Autoconf as soon as possible if you      ***"
 echo " *** would like to develop with a fully functional build      ***"
 echo " *** system.                                                  ***"
 echo " ***                                                          ***"
 echo " *** Thanks a lot in advance for your understanding.          ***"
 echo " ***                                                          ***"
 echo " ****************************************************************"
fi

if test "${abi_am_version}" -lt "11000"; then
 echo ""
 echo " ****************************************************************"
 echo " ***                        WARNING!                          ***"
 echo " ****************************************************************"
 echo " ***                                                          ***"
 echo " *** Automake 1.10 contains a bugfixed Fortran 90 support and ***"
 echo " *** is more than highly recommended.                         ***"
 echo " ***                                                          ***"
 echo " *** Using a prior version is still fine for now, but might   ***"
 echo " *** become an issue in the future.                           ***"
 echo " ***                                                          ***"
 echo " *** Thanks a lot in advance for your understanding.          ***"
 echo " ***                                                          ***"
 echo " ****************************************************************"
fi

if test "${abi_lt_version}" -lt "20202"; then
 echo ""
 echo " ****************************************************************"
 echo " ***                        WARNING!                          ***"
 echo " ****************************************************************"
 echo " ***                                                          ***"
 echo " *** Libtool integration into ABINIT has now started. You     ***"
 echo " *** will have to install Libtool 2.2.2 or later if you want  ***"
 echo " *** to benefit from the advanced features it provides.       ***"
 echo " ***                                                          ***"
 echo " *** This is not an issue for now, as only a very small       ***"
 echo " *** number of developers is currently involved in this       ***"
 echo " *** project. Please note however that the build system will  ***"
 echo " *** progressively rely more and more on it.                  ***"
 echo " ***                                                          ***"
 echo " ****************************************************************"
fi

if test "${abilint_exit_code}" != "0"; then
 echo ""
 echo " ****************************************************************"
 echo " ***                        WARNING!                          ***"
 echo " ****************************************************************"
 echo " ***                                                          ***"
 echo " *** The abilint script returned an non-zero exit code. As a  ***"
 echo " *** consequence, the Fortran interfaces will likely produce  ***"
 echo " *** build-time errors. The 'abilint.log' file has been left  ***"
 echo " *** for your investigations. Its standard output has been    ***" 
 echo " *** also redirected to 'abilint.out'.                        ***"
 echo " *** It might be that the cache file (.abilint) has to be     ***"
 echo " *** regenerated. Remove that file and issue abilint again.   ***"
 echo " ***                                                          ***"
 echo " *** Please have a careful look at abilint's outputs before   ***"
 echo " *** sending a bug report.                                    ***"
 echo " ***                                                          ***"
 echo " ****************************************************************"
fi

echo ""

end_time=`date '+%s'`

cat <<EOF
-- 
Time elapsed : `awk "END{print ${end_time}-${start_time}}" < /dev/null`s

EOF
