#!/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,pkg,pkg_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 package's own Makefile
all_targets all: package-ready

if DO_BUILD_%s  
package-ready:
	$(MAKE) -f $(top_srcdir)/plugins/%s/%s.mk
	-if test -d tmp/bin; then mv tmp/bin/* .; fi
	-if test -d tmp/lib; then mv tmp/lib/* .; fi
	-if test -d tmp/include; then \
	  find tmp/include -name '*.mod' -exec mv {} . \; ; \
	 fi
	-if test -d $(PWD)/tmp/include ; then mv $(PWD)/tmp/include/* .; fi
	-if test -d $(PWD)/tmp/finclude ; then mv $(PWD)/tmp/finclude/* .; fi
	@touch package-ready
else
package-ready:
	@echo "Support for %s has been disabled"
	@touch package-ready
endif

clean-local:
	$(MAKE) -f $(top_srcdir)/plugins/%s/%s.mk clean
	rm -rf %s tmp

EXTRA_DIST = %s.mk

CLEANFILES = \\
  uncompress-stamp \\
  configure-stamp \\
  build-stamp \\
  install-stamp \\
  package-ready@CLEANS@
""" % (name,stamp,name,pkg.upper(),pkg,pkg,pkg.upper(),pkg,pkg,pkg_name,pkg)



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

#
# Main program
#

# Initial setup
my_name    = "make-makefiles-plugins"
my_configs = ["config/specs/plugins.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 library
for pkg in abinit_plugins:

 # Init
 pkg_dir = "plugins/%s" % (pkg)
 cleans = ""
 sep = " \\\n  "

 # Extract mandatory package information
 pkg_name = eval("%s_name" % (pkg))
 pkg_nick = eval("%s_nick" % (pkg))
 pkg_desc = eval("%s_desc" % (pkg))
 pkg_urls = eval("%s_urls" % (pkg))

 # Extract optional package information
 try:
  pkg_bins = eval("%s_bins" % (pkg))
 except NameError:
  pkg_bins = None
 try:
  pkg_hdrs = eval("%s_hdrs" % (pkg))
 except NameError:
  pkg_hdrs = None
 try:
  pkg_libs = eval("%s_libs" % (pkg))
 except NameError:
  pkg_libs = None
 try:
  pkg_mods = eval("%s_mods" % (pkg))
 except NameError:
  pkg_mods = None

 # Update list of files to clean
 if ( pkg_bins != None ):
  cleans += sep+sep.join(pkg_bins)
 if ( pkg_hdrs != None ):
  cleans += sep+sep.join(pkg_hdrs)
 if ( pkg_libs != None ):
  cleans += sep+sep.join(pkg_libs)
 if ( pkg_mods != None ):
  cleans += sep+sep.join(pkg_mods)

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

 # Add additional hand-written information
 add = pkg_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_" % (pkg_dir,pkg)
 if ( os.path.exists(hdr) ):
  makefile += "\nEXTRA_DIST += _%s_\n" % (pkg)

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