#!/bin/sh
#
# Copyright (c) 2005-2007 The 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

start_time=`date '+%s'`

cat <<EOF
Makemake report
===============

EOF

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

BEGIN
EOF
echo -n " * Fixing Autoconf 2.60 issue..."
ac_version=`autoconf --version | head -n 1 | sed -e 's/[ ]*$//;s/.* //;s/.$//'`
if test "${ac_version}" = "2.6"; then
   sed_cmd="s/_AC_SRCPATHS/_AC_SRCDIRS/g"
else
   sed_cmd="s/_AC_SRCDIRS/_AC_SRCPATHS/g"
fi
for f in config/m4/init.m4; do
   sed -e "${sed_cmd}" ${f} > ${f}.tmp
   mv ${f}.tmp ${f}
done
echo "done."

# Update source tree
cat <<EOF
END

Source tree update
------------------

BEGIN
EOF
echo -n " * Updating routines inside 13iovars..."
perl ./util/subs/iovars/make-sub-is_input_variable.pl
echo "done."
echo -n " * 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
  python ./util/maintainers/abilint.py . . 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 -n " * Generating macros for debugging..."
./config/scripts/make-macros-debug
echo "done."
echo -n " * Generating macros for environment variables..."
./config/scripts/make-macros-env
echo "done."
echo -n " * Generating macros for command-line options..."
./config/scripts/make-macros-options
echo "done."
echo -n " * Generating macros for per-directory optimization..."
./config/scripts/make-macros-optflags
echo "done."
echo -n " * Generating macros for external libraries..."
./config/scripts/make-macros-extlibs
echo "done."
echo -n " * Generating macros for optimized linear algebra libraries..."
./config/scripts/make-macros-linalg
echo "done."
echo -n " * Generating macros for configure output..."
./config/scripts/make-macros-output
echo "done."
echo -n " * Generating build examples..."
./config/scripts/make-build-examples
echo "done."
echo "END"
echo ""

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

BEGIN
EOF
echo -n " * Generating makefiles for libraries..."
./config/scripts/make-makefiles-libraries
echo "done."
echo -n " * Generating makefiles for binaries..."
./config/scripts/make-makefiles-binaries
echo "done."
echo -n " * Generating makefiles for tests..."
./config/scripts/make-makefiles-tests
echo "done."
echo -n " * Generating makefiles for utilities..."
./config/scripts/make-makefiles-utils
echo "done."
echo -n " * Generating makefiles for vms..."
./config/scripts/make-makefiles-vms
echo "done."
echo -n " * Generating documentation makefiles..."
./config/scripts/make-makefiles-doc
echo "done."
echo -n " * Generating intermediate makefiles..."
./config/scripts/make-makefiles-inter
echo "done."
echo -n " * Generating top makefile..."
./config/scripts/make-makefiles-top
./config/scripts/add-targets-binaries
echo "done."
echo "END"
echo ""

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

BEGIN
EOF
./config/scripts/build-autotools-framework
echo "END"
echo ""

if test "${abilint_exit_code}" != "0"; then
 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 " ****************************************************************"
 echo ""
fi

end_time=`date '+%s'`

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

EOF
