# (re)create batch scripts in the current directory with correct paths for windows

import os

if os.name == "posix":
    mdpath = "MKDEPPATH=" + os.getcwd() 

    f=open('rules','w')
    f.write(("""
# These predefined variables may be set in the Makefile
#
# EXECUTABLE        name of final program
# LINKER            set this if the linker is different from the compiler
# LINKFLAGS         extra flags to pass to the linker
# FREEFLAGS         extra flags for compiling free form files
# FIXEDFLAGS        extra flags for compiling fixed form files
# PREFIXEDFLAGS     extra flags for compiling fixed form files with preprocessing directives
# PREFREEFLAGS      extra flags for compiling free form files with preprocessing directives
# ORGSRCDIR         build project from all files in $(ORGSRCDIR)/.files and SOURCEFILES, where files
#                   in the latter override files in the former
# CPP_FLAGS         if not "", pass flags such as -I... -D... etc and invoke cpp from mkdep before parsing the fortran. a fair bit slower.

## get the list of object files

include .mkdep_objects

## if EXECUTABLE or FORTRANCOMPILER is not set, default

ifeq ($(strip $(EXECUTABLE)),)
   EXECUTABLE=a.out
endif
ifeq ($(strip $(FORTRANCOMPILER)),)
   FORTRANCOMPILER=gfortran
endif
ifeq ($(strip $(LINKER)),)
   LINKER=$(FORTRANCOMPILER)
endif
ifeq ($(strip $(ENCODING)),)
   ENCODING=utf-8
endif
ifeq ($(strip $(ORGSRCDIR)),)
   ORGSRCDIR=""
endif
ifeq ($(strip $(CPP_FLAGS)),)
   CPP_FLAGS=""
endif



## the link happen here
$(EXECUTABLE):$(OBJS)
\t$(LINKER) -o $(EXECUTABLE) $(OBJS) $(LINKFLAGS)

## get the autogenerated dependencies

include .mkdep_dependencies


""" + mdpath + """

## delete object files (keep mkdep stuff)
clean:
\trm -f build/*.mod *.mod *~ $(EXECUTABLE) $(MAINLIB)
\t$(MKDEPPATH)/rmobjs.py .mkdep_objects
\t$(MKDEPPATH)/mkdep.py reset
\tmake dep

## (re)generate dependencies
dep:
\techo "" > .files
\tfor dir in $(SOURCEFILES); do \\
\t   echo "$$dir" >> .files ; \\
\tdone
\tfor dir in $(SOURCEFILES2); do \\
\t   echo "$$dir OPT2" >> .files ; \\
\tdone
\tif [ "$(strip $(ORGSRCDIR))" != "" ] ; then \\
\t   echo $(ORGSRCDIR) > .incdirs ;\\
\t   $(MKDEPPATH)/mergedotfiles.py $(ORGSRCDIR) ;\\
\telse \\
\t   echo "" > .incdirs ;\\
\t   $(MKDEPPATH)/mergedotfiles.py NOORGSRCDIR ;\\
\tfi
\tif [ $(strip $(CPP_FLAGS)) != "" ] ; then \\
\t   $(MKDEPPATH)/mkdep.py -e $(ENCODING) -i .incdirs --cpp=cpp --cppflags=$(CPP_FLAGS) .files ;\\
\telse \\
\t   $(MKDEPPATH)/mkdep.py -e $(ENCODING) -i .incdirs .files ;\\
\tfi

## explicit rules for each and every file
include .mkdep_rules
\n"""))

    f.close()
   


if os.name == "nt":
    pwd = os.getcwd().replace("\\","/")
    
    f=open('rules','w')
    f.write(("""
# These predefined variables may be set in the Makefile
#
# EXECUTABLE        name of final program
# LINKER            set this if the linker is different from the compiler
# LINKFLAGS         extra flags to pass to the linker
# FREEFLAGS         extra flags for compiling free form files
# FIXEDFLAGS        extra flags for compiling fixed form files
# PREFIXEDFLAGS     extra flags for compiling fixed form files with preprocessing directives
# PREFREEFLAGS      extra flags for compiling free form files with preprocessing directives
# ORGSRCDIR         build project from all files in $(ORGSRCDIR)/.files and SOURCEFILES, where files
#                   in the latter override files in the former
# CPP_FLAGS         if not "", pass flags such as -I... -D... etc and invoke cpp from mkdep before parsing the fortran. a fair bit slower.
#

## get the list of object files
include .mkdep_objects

## if EXECUTABLE or FORTRANCOMPILER is not set, default
ifeq ($(strip $(EXECUTABLE)),)
   EXECUTABLE=a.exe
endif
ifeq ($(strip $(FORTRANCOMPILER)),)
   FORTRANCOMPILER=gfortran
endif
ifeq ($(strip $(LINKER)),)
   LINKER=$(FORTRANCOMPILER)
endif
ifeq ($(strip $(ENCODING)),)
   ENCODING=utf-8
endif
ifeq ($(strip $(ORGSRCDIR)),)
   ORGSRCDIR=""
endif
ifeq ($(strip $(CPP_FLAGS)),)
   CPP_FLAGS=""
endif


## the link happen here
$(EXECUTABLE):$(OBJS)
\t$(LINKER) -o $(EXECUTABLE) $(OBJS) $(LINKFLAGS)

## get the autogenerated dependencies

include .mkdep_dependencies

## delete object files (keep mkdep stuff)

clean:
\trm -f build/*.mod *.mod *~ $(EXECUTABLE) $(MAINLIB)
\t""" + pwd + """/rmobjs.py .mkdep_objects \n
\t""" + pwd + """/mkdep.py reset
\tmake dep

## (re)generate dependencies
dep:
\techo "" > .files
\tfor dir in $(SOURCEFILES); do \\
\t   echo "$$dir" >> .files ; \\
\tdone
\tfor dir in $(SOURCEFILES2); do \\
\t   echo "$$dir OPT2" >> .files ; \\
\tdone
\tif [ "$(strip $(ORGSRCDIR))" != "" ] ; then \\
\t   echo $(ORGSRCDIR) > .incdirs ;\\
\t   echo "." >> .incdirs ;\\
\t   """ + pwd + """/mergedotfiles.py $(ORGSRCDIR) ;\\
\telse \\
\t   echo "." > .incdirs ;\\
\t   """ + pwd + """/mergedotfiles.py NOORGSRCDIR ;\\
\tfi
\tif [ $(strip $(CPP_FLAGS)) != "" ] ; then \\
\t   """ + pwd + """/mkdep.py -e $(ENCODING) -i .incdirs --cpp=cpp --cppflags=$(CPP_FLAGS) .files ;\\
\telse \\
\t   """ + pwd + """/mkdep.py -e $(ENCODING) -i .incdirs .files ;\\
\tfi

## explicit rules for each and every file
include .mkdep_rules
\n"""))

    f.close()

