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

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

all_targets all: BIN_IS_MADE

BIN_IS_MADE:
	$(SHELL) $(top_srcdir)/config/scripts/make-scripts $(top_srcdir)/util . @SHELL@ @PERL@ @PYTHON@
	@touch $@

""" % (name,stamp,name)



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

#
# Main program
#

# Script name
my_name   = "make-makefiles-utils"
my_output = "bin/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/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)

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

# Setup
file_types = ["sh","pl","py"]
util_subdirs = commands.getoutput("find util -depth -type d").split("\n")
cln = "CLEANFILES ="

# Generate file list
for ext in file_types:
 cln += "\n"
 cln += commands.getoutput("find util -name '*.%s'" % (ext))
 cln  = re.sub("\.%s" % (ext),"",cln)

for sub in util_subdirs:
 cln = re.sub(sub+"/","",cln)

cln  = re.sub("\n"," \\\n\t",cln)
cln += "\n\n"

# Write Makefile.am
mf = file(my_output,"w")
mf.write(makefile_header(my_name,now))
mf.write(cln)
mf.close()
