#!/usr/bin/env python
#
# Copyright (C) 2011-2017 ABINIT Group (Yann Pouillon)
#
# This file is part of the ABINIT software document. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#
from __future__ import print_function, division, absolute_import #, unicode_literals

try:
    from ConfigParser import ConfigParser,NoOptionError
except ImportError:
    from configparser import ConfigParser,NoOptionError
from time import gmtime,strftime

import os
import re
import sys

class MyConfigParser(ConfigParser):

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

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

#
# Templates
#

doc_makefile = """\
#                                                          -*- Automake -*-
# Makefile for the Abinit Documentation
# 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.
#

%s%s

all-local: doc-build-stamp

if DO_BUILD_TEX
doc-build-stamp:%s
	touch doc-build-stamp%s
else
doc-build-stamp:
	cd .. && make built-docs-stamp @SET_MAKE@
endif

abidocdir = $(docdir)/%s
nobase%s_abidoc_DATA =%s%s%s
"""

man_makefile = """\
#                                                          -*- Automake -*-
# Makefile for the Abinit Documentation
# 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.
#

dist_man1_MANS = %s

all-local: doc-build-stamp

doc-build-stamp:
	touch doc-build-stamp

CLEANFILES = doc-build-stamp
"""

top_makefile = """\
#                                                          -*- Automake -*-
# Makefile for the Abinit Documentation
# 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.
#

SUBDIRS =%s

EXTRA_DIST = built-docs.tgz config/gnu config/m4 config/util rules.am

%s
"""

doc_rules = open("rules.am", "rt").read()

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

#
# Subroutines
#

# Makefile contents
def makefile_src(name,stamp,dir_name,files,docs=None,flags=None):

  doc_flags = ""
  doc_files = ""
  doc_deps = ""
  doc_tgts = ""
  doc_dist = ""
  doc_clean = ""
  doc_clean1 = ""
  doc_clean2 = ""
  doc_clean3 = ""
  dist_tag = ""

  if ( docs is None ):

    doc_files = " \\\n  " + " \\\n  ".join(files)
    dist_tag = "_dist"

  else:

    if ( not flags is None ):
      doc_flags = "TEXFLAGS_EXTRA = %s\n\n" % (flags)

    tmp_docs = list(docs.keys())
    tmp_docs.sort()

    for doc in tmp_docs:
      doc_name,doc_type = doc.split(".")
      if ( doc_type == "gv" ):
        doc_ext = "pdf"
      elif ( doc_type == "md" ):
        doc_ext = "html"
      elif ( doc_type == "tex" ):
        doc_ext = "pdf"
        doc_clean1 = "\n\trm -f *.aux *.blg *.ilg *.log *.nav *.snm *.tmp *.toc *.vrb"
        doc_clean1 += "\n\trm -f \\\n\t  *Notes.bib \\\n\t  %s.fdb_latexmk \\\n\t  %s.fls \\\n\t  %s.out" % \
          (doc_name,doc_name,doc_name)
        doc_clean2 += "\n\trm -f %s.dvi" %  (doc_name)
      else:
        print("Error: unknown document type '%s'" % (doc_type))
        sys.exit(1)
      doc_files += " \\\n  %s.%s" % (doc_name,doc_ext)
      doc_tgts += " \\\n  %s.%s" % (doc_name,doc_ext)
      doc_deps += "\n\n%s.%s: \\\n  %s" % (doc_name,doc_ext,doc)
      doc_dist += " \\\n  %s" % (doc)
      doc_clean2 += "\n\trm -f %s.%s" % (doc_name,doc_ext)
      if ( len(docs[doc]) > 0 ):
        doc_deps += " \\\n  " + " \\\n  ".join(docs[doc])
        for src in docs[doc]:
          if ( not re.search("\\.%s$" % (doc_ext),src) ):
            doc_dist += " \\\n  %s" % (src)

    if ( len(files) > 0 ):
      doc_dist += " \\\n  " + " \\\n  ".join(files)

  if ( doc_clean1 != "" ):
    doc_clean += "\nmostlyclean-local: %s\n" % (doc_clean1)
  doc_clean += "\nclean-local:\n\trm -f doc-build-stamp"
  if ( doc_clean2 != "" ):
    doc_clean += "%s\n" % (doc_clean2)
  if ( len(doc_dist) > 0 ):
    doc_dist = "\nEXTRA_DIST =%s\n" % (doc_dist)
  doc_files += "\n"

  return doc_makefile % \
    (name,stamp,name,doc_flags,doc_rules,doc_tgts,doc_deps,dir_name,dist_tag,
    doc_files,doc_clean,doc_dist)

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

