#!/usr/bin/env python
#
# Copyright (C) 2005-2014 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_header(name,stamp):

  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.
#

AM_CFLAGS  = @ABI_CPPFLAGS@
AM_FCFLAGS = @FPPFLAGS@ @FCFLAGS_FREEFORM@ @FCFLAGS_MODDIR@ @fc_mod_incs@ @fcflags_opt_98_main@
AM_LDFLAGS = @FC_LDFLAGS@

""" % (name,stamp,name)



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

#
# Main program
#

# Initial setup
my_name    = "make-makefiles-binaries"
my_configs = {
  "libs":"config/specs/corelibs.conf",
  "bins":"config/specs/binaries.conf"}
my_output  = "src/98_main/Makefile.am"

# 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.values():
  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 corelibs
lib_cnf = MyConfigParser()
lib_cnf.read(my_configs["libs"])
abinit_corelibs = lib_cnf.sections()
abinit_corelibs.sort()

# Init binaries
bin_cnf = MyConfigParser()
bin_cnf.read(my_configs["bins"])
main_bins = bin_cnf.sections()
main_bins.sort()

# Generate library and include lists
lib_list = ""
inc_list = ""

# Main binary includes
cnf_file = "src/98_main/abinit.dir"
if ( os.path.exists(cnf_file) ):
  execfile(cnf_file)
else:
  include_dirs = []

# FIXME: hard-coded include list
include_dirs = ["incs"]

for tmp in include_dirs:
  inc_list += " \\\n\t@src_%s_incs@" % (tmp)

# FIXME: hard-coded include for fallbacks
inc_list += " \\\n\t@fallbacks_incs@"

# Core libraries
for lib in abinit_corelibs:
  # Init
  lib_opt = lib_cnf.get(lib,"optional")
  lib_rul = lib_cnf.get(lib,"abirules")
  try:
    lib_deps = lib_cnf.get(lib,"dependencies").split()
  except NoOptionError:
    lib_deps = None

  # Add sequential and parallel libraries
  if ( lib_opt == "yes" ):
    lib_list += "if DO_BUILD_%s\n" % (lib.upper())
    lib_list += " LIB_%s = $(top_builddir)/src/%s/lib%s.a\n" % \
      (lib.upper(),lib,lib)
    lib_list += "else\n LIB_%s =\nendif\n" % (lib.upper())
  else:
    lib_list += "LIB_%s = $(top_builddir)/src/%s/lib%s.a\n" % \
      (lib.upper(),lib,lib)

lib_list += "\n"

# Generate binary list
bin_list = "bin_PROGRAMS ="
opt_list = ""
cln_list = "CLEANFILES ="

for bin in main_bins:

  # Init
  bin_opt = bin_cnf.get(bin,"optional")

  # Regular binaries
  if ( not os.path.exists("src/98_main/%s.F90" % (bin)) ):
    sys.stderr.write("Error: No such file or directory: 'src/98_main/%s.F90'\n" % \
      (bin))
    sys.exit(4)
  cln_list += " \\\n\t%s_cpp.f90" % (bin)

  # Sequential/parallel binaries
  if ( bin_opt == "yes" ):
    opt_list += "if DO_BUILD_%s\n bin_PROGRAMS += %s\nendif\n" % \
      (bin.upper(),bin)
  else:
    bin_list += " \\\n\t%s" % (bin)

cln_list += "\n"
bin_list += "\n\n"
opt_list += "\n"

# Start generating makefile content
makefile = makefile_header(my_name,now)

if ( inc_list != "" ):
  makefile += "AM_CPPFLAGS =%s\n\n" % (inc_list)
makefile += "# Internal libraries\n" + lib_list
makefile += "# Binary list\n" + bin_list + opt_list

# Process each binary
for bin in main_bins:

  # Init
  try:
    bin_deps = bin_cnf.get(bin,"dependencies").split()
  except NoOptionError:
    bin_deps = list()
  bin_opt = bin_cnf.get(bin,"optional")
  bin_cppflags = ""
  bin_fcflags = ""
  bin_ldflags = ""
  bin_libs = ""

  # Check all libraries the binary depends on
  for lib in bin_cnf.get(bin,"libraries").split():
    # Add library pointer
    bin_libs += " $(LIB_%s)" % (lib.upper())

  # Check whether the binary may use external libraries
  for dep in bin_deps:
    bin_cppflags += " @lib_%s_incs@" % (dep)
    bin_fcflags += " @lib_%s_fcflags@" % (dep)
    bin_ldflags += " @lib_%s_ldflags@" % (dep)
    bin_libs += " @lib_%s_libs@" % (dep)

  # Finish setting build-time parameters
  if ( bin_cppflags != "" ):
    bin_cppflags = "$(AM_CPPFLAGS) $(CPPFLAGS) %s" % (bin_cppflags)
  if ( bin_fcflags != "" ):
    bin_fcflags = "$(AM_FCFLAGS) $(FCFLAGS) %s" % (bin_fcflags)
  if ( bin_ldflags != "" ):
    bin_ldflags = "$(FC_LDFLAGS) %s" % (bin_ldflags)
  bin_libs += " $(FC_LIBS)"

  # Write results
  indent = ""
  makefile += "# %s.F90 ---> %s\n" % (bin,bin)

  # Optional build
  if ( bin_opt == "yes" ):
    makefile += "if DO_BUILD_%s\n" % (bin.upper())
    indent = " "

  # Binary sources
  makefile += "%s%s_SOURCES = %s.F90\n" % (indent,bin,bin)

  # Additional preprocessing flags
  if ( bin_cppflags != "" ):
    bin_cppflags = re.sub(r"[ ]+"," ",bin_cppflags.strip())
    bin_cppflags = re.sub(" "," \\\n\t",bin_cppflags)
    makefile += "%s%s_CPPFLAGS = \\\n\t%s\n" % (indent,bin,bin_cppflags)

  # Additional compile flags
  if ( bin_fcflags != "" ):
    bin_fcflags = re.sub(r"[ ]+"," ",bin_fcflags.strip())
    bin_fcflags = re.sub(" "," \\\n\t",bin_fcflags)
    makefile += "%s%s_FCFLAGS = \\\n\t%s\n" % (indent,bin,bin_fcflags)

  # LDFLAGS and additional libraries
  if ( (bin_ldflags != "") or (bin_libs != "") ):
    bin_ldflags += bin_libs
    bin_ldflags = re.sub(r"[ ]+"," ",bin_ldflags.strip())
    bin_ldflags = re.sub(" "," \\\n\t",bin_ldflags)
    makefile += "%s%s_LDADD = \\\n\t%s\n" % (indent,bin,bin_ldflags)

  # Close conditionals
  if ( bin_opt == "yes" ):
    makefile += "endif\n\n"
  else:
    makefile += "\n"

# Write list of files to clean
makefile += cln_list

# Init list of extra files
ext       = "EXTRA_DIST ="
ext_print = 0

# Include additional hand-made information
add = "src/98_main/abinit.amf"
if ( os.path.exists(add) ):
  ext += " \\\n\tabinit.amf"
  ext_print = 1

# Include RoboDOC header
hdr = "src/98_main/_main_"
if ( os.path.exists(hdr) ):
  ext += " \\\n\t_main_"
  ext_print = 1

# Finish list of extra files
ext += "\n"

# Write list of extra files
if ( ext_print == 1 ):
  makefile += "\n"+ext+"\n"

# Write additional hand-made information
if ( os.path.exists(add) ):
  makefile += "\n"+file(add,"r").read()

mf = file(my_output,"w")
mf.write(makefile)
mf.close()
