######################################################################
# Define needed utilities
F90=		ifort
CFCNVT=		cfcnvt
GMAN=		gman_pre.exe
RESPLT=		resplt.pl
CFPOST=		cfpost_prod.exe
GEN2TEC=	gen2tec.pl

######################################################################
.PHONY: help
help:
	#@grep -h '^#HELP: ' Makefile | sed -e 's/#HELP: //'
	@grep -h '^#HELP: ' $(MAKEFILE_LIST) | sed -e 's/#HELP: //'

#HELP: Makefile will create case4 runfiles for Wind-US.
#HELP:   Targets:
#HELP:     all
#HELP:       inputs
#HELP:         grid
##HELP:           case4.xyz
##HELP:           case4.cgd
#HELP:         dat
##HELP:           case4.dat
##HELP:           case4.dat.commented
#HELP:       post
#HELP:         chist
#HELP:         pdist
#HELP:         pcont
#HELP:     clean_all
#HELP:       clean_all_inputs
#HELP:         clean_all_grid
#HELP:         clean_all_dat
#HELP:       clean_all_post
#HELP:         clean_all_chist
#HELP:         clean_all_pdist
#HELP:         clean_all_pcont
######################################################################
.PHONY: all
all: inputs post

.PHONY: inputs
inputs: grid dat

.PHONY: grid
grid: case4.cgd

.PHONY: dat
dat: case4.dat

.PHONY: post
post: chist pdist pcont
######################################################################
%.exe: %.f90
	@echo
	@echo "Compile $@"
	$(F90) $(F90FLAGS) $< -o $@

######################################################################
case4.cgd: case4.cgd.orig gman.bc.inp
	@echo
	@echo "Use GMAN to set boundary conditions on $@"
	cp -p case4.cgd.orig $@
	rm -f gman.bcreport.out
	$(GMAN) < gman.bc.inp > gman.bc.out
	@echo "  Now have:  $@"
	@echo "  Now have:  gman.bcreport.out"

case4.cgd.orig: case4.xyz cfcnvt.inp
	@echo
	@echo "Use CFCNVT to convert case4.xyz to $@"
	$(CFCNVT) < cfcnvt.inp > cfcnvt.out
	@echo "  Now have:  $@"

case4.xyz: case4.mesh.exe
	@echo
	@echo "Create plot3d grid file $@"
	case4.mesh.exe

######################################################################
case4.dat: case4.dat.make.pl
	@echo
	@echo "Create DAT file $@"
	case4.dat.make.pl
#	# Perl script already provides similar output.
#	@echo "  Now have:  case4.dat"
#	@echo "  Now have:  case4.dat.commented"

######################################################################
case4.cfl: case4.cgd case4.dat
	@echo "$@ is out of date."
	@echo "Run Wind-US, then rerun this makefile."
	@exit 1;

case4.lis: case4.cgd case4.dat
	@echo "$@ is out of date."
	@echo "Run Wind-US, then rerun this makefile."
	@exit 1;

######################################################################
CHIST_GENPLOT_FILES= \
chist_big.gen \
chist_l2.gen \
chist_mass.gen

CHIST_TECPLOT_FILES= \
chist_big.tec \
chist_l2.tec \
chist_mass.tec

.PHONY: chist_banner
chist_banner:
	@echo
	@echo "Creating convergence history data files"

.PHONY: chist
chist:  | chist_banner \
        $(CHIST_GENPLOT_FILES) \
        $(CHIST_TECPLOT_FILES)

chist_%.gen: resplt_%.inp case4.lis
	$(RESPLT) < resplt_$*.inp > resplt_$*.out
	@echo "  Now have:  $@"

######################################################################
%.tec: %.gen
	$(GEN2TEC) $*.gen
	@echo "  Now have $@"

######################################################################
PDIST_FILES= \
pdist.gen \
pdist_upper.gen \
pdist_upper.lis \
pdist_upper.lis.raw \
pdist_lower.gen \
pdist_lower.lis \
pdist_lower.lis.raw

