#!/usr/bin/env python
#
# 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.
#

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

#
# Configuration dumper
#

#
# IMPORTANT NOTE
#
# This file has been automatically generated by the %s
# script. Any change will systematically be overwritten.
#
""" % (name,stamp,name)



# Init macro header
def macro_dump_header():

 return """


# ABI_DUMP_CONFIG()
# -----------------
#
# Dumps ABINIT configuration to standard error.
#
AC_DEFUN([ABI_DUMP_CONFIG],
[
"""



# Init macro footer
def macro_dump_footer():

 return "]) # ABI_DUMP_CONFIG\n"



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

#
# Main program
#

# Initial setup
my_name    = "make-macros-debug"
my_configs = ["config/specs/env.cf","config/specs/options.cf"]
my_output  = "config/m4/do-not-edit-debug.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/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)

# Read config file(s)
for cnf in my_configs:
 if ( os.path.exists(cnf) ):
  execfile(cnf)
 else:
  print "%s: Could not find config file (%s)." % (my_name,cnf)
  print "%s: Aborting now." % my_name
  sys.exit(2)

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

# Start writing macro
m4 = file(my_output,"w")
m4.write(macro_header(my_name,now))

# Start writing dump macro
m4.write(macro_dump_header())

# Process environment variables
m4.write("\n dnl\n dnl Environment variables\n dnl\n\n")
for var in abinit_env_vars:
 m4.write(" AC_MSG_WARN([%s = ${%s}])\n" % (var[0],var[0]))

# Process arguments
for arg in ("enable","with"):
 m4.write("\n dnl\n dnl --%s arguments\n dnl\n\n" % (arg))
 for opt in eval("ac_args_%s" % (arg)):
  var = re.sub("-","_",opt[0])
  m4.write(" AC_MSG_WARN([%s_%s = ${%s_%s}])\n" % (arg,var,arg,var))

# Finish writing dump macro
m4.write(macro_dump_footer())

m4.close()

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