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

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

""" % (name,stamp,name)



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

#
# Main program
#

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

sep = "# ---------------------------------------------------------------------------- #\n\n"

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

# Generate library lists
lib_list = ""
opt_list = ""
src_list = ""

tgt_abirules = "# Enforce ABINIT Coding Style (the so-called ABIRULES)\n" + \
 "abirules:\n\t@echo 'Reporting possible errors in the abirules procedure'" + \
 " > ,,abirules.log\n"

for lib in abinit_libs:
 if ( lib in abilibs_specs ):
  lib_specs = abilibs_specs[lib]
 else:
  lib_specs = ABI_LIB_NIL

 if ( (lib_specs & ABI_LIB_EXT) == 0 ):
  src_list += " \\\n\t"+lib
  if ( (lib_specs & ABI_LIB_RUL) == 0 ):
   tgt_abirules += "\t@$(PERL) ../util/maintainers/abirules.pl -d %s >> ,,abirules.log\n" % (lib)
 else:
  if ( (lib_specs & ABI_LIB_OPT) == 0 ):
   lib_list += " \\\n\t"+lib
  else:
   opt_list += "if DO_BUILD_%s\n SUBDIRS += %s\nendif\n" % (lib.upper(),lib)

lib_list += "\n\n"
opt_list += "\n"
src_list += " \\\n\tmain\n\n"
tgt_abirules += "\t@$(PERL) ../util/maintainers/abirules.pl -d main >> ,,abirules.log\n"
tgt_abirules += "\tcd .. ; ./util/maintainers/abilint.py . . > ,abilint.out1 ; cd src\n"
tgt_abirules += "\t@grep 'Error' ,,abirules.log \n\n"

# Write intermediate Makefile.am for external libraries
mf = file("lib/Makefile.am","w")
mf.write(makefile_header(my_name,now))
mf.write("SUBDIRS =%s" % (lib_list))
mf.write(opt_list)
add = "config/makefiles/lib.am"
if ( os.path.exists(add) ):
 mf.write(file(add,"r").read())
mf.close()

# Write intermediate Makefile.am for core source
mf = file("src/Makefile.am","w")
mf.write(makefile_header(my_name,now))
mf.write("SUBDIRS =%s" % (src_list))
mf.write(sep+tgt_abirules)
add = "config/makefiles/src.am"
if ( os.path.exists(add) ):
 mf.write(sep+file(add,"r").read())
mf.close()
