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

#
# NOTES:
#
#   * This script changes the current directory to doc/.
#   * The structure of the script is ready for individual builds.
#

from ConfigParser import ConfigParser
from time import gmtime,strftime

import commands
import os
import re
import sys

class MyConfigParser(ConfigParser):

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

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

#
# Subroutines
#

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
#

# Script name
my_name    = "make-makefiles-doc"
my_configs = ["config/specs/documents.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 the ABINIT source tree." % my_name
  print "%s: Aborting now." % my_name
  sys.exit(1)

# Read config file(s)
cnf = MyConfigParser()
for cnf_file in my_configs:
  if ( not os.path.exists(cnf_file) ):
    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())

# Init
cnf.read(my_configs[0])
abinit_docs = cnf.sections()

# First go to the doc subdirectory
os.chdir("doc")

# Prepare regexps
olddir = re.compile(",.*")
tmpdir = re.compile("tmp-*")
vimswp = re.compile("\..*\.swp")

# Get list of subdirectories
doc_subdirs = list()
for tmp in os.listdir("."):
  if ( os.path.isdir("%s" % (tmp)) ):
    doc_subdirs.append(tmp)
doc_subdirs.sort()

# Init file list
doc = "nobase_dist_doc_DATA ="

# Write one Makefile per subdirectory
for sub in doc_subdirs:

  for root,dirs,files in os.walk(sub):
    # Clean-up lists
    if ( "Makefile.am" in files ):
      files.remove("Makefile.am")
    if ( "Makefile.in" in files ):
      files.remove("Makefile.in")
    if ( "Makefile" in files ):
      files.remove("Makefile")

    # We don't want to distribute nor install temporary directories
    for tmp in dirs:
      if olddir.match(tmp):
        dirs.remove(tmp)
      if tmpdir.match(tmp):
        dirs.remove(tmp)

    # Add data files
    files.sort()
    for dat in files:
      if ( not vimswp.match(dat) ):
        doc += " \\\n\t%s" % (os.path.join(root,dat))

  # Write makefile
  #mf = file("%s/Makefile.am" % (root),"w")
  #mf.write(makefile_header(my_name,now))
  #mf.close()

# Go back to top directory
os.chdir("..")

# Create top doc makefile
mf = file("doc/Makefile.am","w")
mf.write(makefile_header(my_name,now))
mf.write(doc)

#mf.write("SUBDIRS = \\\n\t%s\n" % (" \\\n\t".join(doc_subdirs)))

# Write additional data
add = "config/makefiles/doc.am"
if( os.path.exists(add) ):
  mf.write("\n"+file(add,"r").read())

# End of file
mf.close()
