# Makefile for compiling modflow usg
# Mike Ronayne and Kristen Cognac, 5/11/23

# Define the Fortran compile flags
# "-w" suppresses all warning messages
# "-c" means compile only (produce .o file for each source code file)
# "-I...." specify a path so that the directory will be searched for INCLUDE files 

# Build configuration: Debug or Release
# Default: Release
# To build Debug: make BUILD=Debug
# To build Release: make BUILD=Release (or just 'make')
BUILD ?= Release

# Build directory
BUILD_DIR = $(BUILD)

# Compiler selection:
# Default: gfortran (GNU Fortran)
# To use Intel IFX: make F77=ifx BUILD=Release
# To use Intel IFX with debug: make F77=ifx BUILD=Debug

F77 = gfortran

# Compiler flags based on build type
ifeq ($(BUILD),Debug)
    # Debug flags for gfortran
    F77FLAGS = -g -O0 -fbacktrace -ffpe-summary=overflow -fdec -fcheck=all
    # Debug flags for Intel IFX (use when F77=ifx)
    # NOTE: use '-debug full' (ifx) instead of bare '-g', which is ambiguous
    # with other '/g*' options in the Windows ifx driver.
    IFXFLAGS = -O0 -debug full -check all -traceback -warn all
else
    # Release flags for gfortran
    F77FLAGS = -O2 -fbacktrace -ffpe-summary=overflow -fdec
    # Release flags for Intel IFX (use when F77=ifx)
    # NOTE: bare '-g' is ambiguous for the Windows ifx driver; use '-traceback'
    # (add '-debug full' here if debug symbols are needed in Release).
    IFXFLAGS = -O2 -traceback
endif

# Override flags if using Intel IFX
ifeq ($(F77),ifx)
    F77FLAGS = $(IFXFLAGS)
endif

# Specify directory where executable will be installed
BINDIR = './bin/'

# Define all object files (with build directory prefix)
OBJECTS = $(addprefix $(BUILD_DIR)/,\
	gmodules.o \
	density.o \
	gwf2evt8u1.o \
	gwf2ets8u1.o \
	gwf2rch8u1.o \
	lak_gag_sfr_modules.o \
	gwf2lak7u1.o \
	gwf2wel7u1.o \
	gwf2drn7u1.o \
	gwf2riv7u1.o \
	gwf2ghb7u1.o \
	gwf2str7u1.o \
	gwf2drt8u.o \
	gwf2QRT8u.o \
	sparse.o \
	gwf2dpf1u1.o \
	gwf2basu1.o \
	gwf2bcf-lpf-u1.o \
	gwf2chd7u1.o \
	gwf2fhb7u1.o \
	gwf2gag7u1.o \
	gwf2hfb7u1.o \
	gwf2sfr7u1.o \
	gwf2sub7u1.o \
	gwt2aw_adsorb.o \
	gwt2bndsu1.o \
	gwt2dptu1.o \
	gwt2rxnu1.o \
	gwt2mdtu1.o \
	xmdlib_2.o \
	xmd.o \
	pcgu7.o \
	ReadCBC.o \
	mfusg.o \
	cln2basu1.o \
	cln2props1.o \
	disu2gncb1.o \
	disu2gncn1.o \
	dpt2aw_adsorb.o \
	glo2btnu1.o \
	glo2sgbu1.o \
	glo2sms-u1.o \
	parutl7.o \
	tvmu1.o \
	tvmu2.o \
	utl7u1_RD.o \
	utl7u1.o \
	UpdtSt.o \
	glo2basu1.o \
)

# Executable path
EXECUTABLE = $(BUILD_DIR)/mfusg.exe

# Default target
all: $(EXECUTABLE)

# Create build directory if it doesn't exist
$(BUILD_DIR):
	@mkdir -p "$(BUILD_DIR)" 2>nul || mkdir "$(BUILD_DIR)" 2>nul || true

# Build executable
$(EXECUTABLE): $(BUILD_DIR) $(OBJECTS)
	$(F77) -o $(EXECUTABLE) $(OBJECTS)

# Pattern rule to compile .f files to .o files in build directory
$(BUILD_DIR)/%.o: %.f | $(BUILD_DIR)
	$(F77) -c $(F77FLAGS) -o "$@" "$<"

# Pattern rule for .for files (some Fortran files use .for extension)
$(BUILD_DIR)/%.o: %.for | $(BUILD_DIR)
	$(F77) -c $(F77FLAGS) -o $@ $<

# Specific rule for mfusg.o (if needed)
$(BUILD_DIR)/mfusg.o: mfusg.f | $(BUILD_DIR)
	$(F77) -c $(F77FLAGS) -o $@ $<

# Convenience targets
debug:
	@$(MAKE) BUILD=Debug

release:
	@$(MAKE) BUILD=Release

# Install target (copies executable to bin directory)
install: $(EXECUTABLE)
	@mkdir -p "$(BINDIR)" 2>nul || mkdir "$(BINDIR)" 2>nul || true
	@cp $(EXECUTABLE) $(BINDIR) 2>nul || copy $(EXECUTABLE) $(BINDIR)

# Clean targets (works with both Windows CMD and Unix shells)
clean:
	@rm -f Debug/*.o Release/*.o Debug/mfusg.exe Release/mfusg.exe 2>nul || \
	 (if exist "Debug\*.o" del /q "Debug\*.o" 2>nul & \
	  if exist "Release\*.o" del /q "Release\*.o" 2>nul & \
	  if exist "Debug\mfusg.exe" del /q "Debug\mfusg.exe" 2>nul & \
	  if exist "Release\mfusg.exe" del /q "Release\mfusg.exe" 2>nul)

clean-debug:
	@rm -f Debug/*.o Debug/mfusg.exe 2>nul || \
	 (if exist "Debug\*.o" del /q "Debug\*.o" 2>nul & \
	  if exist "Debug\mfusg.exe" del /q "Debug\mfusg.exe" 2>nul)

clean-release:
	@rm -f Release/*.o Release/mfusg.exe 2>nul || \
	 (if exist "Release\*.o" del /q "Release\*.o" 2>nul & \
	  if exist "Release\mfusg.exe" del /q "Release\mfusg.exe" 2>nul)

# Help target
help:
	@echo Build configuration: $(BUILD)
	@echo Compiler: $(F77)
	@echo Flags: $(F77FLAGS)
	@echo Build directory: $(BUILD_DIR)
	@echo Executable: $(EXECUTABLE)
	@echo.
	@echo Available targets:
	@echo   all          - Build $(BUILD) version (default)
	@echo   debug        - Build Debug version
	@echo   release      - Build Release version
	@echo   clean        - Remove all build artifacts
	@echo   clean-debug  - Remove Debug build artifacts
	@echo   clean-release- Remove Release build artifacts
	@echo   install      - Copy executable to bin directory
	@echo   help         - Show this help message
	@echo.
	@echo Examples:
	@echo   make                    - Build Release with gfortran
	@echo   make BUILD=Debug        - Build Debug with gfortran
	@echo   make F77=ifx BUILD=Debug - Build Debug with Intel IFX
	@echo   make F77=ifx            - Build Release with Intel IFX

#  end
