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

# Makefile header
def makefile_template(name,stamp,b_name):

 return """#
# Makefile for ABINIT                                      -*- Automake -*-
# Generated by %s on %s

#
# IMPORTANT NOTE
#
# Any manual change to this file will systematically be overwritten.
# Please modify the %s script or its config file instead.
#

# Delegate build to bindings' own Makefile
all_targets all: bindings-ready

if DO_BUILD_BINDINGS
bindings-ready:
	$(MAKE) -f $(top_srcdir)/bindings/%s/%s.mk
	@touch bindings-ready
else
bindings-ready:
	@echo "Support for bindings has been disabled"
	@touch bindings-ready
endif

clean-local:
	$(MAKE) -f $(top_srcdir)/bindings/%s/%s.mk clean

EXTRA_DIST = @SOURCES@

CLEANFILES = @CLEANS@ \\
  bindings-ready
""" % (name,stamp,name,b_name,b_name,b_name,b_name)



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

#
# Main program
#

# Initial setup
my_name    = "make-makefiles-bindings"
my_configs = ["config/specs/bindings.cf"]

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

# Process each bindings blob
for bnd in abinit_bindings:

 # Init
 bnd_dir = "bindings/%s" % (bnd)
 sources = ""
 cleans = ""
 sep = " \\\n  "

 # Extract bindings information
 try:
  bnd_bins = eval("%s_bins" % (bnd))
 except NameError:
  bnd_bins = None
 try:
  bnd_hdrs = eval("%s_hdrs" % (bnd))
 except NameError:
  bnd_hdrs = None
 try:
  bnd_libs = eval("%s_libs" % (bnd))
 except NameError:
  bnd_libs = None
 try:
  bnd_mods = eval("%s_mods" % (bnd))
 except NameError:
  bnd_mods = None
 try:
  bnd_srcs = eval("%s_srcs" % (bnd))
 except NameError:
  bnd_srcs = None

 # Update list of files to clean
 if ( bnd_bins != None ):
  cleans += sep+sep.join(bnd_bins)
 if ( bnd_hdrs != None ):
  cleans += sep+sep.join(bnd_hdrs)
 if ( bnd_libs != None ):
  cleans += sep+sep.join(bnd_libs)
 if ( bnd_mods != None ):
  cleans += sep+sep.join(bnd_mods)
 if ( bnd_srcs != None ):
  sources += sep+sep.join(bnd_srcs)

 # Import and update template 
 makefile = makefile_template(my_name,now,bnd)
 makefile = re.sub("@SOURCES@",sources,makefile)
 makefile = re.sub("@CLEANS@",cleans,makefile)

 # Add additional hand-written information
 add = bnd_dir+"/abinit.amf"
 if ( os.path.exists(add) ):
  makefile += "\nEXTRA_DIST += abinit.amf\n"
  makefile += "\n"+file(add,"r").read()

 # Add RoboDOC header
 hdr = "%s/_%s_" % (bnd_dir,bnd)
 if ( os.path.exists(hdr) ):
  makefile += "\nEXTRA_DIST += _%s_\n" % (bnd)

 mf = file(bnd_dir+"/Makefile.am","w")
 mf.write(makefile)
 mf.close()