PDIST_FILES_TECPLOT= \
pdist.tec \
pdist_upper.tec \
pdist_lower.tec 

.PHONY: pdist
pdist: $(PDIST_FILES) $(PDIST_FILES_TECPLOT)

# This CFPOST script creates several files, so output them here.
pdist.gen: case4.cgd case4.cfl cfpost_pdist.jou
	@echo
	@echo "Creating pressure distribution data files."
	$(CFPOST) < cfpost_pdist.jou > cfpost_pdist.out
	@for i in $(PDIST_FILES); do\
	  echo "  Now have $$i"; \
	done

# These files are created with pdist.gen.
pdist_upper.gen: pdist.gen
pdist_lower.gen: pdist.gen
pdist_upper.lis: pdist.gen
pdist_lower.lis: pdist.gen
pdist_upper.lis.raw: pdist.gen
pdist_lower.lis.raw: pdist.gen

######################################################################
PCONT_FILES= \
pcont.xyz \
pcont.q   \
pcont.fun \
pcont.nam

.PHONY: pcont
pcont: pcont.fun pcont.gpc

# Plot3d function file.
pcont.fun: case4.cgd case4.cfl cfpost_pcont.jou
	@echo
	@echo "Creating plot3d pressure contour data files."
	$(CFPOST) < cfpost_pcont.jou > cfpost_pcont.out
	@for i in $(PCONT_FILES); do\
	  echo "  Now have $$i"; \
	done

# Other Plot3d files created with pcont.fun.
pcont.xyz: pcont.fun
pcont.q:   pcont.fun
pcont.nam: pcont.fun

# Genplot surface file.
pcont.gpc: case4.cgd case4.cfl cfpost_pcont_gpc.jou
	@echo
	@echo "Creating genplot pressure contour data file."
	$(CFPOST) < cfpost_pcont_gpc.jou > cfpost_pcont_gpc.out
	@echo "  Now have $@";

######################################################################
# Remove all files that can be recreated.
.PHONY: clean_all
clean_all: clean_all_exe clean_all_inputs clean_all_post

.PHONY: clean_all_exe
clean_all_exe:
	rm -f case4.mesh.exe

.PHONY: clean_all_inputs
clean_all_inputs: clean_all_grid clean_all_dat

.PHONY: clean_all_grid
clean_all_grid:
	@echo "Remove grid files"
	rm -f case4.xyz
	rm -f mesh.plotin.data
	rm -f cfcnvt.out
	rm -f gman.jou
	rm -f gman.bc.out
	rm -f gman.bcreport.out
	rm -f case4.cgd.orig
	rm -f case4.cgd

.PHONY: clean_all_dat
clean_all_dat:
	@echo "Remove dat files"
	rm -f case4.dat
	rm -f case4.dat.commented

.PHONY: clean_all_post
clean_all_post: clean_all_chist \
                clean_all_pdist \
                clean_all_pcont

.PHONY: clean_all_chist
clean_all_chist:
	@echo "Remove convergence history files"
	@for i in $(CHIST_GENPLOT_FILES) $(CHIST_TECPLOT_FILES); do\
	  echo "rm -f $$i"; \
	        rm -f $$i; \
	done

.PHONY: clean_all_pdist
clean_all_pdist:
	@echo "Remove pressure-distribution files"
	@for i in $(PDIST_FILES) $(PDIST_FILES_TECPLOT); do\
	  echo "rm -f $$i"; \
	        rm -f $$i; \
	done

.PHONY: clean_all_pcont
clean_all_pcont:
	@echo "Remove pressure-contour files"
	@for i in $(PCONT_FILES) pcont.gpc; do\
	  echo "rm -f $$i"; \
	        rm -f $$i; \
	done

######################################################################
# Instructions for bulding and uploading the tutorial tarfile.
-include Makefile_tar

