#!/usr/bin/env python
#
# Copyright (c) 2006-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

#
# Environment variables relevant to ABINIT
#

#
# 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_init_header():

 return """


# ABI_ENV_INIT()
# --------------
#
# Declares ABINIT environment variables and sets their default values.
#
AC_DEFUN([ABI_ENV_INIT],
[
"""



# Init macro footer
def macro_init_footer():

 return "]) # ABI_ENV_INIT\n"



# Backup macro header
def macro_backup_header():

 return """


# ABI_ENV_BACKUP()
# ----------------
#
# Saves all ABINIT environment variables.
#
AC_DEFUN([ABI_ENV_BACKUP],
[dnl All variables will be saved, yet please note that they are conditionally
 dnl restored. Empty ones might be ignored (see ABI_ENV_RESTORE for details).
"""



# Backup macro footer
def macro_backup_footer():

 return "]) # ABI_ENV_BACKUP\n"



# Restore macro header
def macro_restore_header():

 return """


# ABI_ENV_RESTORE()
# -----------------
#
# Restores all previously-saved ABINIT environment variables.
#
AC_DEFUN([ABI_ENV_RESTORE],
[dnl The following ensures that non-empty environment variables always override
 dnl what is read from the config files. For now, we let the config file define
 dnl empty environment variables though.
"""



# Restore macro footer
def macro_restore_footer():

 return "]) # ABI_ENV_RESTORE\n"



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

#
# Main program
#

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

# Process environment variables
for var in abinit_env_vars:
 if ( len(var) > 2 ):
  m4.write("\n dnl %s\n" % (var[1]))
  m4.write(" if test \"${%s}\" = \"\"; then\n  %s=\"%s\"\n fi\n" % \
   (var[0],var[0],var[2]))
  m4.write(" AC_SUBST(%s)\n" % (var[0]))

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

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

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

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

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

# Process environment variables
for var in abinit_env_vars:
 m4.write("\n dnl Restore %s\n" % (var[1]))
 m4.write(" if test \"${abi_env_%s}\" != \"\"; then\n" % (var[0]) \
  + "  %s=\"${abi_env_%s}\"\n fi\n" % (var[0],var[0]))

# 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
