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

#
# Per-directory optimization support
#

#
# 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_OPTFLAGS_DIRS(FCFLAGS_OPT)
# ------------------------------
#
# Initializes optimization flags on a per-directory basis. Currently
# limited to Fortran.
#
AC_DEFUN([ABI_OPTFLAGS_DIRS],
[dnl Check arguments
 m4_if([$1], , [AC_FATAL([$0: missing argument 1])])dnl

 fcflags_opt_default="$1"

 if test "${enable_debug}" = "no"; then
  AC_MSG_NOTICE([enabling per-directory Fortran optimizations])
 fi
""" % (name,stamp,name)



# Variable test
def macro_optflags(name):

 return """
 dnl %s library
 if test "${fcflags_opt_%s}" = ""; then
  fcflags_opt_%s="${fcflags_opt_default}"
 else
  if test "${enable_debug}" = "no"; then
   AC_MSG_NOTICE([optimization for %s is ${fcflags_opt_%s}])
  else
   fcflags_opt_%s="${fcflags_opt_default}"
  fi
 fi
 AC_SUBST(fcflags_opt_%s)
""" % (name,name,name,name,name,name,name)



# Macro footer
def macro_footer():

 return "]) # ABI_OPTFLAGS_DIRS\n"



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

#
# Main program
#

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

# Process each library
for lib in abinit_libs:

 # Init
 if ( lib in abilibs_specs ):
  specs = abilibs_specs[lib]
 else:
  specs = ABI_LIB_NIL

 # Write optflags assignment
 if ( (specs & ABI_LIB_OWN) == 0 ):
  m4.write(macro_optflags(lib))

# Write macro for the main binaries
m4.write(macro_optflags("main"))

# The end
m4.write(macro_footer())
m4.close()

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