#!/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

#
# Command-line options 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)



# Define macro header
def macro_define_header():

 return """


# ABI_OPTIONS_DEFINE()
# --------------------
#
# Declares command-line arguments for the "configure" script.
#
AC_DEFUN([ABI_OPTIONS_DEFINE],
[
"""



# Define macro footer
def macro_define_footer():

 return "]) # ABI_OPTIONS_DEFINE\n"



# Init macro header
def macro_init_header():

 return """


# ABI_OPTIONS_INIT()
# ------------------
#
# Sets the default values of command-line arguments.
#
AC_DEFUN([ABI_OPTIONS_INIT],
[
"""



# Init macro footer
def macro_init_footer():

 return "]) # ABI_OPTIONS_INIT\n"



# Backup macro header
def macro_backup_header():

 return """


# ABI_OPTIONS_BACKUP()
# --------------------
#
# Saves all command-line arguments.
#
AC_DEFUN([ABI_OPTIONS_BACKUP],
[
"""



# Backup macro footer
def macro_backup_footer():

 return "]) # ABI_OPTIONS_BACKUP\n"



# Restore macro header
def macro_restore_header():

 return """


# ABI_OPTIONS_RESTORE()
# ---------------------
#
# Restores all previously-saved command-line arguments.
#
AC_DEFUN([ABI_OPTIONS_RESTORE],
[
"""



# Restore macro footer
def macro_restore_footer():

 return "]) # ABI_OPTIONS_RESTORE\n"



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

#
# Main program
#

# Initial setup
my_name    = "make-macros-options"
my_configs = ["config/specs/options.cf"]
my_output  = "config/m4/do-not-edit-options.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 define macro
m4.write(macro_define_header())

# Process arguments
defaults = ""
for arg in ("enable","with"):
 m4.write("\n dnl\n dnl --%s arguments\n dnl\n" % (arg))
 defaults += "\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("\n AC_ARG_%s(%s,\n" % (arg.upper(),opt[0]) \
   + "  AC_HELP_STRING([--%s-%s],\n   [%s (default: %s)]))\n" % \
    (arg,opt[0],opt[1],opt[2]))

  if ( opt[2] != None ):
   defaults += " if test \"${%s_%s}\" = \"\"; then\n  %s_%s=\"%s\"\n fi\n" % \
    (arg,var,arg,var,opt[2])

  m4.write(" AC_SUBST(%s_%s)\n" % (arg,var))

# Finish writing define macro
m4.write(macro_define_footer())

# Start writing init macro
m4.write(macro_init_header())

# Process arguments
m4.write(defaults)

# Finish writing init macro
m4.write(macro_init_footer())

# Start writing backup macro
m4.write(macro_backup_header())

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

# Do not forget "prefix"
m4.write("""
 dnl
 dnl Prefix
 dnl
 if test "${prefix}" != "NONE"; then
  cmd_prefix="${prefix}"
 fi
""")

# Finish writing backup macro
m4.write(macro_backup_footer())

# Start writing restore macro
m4.write(macro_restore_header())

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

# Do not forget "prefix"
m4.write("""
 dnl
 dnl Prefix
 dnl
 if test "${cmd_prefix}" != ""; then
  prefix="${cmd_prefix}"
 fi
""")

# Finish writing restore macro
m4.write(macro_restore_footer())

m4.close()

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