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

#
# Info to be made available for the core libraries of ABINIT
#

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



# ABI_CORELIBS_INIT()
# -------------------
#
# Sets all variables needed to handle the core libraries.
#
AC_DEFUN([ABI_CORELIBS_INIT],
[dnl Set include dirs
@INCDIRS@
 dnl Substitute variables
@SUBSTS@
]) # ABI_CORELIBS_INIT
""" % (name,stamp,name)



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

#
# Main program
#

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

# Init output
inc = ""
sub = ""

# Process libraries
for lib in abinit_corelibs:
 inc += " src_%s_includes='-I$(top_builddir)/src/%s -I$(top_srcdir)/src/%s'\n" % \
  (lib,lib,lib)
 sub += " AC_SUBST(src_%s_includes)\n" % (lib)

# Update macro
mac = macro_header(my_name,now)
mac = re.sub("@INCDIRS@",inc,mac)
mac = re.sub("@SUBSTS@",sub,mac)

# Finish
m4 = file(my_output,"w")
m4.write(mac)
m4.close()

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