#!/usr/bin/env python
#
# Copyright (C) 2005-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 ConfigParser import ConfigParser
from time import gmtime,strftime

import commands
import os
import re
import sys

class MyConfigParser(ConfigParser):

  def optionxform(self,option):
    return str(option)

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

#
# 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-dumpers"
my_configs = [
  "config/specs/environment.conf",
  "config/specs/options.conf"]
my_output  = "config/m4/auto-dumpers.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)

# Check presence of config file(s)
for cnf in my_configs:
  if ( not os.path.exists(cnf) ):
    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
cnf = MyConfigParser()
cnf.read(my_configs[0])
abinit_env_vars = cnf.sections()
abinit_env_vars.sort()
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,var))

# Process arguments
cnf = MyConfigParser()
cnf.read(my_configs[1])
ac_args = { "enable":list(), "with":list() }
re_en = re.compile("enable_")
re_wi = re.compile("with_")
for arg in cnf.sections():
  arg_stat = cnf.get(arg,"status")
  if ( arg_stat != "removed" ):
    if ( re_en.match(arg) ):
      ac_args["enable"].append(arg)
    if ( re_wi.match(arg) ):
      ac_args["with"].append(arg)
ac_args["enable"].sort()
ac_args["with"].sort()

for arg in ("enable","with"):
  m4.write("\n  dnl\n  dnl --%s arguments\n  dnl\n\n" % (arg))
  for opt in ac_args[arg]:
    m4.write("  AC_MSG_WARN([%s = ${%s}])\n" % (opt,opt))

# 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
