#!/bin/sh
#
# Copyright (C) 2005-2010 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.
#

# Stop at first error encountered
set -e

# Check that we are in the right directory
if test ! -s "./configure.ac" -o ! -s "src/98_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

# Process options
with_abilint="yes"
with_autotools="yes"
for arg in ${@} ; do
  if test "${arg}" = "--without-abilint" ; then
    with_abilint="no"
    echo "makemake: will not run abilint" >&2
  fi
  if test "${arg}" = "--without-autotools" ; then
    with_autotools="no"
    echo "makemake: will not rebuild the autotools framework" >&2
  fi
done

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

BEGIN
EOF

echon " * Generating CPP option dumper..."
./config/scripts/make-cppopts-dumper
echo "done."
echon " * Updating routines inside 57_iovars..."
perl ./util/subs/iovars/make-sub-is_input_variable.pl
echo "done."
echon " * Generating build examples..."
./config/scripts/clean-build-examples
./config/scripts/make-build-examples
echo "done."
echon " * Updating Fortran interfaces..."
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

# This script requires 'bindings/parser/dtset.pickle' file generated by abilint.
echon " * Generating dtset source files..."
./config/scripts/make-sources-parser
echo "done."

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 dumpers..."
./config/scripts/make-macros-dumpers
echo "done."
echon " * Generating macros for environment variables..."
./config/scripts/make-macros-environment
echo "done."
echon " * Generating macros for command-line options..."
./config/scripts/make-macros-options
echo "done."
echon " * Generating macros for hints..."
./config/scripts/make-macros-hints
echo "done."
echon " * Generating macros for debugging..."
./config/scripts/make-macros-debug
echo "done."
echon " * Generating macros for default optimizations..."
./config/scripts/make-macros-optim
echo "done."
echon " * Generating macros for per-directory optimizations..."
./config/scripts/make-macros-dirflags
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 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 nightly..."
./config/scripts/make-makefiles-nightly
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

if test "${with_autotools}" = "yes"; then
  ./config/scripts/build-autotools-framework
else
  echo "  *** SKIPPED ***"
fi

echo "END"
echo ""

# Check branch parameters
if bzr --version >/dev/null 2>&1; then
  cat <<EOF
Branch parameters
-----------------

BEGIN
EOF

  branch_info=`bzr version-info --custom --template="{branch_nick} {revno} {clean}\n"`
  branch_name=`echo "${branch_info}" | awk '{print $1}'`
  branch_revno=`echo "${branch_info}" | awk '{print $2}'`
  branch_status=`echo "${branch_info}" | awk '{print $3}'`
  echo " * BN: ${branch_name} BR: ${branch_revno} BS: ${branch_status}"
  branch_version=`echo "${branch_name}" | sed 's/-//g ; s/_//g ; s/[a-z]//g'  | grep '^[0-9].'` 
  echo " * B: ${branch_version}"
  config_version=`grep AC_INIT configure.ac | sed -e 's/[^,]*,\[\([0-9\.]*\).*/\1/'`
  echo " * C: ${config_version}"
  echo " * branch revision: ${branch_revno}"
  if test "${branch_status}" = "1"; then
    echo " * branch is committed"
  else
    echo " * branch has uncommitted changes"
  fi
fi

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 "26300"; then
  echo ""
  echo " ****************************************************************"
  echo " ***                        WARNING!                          ***"
  echo " ****************************************************************"
  echo " ***                                                          ***"
  echo " *** Autoconf 2.63 fixes regressions, Fortran-related bugs,   ***"
  echo " *** and performance issues, introduced by Autoconf 2.62 and  ***"
  echo " *** earlier versions. You are thus strongly advised to       ***"
  echo " *** upgrade your version of Autoconf if you want to          ***"
  echo " *** contribute to Abinit 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 "11001"; then
  echo ""
  echo " ****************************************************************"
  echo " ***                        WARNING!                          ***"
  echo " ****************************************************************"
  echo " ***                                                          ***"
  echo " *** Automake 1.10 has been released in 2006 and contains     ***"
  echo " *** several portability issues. More recent versions are     ***"
  echo " *** now available which fix many long-standing bugs.         ***"
  echo " ***                                                          ***"
  echo " *** You are strongly advised to upgrade your version of      ***"
  echo " *** Automake at your earliest convenience.                   ***"
  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 " *** Some features of the build system will be disabled until ***"
  echo " *** you install a proper version of Libtool.                 ***"
  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

if test "${branch_version}" != "" -a \
        "${branch_version}" != "${config_version}"; then
  echo ""
  echo " ****************************************************************"
  echo " ***                        WARNING!                          ***"
  echo " ****************************************************************"
  echo " ***                                                          ***"
  echo " *** The version number contained in the branch name does not ***"
  echo " *** match that found in configure.ac. You might want to      ***"
  echo " *** address this consistency issue before continuing.        ***"
  echo " ***                                                          ***"
  echo " ****************************************************************"
fi

echo ""

end_time=`date '+%s'`

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

EOF
