#*************************************************************************
#                                                                        *
# GNUmakefile (sun4 or sun5, depending upon .cshrc):                     *
#                                                                        *
# Script to C-preprocess, compile, and link MKMKFLUX, a "pre-processor"  *
# for make_flux. It creates the executable file specified in the macro   *
# BIN.                                                                   *
#                                                                        *
# This makefile is written for GNU Make, version 3.75                    *
#                                                                        *
#    Free Software Foundation          (617) 876-3296                    *
#    675 Mass Ave.                     gnu@prep.ai.mit.edu               *
#    Cambridge, MA  02139, USA         http://www.gnu.ai.mit.edu/        *
#                                                                        *
# It is NOT compatible with the standard UNIX Make.                      *
#                                                                        *
# Executable:                                                            *
#                                                                        *
#    mkmkflux    Program to process FNMOC atmospheric data.   (default)  *
#                                                                        *
# GNUmake targets:                                                       *
#                                                                        *
#    clean       Remove all intermediate files.                          *
#                                                                        *
# This makefile has been designed to allow the user to compile the       *
# MKMKFLUX source codes in a separate directory from that in which the   *
# source codes are located.  The makefile searches for the code segments *
# in the following alternate paths:                                      *
#                                                                        *
#    source code:  (1) the directory containing this GNUmakefile.        *
#                  (2) the directory specified by the macro SRCDIR       *
#                                                                        *
#    include files:  (1) the directory containing this GNUmakefile.      *
#                    (2) the directory specified by the macro PARAMDIR   *
#                    (3) the directory specified by the macro SRCDIR     *
#                                                                        *
# This provides the user with the flexibility for the following          *
# configurations:                                                        *
#                                                                        *
#    (1) The user needs only copies of this GNUmakefile, the parameter   *
#        file "param.h" and a path to the source codes to produce a      *
#        version of MKMKFLUX with the appropriate C-preprocessing and    *
#        compilier options.                                              *
#                                                                        *
#    (2) The user who is modifying MKMKFLUX, can isolate                 *
#        those routines actually being changed with a copy of this       *
#        GNUmakefile in a sub-directory.                                 *
#                                                                        *
#                                                                        *
#                             Patrick J. Haley Jr.    05/28/2001         *
#                             MKALADWIND Version      1.1                *
#                                                                        *
#*************************************************************************
#************************ USER's tunable macros **************************
#*************************************************************************
#
# BIN           name of the executable code
# BINDIR        directory path for executable code
# CFLAGS        flags for the C compiler
# CPPFLAGS      C-preprocessing flags and options
# FFLAGS        flags for the FORTRAN compiler
# PARAMDIR      directory path for include files
# SRCDIR        directory path for source codes
#
#*************************************************************************
#
# C-preprocessing options:
#
#  -Dgendbg          Generic Debugging:  Preserves intermediate files
#  -Dknowtime        Use knowledge of current time to limit search.
#                    (an unreliable option)
#  -Dsunfpe          SUN's floating point exception trap
#
#************************************************************************

  SRCDIR = ../Lib
PARAMDIR = .
  BINDIR = ..

     BIN = mkaladwind
CPPFLAGS =

  FFLAGS = -u -fast -fsimple=1 -ansi
  CFLAGS =

#************************************************************************
#******************** End of USER's tunable macros **********************
#************************************************************************

#------------------------------------------------------------------------
#--------------------- Internal macro definitions. ----------------------
#------------------------------------------------------------------------

RMBLKLINES = rmblklines
     SHELL = /bin/sh
        RM = rm -f
      ECHO = echo
     FALSE = false
       LIB =
       CPP = cpp -P -C -I. -I$(PARAMDIR) -I$(SRCDIR)
        FC = f77
        CC = cc

#------------------------------------------------------------------------
#---------------- Look for bad C Pre-Processing Flags. ------------------
#------------------------------------------------------------------------

VALID_CPP_OPT = -Dgendbg -Dknowtime -Dsunfpe

INVALID_CPP_OPT = $(strip $(filter-out $(VALID_CPP_OPT),$(CPPFLAGS)))

#------------------------------------------------------------------------
#--------------------- Redefine compiling macros. -----------------------
#------------------------------------------------------------------------

MAKEFLAGS := -r -$(MAKEFLAGS)

.SUFFIXES:

.SUFFIXES: .o .f .F .FF .c .C

%.o:	%.f
	-$(FC) -c $(FFLAGS) -o $*.o $*.f

%.o:	%.c
	-$(CC) -c $(CFLAGS) -o $*.o $*.c

%.o:	%.F

%.o:	%.FF

%.o:	%.C

%.f:	%.F
	-$(CPP) $(CPPFLAGS) $< ./$*.cpp
	-./$(RMBLKLINES) < $*.cpp > $*.f
	-$(RM) $*.cpp

%.f:	%.FF
	-$(CPP) $(CPPFLAGS) $< ./$*.f

%.c:	%.C
	-$(CPP) $(CPPFLAGS) $< ./$*.c

#------------------------------------------------------------------------
#-------------- Define alternate paths for source codes. ----------------
#------------------------------------------------------------------------

#--------------------------
#--- Define source path ---
#--------------------------

testpath = .
pathstr  =