#
# Main program
#

# Initial setup
my_name    = "make-makefiles-doc"
my_configs = ["config/specs/documents.conf"]
my_doctypes = {
  "gv":"pdf",
  "md":"html",
  "tex":"pdf"}

# Check if we are in the top of the ABINIT source tree
if ( not os.path.exists("configure.ac") or
     not os.path.exists("config/specs/documents.conf") ):
  print("%s: You must be in the top of an Abinit Documentation 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) ):
      exec(compile(open(cnf_file).read(), cnf_file, 'exec'))
  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())

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

# Init document list
cnf = MyConfigParser()
cnf.read(my_configs[0])
doc_list = cnf.sections()
doc_list.sort()
all_docs = {}

# Process each document
for doc_dir in doc_list:

  # Work directly from there
  os.chdir(doc_dir)

  # Extract document information
  dir_type = cnf.get(doc_dir,"type")

  # Filter documents according to their types
  all_docs[doc_dir] = []
  if ( dir_type == "data" ):
    if ( cnf.has_option(doc_dir,"ignore") ):
      ign_list = cnf.get(doc_dir,"ignore").split()
    else:
      ign_list = []
    ign_list += ["Makefile.am","Makefile.in","Makefile"]
    dat_files = []

    for root,dirs,files in os.walk("."):
      # We don't want to distribute nor install temporary directories
      dirs = [item for item in dirs if (not olddir.match(item)) and (not tmpdir.match(item))]

      # Get rid of ignored files
      files = [item for item in files if (not item in ign_list) and (not vimswp.match(item))]
      files.sort()

      # Just add files as raw data
      dat_files += [os.path.join(root,item) for item in files]

    makefile_data = makefile_src(my_name,now,doc_dir,dat_files)

  elif ( dir_type == "source" ):

    if ( cnf.has_option(doc_dir,"texflags") ):
      doc_flags = cnf.get(doc_dir,"texflags")
    else:
      doc_flags = None

    dat_files = []
    if ( cnf.has_option(doc_dir,"extra") ):
      dat_files = cnf.get(doc_dir,"extra").split()
    doc_files = {}
    for doc in cnf.get(doc_dir,"docs").split():
      doc_specs = cnf.get(doc_dir,doc).split()
      doc_files["%s.%s" % (doc,doc_specs[0])] = doc_specs[1:]
      all_docs[doc_dir].append("%s.%s" % (doc,my_doctypes[doc_specs[0]]))

    makefile_data = makefile_src(my_name,now,doc_dir,dat_files,doc_files,doc_flags)

  elif ( dir_type == "manual" ):

    makefile_data = man_makefile % (my_name,now,my_name,cnf.get(doc_dir,"mans"))

  else:
    raise ValueError("processing of document type '%s' not implemented" % \
      (dir_type))

  # Write makefile
  open("Makefile.am", "wt").write(makefile_data)

  # Got back one level
  os.chdir("..")

# Write top makefile
tar_deps = ""
tar_tgts = ""
tar_files = ""
for doc_dir in doc_list:
  tar_deps += " \\\n  %s/doc-build-stamp" % (doc_dir)
  tar_tgts += "\n%s/doc-build-stamp:\n\tcd %s && $(MAKE) doc-build-stamp @SET_MAKE@\n" % \
    (doc_dir,doc_dir)
  for doc_file in all_docs[doc_dir]:
    tar_files += " \\\n\t  %s" % (os.path.join(doc_dir,doc_file))
doc_tarball = "if DO_BUILD_TEX\n\nbuilt-docs-tarball:%s\n\ttar czf built-docs.tgz%s\n%s\nelse\n\nall-local: built-docs-stamp\n\ninstall-data-local: built-docs-stamp\n\nbuilt-docs-stamp: built-docs.tgz\n\ttar xmzf $(top_srcdir)/built-docs.tgz\n\ttouch built-docs-stamp\n\nbuilt-docs-tarball:\n\t@echo \"Please install Graphviz and LaTeX to build the documentation\"\n\nCLEANFILES = built-docs-stamp\n\nendif" % \
  (tar_deps,tar_files,tar_tgts)
open("Makefile.am", "wt").write(top_makefile % \
  (my_name,now,my_name," \\\n  " + " \\\n  ".join(doc_list),doc_tarball))
