#!/usr/bin/env python
#
# Copyright (C) 2009-2014 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.
#

from time import gmtime,strftime

import commands
import os
import re
import sys

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

#
# Subroutines
#

# Macro header
def macro_header(name,stamp):

  return """# Generated by %s on %s

#
# ABINIT Autotools info dumper for the "configure" script
#

#
# IMPORTANT NOTE
#
# This file has been automatically generated by the %s
# script. If you try to edit it, your changes will systematically be
# overwritten.
#
""" % (name,stamp,name)



# Autotools info macro
def macro_info(m4_version,ac_version,am_version,lt_version):

  return """


# ABI_INFO_AUTOTOOLS()
# --------------------
#
# Make information about the Autotools available.
#
AC_DEFUN([ABI_INFO_AUTOTOOLS],[
  dnl Store version numbers
  abi_m4_version="%6.6d"
  abi_ac_version="%6.6d"
  abi_am_version="%6.6d"
  abi_lt_version="%6.6d"

  dnl Display version information
  AC_MSG_NOTICE([M4 ${abi_m4_version} - Autoconf ${abi_ac_version} - Automake ${abi_am_version} - Libtool ${abi_lt_version}])

  dnl Substitute variables
  AC_SUBST(abi_m4_version)
  AC_SUBST(abi_ac_version)
  AC_SUBST(abi_am_version)
  AC_SUBST(abi_lt_version)
]) # ABI_INFO_AUTOTOOLS
""" % (m4_version,ac_version,am_version,lt_version)



# Init macro for source and build dirs
def macro_init_dirs(ac_version):

  if ( ac_version > 25900 ):
    ac_macro = "_AC_SRCDIRS"
  else:
    ac_macro = "_AC_SRCPATHS"

  ret = """


# ABI_INIT_DIRS()
# ---------------
#
# Set paths to source and build directories.
#
AC_DEFUN([ABI_INIT_DIRS],[
  dnl Set paths (needed by other ABINIT macros)
  %s(["."])
  abinit_srcdir="${ac_abs_top_srcdir}"
  abinit_builddir="${ac_abs_top_builddir}"
  abinit_moddir="${abinit_builddir}/src/mods"

  export abinit_srcdir abinit_builddir

  AC_SUBST(abinit_srcdir)
  AC_SUBST(abinit_builddir)
  AC_SUBST(abinit_moddir)
]) # ABI_INIT_DIRS
""" % (ac_macro)

  return ret



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

#
# Main program
#

# Initial setup
my_name    = "make-macros-autotools"
my_output  = "config/m4/auto-autotools.m4"

# Check if we are in the top of the ABINIT source tree
if ( not os.path.exists("configure.ac") or
          not os.path.exists("src/98_main/abinit.F90") ):
  print "%s: You must be in the top of an ABINIT source tree." % my_name
  print "%s: Aborting now." % my_name
  sys.exit(1)

# What time is it?
now = strftime("%Y/%m/%d %H:%M:%S +0000",gmtime())

# Get Autotools versions
at_file = "./config/local/autotools.sh"
if ( os.path.exists(at_file) ):
  at_versions = file(at_file, "r").read()
  exec(at_versions)
  m4_version = int(abi_m4_version)
  ac_version = int(abi_ac_version)
  am_version = int(abi_am_version)
  lt_version = int(abi_lt_version)
else:
  m4_version = 0
  ac_version = 0
  am_version = 0
  lt_version = 0
  sys.stderr.write(
    "[%s] WARNING: could not get Autotools version information" % \
      (my_name))

# Write macros
m4 = file(my_output,"w")
m4.write(macro_header(my_name,now))
m4.write(macro_info(m4_version,ac_version,am_version,lt_version))
m4.write(macro_init_dirs(ac_version))
m4.close()

tmp = commands.getoutput("./config/scripts/add-header-typed Autoconf %s" % (my_output))
if ( tmp != "" ):
  print tmp
