#!/usr/bin/env python
#
# Copyright (C) 2005-2010 ABINIT Group (Yann Pouillon)
#
# 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 ConfigParser import ConfigParser,NoOptionError
from time import gmtime,strftime

import commands
import os
import re
import sys

class MyConfigParser(ConfigParser):

  def optionxform(self,option):
    return str(option)

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

#
# Subroutines
#

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

  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
    
package-ready:
	$(MAKE) -f $(top_srcdir)/prereqs/%s/%s.mk @SET_MAKE@
	-if test -d $(PWD)/tmp/bin ; then mv $(PWD)/tmp/bin/* .; fi
	-if test -d $(PWD)/tmp/lib ; then mv $(PWD)/tmp/lib/* .; fi
	-if test -d $(PWD)/tmp/include ; then mv $(PWD)/tmp/include/* .; fi;
	touch package-ready

clean-local:
	$(MAKE) -f $(top_srcdir)/prereqs/%s/%s.mk clean @SET_MAKE@
	rm -rf @%s_pkg_name@ tmp

EXTRA_DIST = @%s_pkg_name@.tar.gz %s.mk

CLEANFILES = uncompress-stamp configure-stamp build-stamp install-stamp package-ready
""" % (name,stamp,name,lib,lib,lib,lib,lib,lib,lib)



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

#
# Main program
#

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

# 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/98_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_file in my_configs:
  if ( os.path.exists(cnf_file) ):
    if ( re.search("\.cf$",cnf_file) ):
      execfile(cnf_file)
  else:
    print "%s: Could not find config file (%s)." % (my_name,cnf_file)
    print "%s: Aborting now." % my_name
    sys.exit(2)

# What time is it?
now = strftime("%Y/%m/%d %H:%M:%S +0000",gmtime())

# Init prereqs
cnf = MyConfigParser()
cnf.read(my_configs[0])
abinit_prereqs = cnf.sections()
abinit_prereqs.sort()

# Process each library
for lib in abinit_prereqs:

  # Init
  lib_dir = "prereqs/%s" % (lib)

  # Write library's Makefile.am
  mf = file(lib_dir+"/Makefile.am","w")
  mf.write(makefile_template(my_name,now,lib))

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

  # Add RoboDOC header
  hdr = "%s/_%s_" % (lib_dir,lib)
  if ( os.path.exists(hdr) ):
    mf.write("\nEXTRA_DIST += _%s_\n" % (lib))

  mf.close()
