#!/usr/bin/env python
#
# Copyright (C) 2011-2012 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
from time import gmtime,strftime

import os
import re
import sys

class MyConfigParser(ConfigParser):

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



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

#
# Main program
#

# Initial setup
my_name     = "make-data-fallbacks"
my_config   = "config/specs/buildsys.conf"
my_macro    = "config/m4/auto-fallbacks.m4"
my_makefile = "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)

# Check if we have a config file
if ( os.path.exists(my_config) ):
  cnf = MyConfigParser()
  cnf.read(my_config)
else:
  print "%s: Could not find config file (%s)." % (my_name,my_config)
  print "%s: Aborting now." % my_name
  sys.exit(2)

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

# Init macro
fallbacks_macro = """\
# Generated by %s on %s

#
# ABINIT fallbacks support for the "configure" script
#

#
# IMPORTANT NOTE
#
# This file has been automatically generated by the %s
# script. If you try to edit it, your changes will systematically be
# overwritten.
#



# ABI_FALLBACKS_INIT()
# --------------------
#
# Defines whether the Abinit FALLBACKS may be built along with the package.
#
AC_DEFUN([ABI_FALLBACKS_INIT],[
  dnl Init
  abi_fallbacks_mode="@FALLBACKS_MODE@"
@FALLBACKS_INIT@

  case "${abi_fallbacks_mode}" in
    data)
      AC_MSG_NOTICE([the Abinit FALLBACKS will never be built])
      ;;
    subsystem)
      AC_MSG_NOTICE([the Abinit FALLBACKS may be built when necessary])
      ;;
  esac
]) # ABI_FALLBACKS_INIT
""" % (my_name,now,my_name)

# Extract information from config file
fallbacks_mode = cnf.get("fallbacks","mode")

# Generate Autoconf macro
if ( fallbacks_mode == "data" ):
  fallbacks_init = "  AC_CONFIG_FILES([fallbacks/Makefile])"
elif ( fallbacks_mode == "subsystem" ):
  fallbacks_init = "  AC_CONFIG_SUBDIRS([fallbacks])"

fallbacks_macro = re.sub("@FALLBACKS_MODE@",fallbacks_mode,fallbacks_macro)
fallbacks_macro = re.sub("@FALLBACKS_INIT@",fallbacks_init,fallbacks_macro)
file(my_macro,"w").write(fallbacks_macro)

# Generate makefile snippet
#file(my_makefile,"a").write("\nSUBDIRS += fallbacks\n")
if ( fallbacks_mode == "data" ):
  fallbacks_files= list()
  os.system("cd fallbacks && ./wipeout.sh >/dev/null 2>&1")
  os.system("cd fallbacks && ./autogen.sh >/dev/null 2>&1")
  for root,dirs,files in os.walk("fallbacks"):
    if ( "autom4te.cache" in dirs ):
      dirs.remove("autom4te.cache")
    fallbacks_files += [os.path.join(root,item) for item in files]
  fallbacks_files.sort()
  file(my_makefile,"a").write("\nEXTRA_DIST += \\\n\t%s\n" % \
    " \\\n\t".join(fallbacks_files))
elif ( fallbacks_mode == "subsystem" ):
  os.system("cd fallbacks && ./autogen.sh >/dev/null 2>&1")
