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



# Init macro header
def macro_extlib(name,include,tricks):

 build_flags = ("CPPFLAGS","CFLAGS","CXXFLAGS","FCFLAGS")

 ret = """


# ABI_EXTLIB_%s()
# -----------%s--
#
# Sets all variables needed to handle the %s external library.
#
AC_DEFUN([ABI_EXTLIB_%s],
[dnl Initial setup
 %s_include=""
 %s_ldflags=""

 dnl Define variables needed to build the library
""" % (name.upper(),re.sub(".","-",name),name.upper(),name.upper(),name,name)

 for flag in build_flags:
  ret += " if test -z \"${%s_%s}\"; then\n  %s_%s=\"${%s}\"\n fi\n" % \
   (flag,name.upper(),flag,name.upper(),flag)
  ret += " AC_SUBST(%s_%s)\n" % (flag,name.upper())

 ret += """
 dnl Add optimizations for Fortran flags
 FCFLAGS_%s="${FCFLAGS_%s} ${FCFLAGS_OPT}"

 dnl Output library status
 AC_MSG_CHECKING([whether to use the %s library])
 AC_MSG_RESULT([${enable_%s}])

 dnl Determine what to do
 if test "${enable_%s}" = "yes"; then
  AC_DEFINE([HAVE_%s],1,[Define to 1 if you want to use the %s bindings])

""" % (name.upper(),name.upper(),name.upper(),name,name,name.upper(),
       name.upper())

 if ( include != None ):
  ret += """  if test "${with_%s_include}" = "" -o "${with_%s_ldflags}" = "" -o \
          "${enable_extlibs}" = "no"; then
   %s_include="-I\$(abinit_builddir)/lib/%s"
""" % (name,name,name,name)
 else:
  ret += """  if test "${with_%s_ldflags}" = "" -o "${enable_extlibs}" = "no"; then
""" % (name)

 ret += """   %s_ldflags="-L\$(abinit_builddir)/lib/%s -l%s"
   build_%s="yes"

   if test "${enable_tricks}" = "yes"; then
    ABI_TRICKS_%s(%s)
    if test "${%s_tricks_bypass}" = "yes"; then
     build_%s="no"
    fi
   fi
  else
""" % (name,name,name,name,name.upper(),tricks,name,name)

 if ( include != None ):
  ret += """   %s_include="${with_%s_include}"
""" % (name,name)

 ret += """   %s_ldflags="${with_%s_ldflags}"
   build_%s="no"
  fi
 else
  build_%s="no"
 fi

 dnl Output result
 AC_MSG_CHECKING([whether to build the %s library])
 AC_MSG_RESULT([${build_%s}])

 dnl Substitute variables needed for the use of the library
 AC_SUBST(%s_include)
 AC_SUBST(%s_ldflags)
 AC_SUBST(build_%s)

 dnl Inform Automake
 AM_CONDITIONAL(DO_BUILD_%s,test "${build_%s}" = "yes")
 AM_CONDITIONAL(DO_ENABLE_%s,test "${enable_%s}" = "yes")
]) # ABI_EXTLIB_%s
""" % (name,name,name,name,name.upper(),name,name,name,name,
       name.upper(),name,name.upper(),name,name.upper())

 return ret



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

#
# Main program
#

# Initial setup
my_name    = "make-macros-extlibs"
my_configs = ["config/specs/extlibs.cf"]
my_output  = "config/m4/do-not-edit-extlibs.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 external libraries
for (lib,inc,tip) in abinit_extlibs:
 m4.write(macro_extlib(lib,inc,tip))

# Finish
m4.close()

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