ifeq (,$(strip $(filter $(SRCDIR),$(subst :, ,$(testpath)))))
   testpath := $(testpath):$(SRCDIR)
   pathstr  := $(SRCDIR)
endif

ifneq (,$(pathstr))
   vpath %.F   $(pathstr)
   vpath %.FF  $(pathstr)
   vpath %.C   $(pathstr)
endif

#---------------------------
#--- Define include path ---
#---------------------------

ifeq (,$(strip $(filter $(PARAMDIR),$(subst :, ,$(testpath)))))
   testpath := $(PARAMDIR):$(testpath)
   ifneq (,$(pathstr))
      pathstr  := $(PARAMDIR):$(pathstr)
     else
      pathstr  := $(PARAMDIR)
   endif
endif

ifneq (,$(pathstr))
   vpath %.h   $(pathstr)
   vpath %.inc $(pathstr)
endif

#------------------------------------------------------------------------
#-------- Define string macro to parse C Pre-Processing Flags. ----------
#------------------------------------------------------------------------

find_flag = $(findstring $(flag),$(CPPFLAGS))

#------------------------------------------------------------------------
#------- Preserve intermediate source codes only when debugging. --------
#------------------------------------------------------------------------

dbg_flags = sunfpe gendbg

ifneq (,$(strip $(foreach flag,$(dbg_flags),$(find_flag))))
   .PRECIOUS: %.f %.c
endif

#------------------------------------------------------------------------
#--- Declare "phony" targets.  GNUmake can always run these targets. ----
#------------------------------------------------------------------------

ifeq (,$(INVALID_CPP_OPT))
.PHONY: clean
else
.PHONY: clean $(BIN)
endif

#------------------------------------------------------------------------
#---------------------------- Source Codes. -----------------------------
#------------------------------------------------------------------------

#------------------------------------------------------------------------
#  Basic routines.
#------------------------------------------------------------------------

  FSRC = 		mkaladwind.F					\
	advtime.F	caldate.F	findname.F	getname.F	\
	gregorian.F	intbas.F	julian.F	length.F	\
	newtime.F	no_digit.F	read_mkflx.F	regtime.F	\
	set_files.F	wrt_file.F

  CSRC = getutc.C

#------------------------------------------------------------------------
#  Routine for SUN's floating point exception trap.
#------------------------------------------------------------------------

   DBGSRC = my_handler.F

#------------------------------------------------------------------------
#--------------------- Active Sources and Objects. ----------------------
#------------------------------------------------------------------------

    OBJS = $(FSRC:.F=.o)

ifneq (,$(findstring knowtime,$(CPPFLAGS)))
   OBJS := $(OBJS) $(CSRC:.C=.o)
endif

ifneq (,$(findstring sunfpe,$(CPPFLAGS)))
   OBJS := $(OBJS) $(DBGSRC:.F=.o)
endif

#------------------------------------------------------------------------
#--------------------------- Strip out tabs. ----------------------------
#------------------------------------------------------------------------

empty :=
space := $(empty) $(empty)
tab := $(empty)	$(empty)

OBJSnt := $(strip $(subst $(tab),$(space),$(OBJS)))

#------------------------------------------------------------------------
#-------------------------- Create MKMKFLUX. ----------------------------
#------------------------------------------------------------------------

ifeq (,$(INVALID_CPP_OPT))
$(BIN):		$(RMBLKLINES) $(OBJSnt)
		@($(ECHO) "";\
                $(ECHO) "Creating MKMKFLUX:" ;\
	        $(ECHO) "" )
		$(FC) $(FFLAGS) -o $(BINDIR)/$(BIN) $(OBJSnt) $(LIB)
else
$(BIN):
		@($(ECHO) "";\
                $(ECHO) "***********************************" ;\
                $(ECHO) "*** Invalid CPP options present ***" ;\
                $(ECHO) "***********************************" ;\
                $(ECHO) "" ;\
                $(ECHO) "$(INVALID_CPP_OPT)" ;\
                $(ECHO) "" ;\
                $(ECHO) "***********************************" ;\
                $(ECHO) "*** No compilation will be done ***" ;\
                $(ECHO) "***********************************" ;\
	        $(ECHO) "" ;\
                $(FALSE) )
endif

#------------------------------------------------------------------------
#---------------- Create remove blank lines executable. -----------------
#------------------------------------------------------------------------

$(RMBLKLINES):	rmblklines.o
		@($(ECHO) "";\
                $(ECHO) "Making post-processing code  $(RMBLKLINES):";\
		$(ECHO) "" )
		$(FC) $(FFLAGS) -o $(RMBLKLINES) rmblklines.o
		@($(ECHO) "" )

#------------------------------------------------------------------------
#--------- Target for cleaning preprocessed and object files. -----------
#------------------------------------------------------------------------

clean:
		@$(RM) *.f *.c *.o $(RMBLKLINES)

#------------------------------------------------------------------------
#------------- Set dependencies for C-preprocessed files. ---------------
#------------------------------------------------------------------------

mkaladwind.f:	mkflx_tim.h

read_mkflx.f:	pconst.h	mkflx_tim.h

set_files.f:	pconst.h	mkflx_tim.h

wrt_file.f:	mkflx_tim.h
