#= Makefile for dcpam top directory
#
# Authors::   Yasuhiro MORIKAWA, Shin-ichi Takehiro
# Version::   $Id: Makefile,v 1.22 2025/09/19 10:00:00 takepiro Exp $
# Tag Name::  $Name:  $
# Copyright:: Copyright (C) GFD Dennou Club, 2004-2025. All rights reserved.
# License::   See COPYRIGHT[link:COPYRIGHT]
#
######################################################################
#
#== Settings

DIRDEPTH=.
INCLUDEFILE = $(DIRDEPTH)/Config.mk
include $(INCLUDEFILE) # Include file              (optional)
                       # インクルードファイル      (任意)

GuideFiles = INSTALL CREDITS HISTORY
                       # Documentation located in top directory       (optional)
                       # トップディレクトリに置くドキュメントファイル (任意)

TexSrcFiles = CREDITS

# End Settings
######################################################################

######################################################################
#== Set GuideRDFiles
GuideRDFiles = $(GuideFiles:%=%.rd)
GuideJA      = $(GuideFiles:%=%.htm.ja)
GuideEN      = $(GuideFiles:%=%.htm.en)

######################################################################
#== Commands for TeX files
#
RUBYLIB = script
RDFORM2TEX  = $(RD2) -r rd/rd2latex-lib.rb --documentclass="jreport"
RDFORM2TEXJA= $(RDFORM2TEX) --with-part=JA:rd --out-code=euc
TEX2BODY    = $(RUBY) $(RUBYLIB)/rd/latex2body.rb

TexFiles    = $(TexSrcFiles:%=doc/%.tex)

History     = HISTORY
HistoryTex  = $(History:%=doc/%.tex)

#Copyright := `cat COPYRIGHT`

######################################################################
#== Rules
#
serial:
	$(MAKE) clean
	cd libsrc || exit 1; $(MAKE) $@
	cd bin || exit 1; $(MAKE)

zonal: 
	$(MAKE) clean
	cd libsrc || exit 1; $(MAKE) $@
	cd bin || exit 1; $(MAKE)  $@

mpi: 
	$(MAKE) clean
	cd libsrc || exit 1; $(MAKE) $@
	cd bin || exit 1; $(MAKE)  $@

all: serial zonal mpi
	@echo ""
	@echo "        You have successfully built DCPAM6 library."
#	@echo '    +--------------------------------------------------------'
#	@echo "    |   You have successfully built DCPAM6."
#	@echo "    |"
#	@echo "    |   Next, move to a working top directory \"practice\"."
#	@echo "    |   Then command \"make\". As a result, "
#	@echo "    |   a working directory will be created "
#	@echo "    |   under \"practice\", and built executable files "
#	@echo "    |   and some NAMELIST files will be copied to "
#	@echo "    |   the working directory."
#	@echo "    |"
#	@echo "    |   Edit the NAMELIST files, and run the executable files"
#	@echo "    |   as follows."
#	@echo "    |"
#	@echo "    |       ./dcpam_main -N=dcpam_ape_T21L16.nml"
#	@echo "    |"
#	@echo "    |   For details, please see the DCPAM web site:"
#	@echo "    |   http://www.gfd-dennou.org/library/dcpam/"
#	@echo '    +--------------------------------------------------------'
	@echo ""

bin:
	cd bin || exit 1; $(MAKE)

exec:
	cd execsrc || exit 1; $(MAKE)

doc: guide
	cd doc || exit 1; $(MAKE)
#	cd libsrc || exit 1; $(MAKE) $@

doc-noref: guide
	cd doc || exit 1; $(MAKE) docs

install: all
	cd lib || exit 1; $(MAKE) $@
	cd include || exit 1; $(MAKE) $@
	cd bin || exit 1; $(MAKE) $@

install-doc:
	cd doc || exit 1 ; $(MAKE) install

clean: 
	-@$(RM) $(wildcard *~)
	cd libsrc || exit 1; $(MAKE) $@
	cd lib || exit 1; $(MAKE) $@
	cd bin || exit 1; $(MAKE) $@
	cd execsrc || exit 1; $(MAKE) $@

clean.all:
	cd libsrc || exit 1; $(MAKE) $@
	cd lib || exit 1; $(MAKE) $@
	cd bin || exit 1; $(MAKE) $@
	cd execsrc || exit 1; $(MAKE) $@
	-@$(RM) $(wildcard $(MODDIR)/$(CLEAN_MODS) $(MODZONALDIR)/$(CLEAN_MODS)  $(MODMPIDIR)/$(CLEAN_MODS))
#	-@$(RM) $(MODDIR)/* 2> /dev/null || true
#	-@$(RM) $(MODZONALDIR)/* 2> /dev/null || true
#	-@$(RM) $(MODMPIDIR)/* 2> /dev/null || true
# @test -d $(MODDIR) && $(RMDIR) $(MODDIR) 2> /dev/null || true
# @test -d $(MODZONALDIR) && $(RMDIR) $(MODZONALDIR) 2> /dev/null || true
# @test -d $(MODMPILDIR) && $(RMDIR) $(MODMPIDIR) 2> /dev/null || true

clean.bin:
	-cd $(BINDIR) ; $(RM) $(TARGETS)

clean.doc: 
	cd doc || exit 1; $(MAKE) clean

clean.all.doc: clean.guide clean.tex
	cd doc || exit 1; $(MAKE) clean.all

distclean: clean.all clean.configure

clean.configure:
	-@$(RM) config.status chkfort.cfg chkgmake.cfg chkrps.cfg
	-@$(RM) config.cache config.log Config.mk
	-@$(RM) autom4te.cache/*
	@test -d autom4te.cache && $(RMDIR) autom4te.cache 2> /dev/null || true
	@echo ""
	@echo "    Removing Config.mk,"
	@echo "      you should rerun ./configure to build dcpam."
	@echo ""

tags:
	cd libsrc || exit 1; $(MAKE) $@

depend: 
	@for i in libsrc ; do \
	  cd $$i || exit 1; \
	  $(MAKE) $@; \
	  cd .. ; \
	done

clean.depend: 
	@for i in libsrc ; do \
	  cd $$i || exit 1; \
	  $(MAKE) $@; \
	  cd .. ; \
	done

guide: 
	@for file in $(GuideFiles) ; do \
	    echo $(CP) $${file} $${file}.rd ;\
	    $(CP) $${file} $${file}.rd ;\
	done
#	$(MAKE) -f htmltools/Makefile.rd2html
	rake 
	$(RM) $(GuideRDFiles)
#	cd doc || exit 1 ; $(MAKE) -f Makefile.rd2html

clean.guide:
	@if [ ! X"$(wildcard $(GuideJA) $(GuideEN) $(GuideRDFiles))" = X"" ]; then \
	    echo $(RM) $(wildcard $(GuideJA) $(GuideEN) $(GuideRDFiles)) ;\
	    $(RM) $(wildcard $(GuideJA) $(GuideEN) $(GuideRDFiles)) ;\
	fi

tex: $(TexFiles)
	-$(RM) $(HistoryTex)
	@echo -n "Generating" $(HistoryTex) "from" $(History) "... "
	@echo "%" > $(HistoryTex)
	@echo "% This file is generated from" $(History) \
	     "automatically." >> $(HistoryTex)
	@echo "% So do not edit this file directly." >> $(HistoryTex)
	@echo "%" >> $(HistoryTex)
	@$(RUBY) $(CVSTAG2LATEX) < $(History) >> $(HistoryTex)
	echo "done."

clean.tex:
	-$(RM) $(TexFiles) $(HistoryTex)


cl:
	@if [ ! -x `which git2cl` ]; then \
		echo -n "git2cl is is not found."; \
		exit 1; \
	fi
	@echo "This file is generated automatically by git2cl.\n" > ChangeLog
	@git2cl >> ChangeLog
	@echo "ChangeLog is Updated."

#	@cvs2cl --help > /dev/null 2>&1 || \
#	    echo -e \
#		"\n" \
#		"    cvs2cl is not found.\n" \
#		"    Please get from http://www.red-bean.com/cvs2cl/\n"
#	@cvs2cl --help > /dev/null 2>&1 && \
#	    echo -e \
#		"This file is generated automatically by\n" \
#		"\n" \
#		"    cvs2cl -r -b -t --fsf -w --header <This header>\n" \
#		"\n" \
#		`cvs2cl --version` "\n" \
#		"This script is available from "      \
#		"http://www.red-bean.com/cvs2cl/\n\n" \
#		    | cvs2cl -r -b -t --fsf -w --header -
#	rm -f ChangeLog.bak
#	@echo -e "\n" "ChangeLog is Updated." "\n"

doc/%.tex: %
	-$(RM) $@
	echo "%" > $@
	echo "% This file is generated from" $< "automatically." >> $@
	echo "% So do not edit this file directly." >> $@
	echo "%" >> $@
	$(RDFORM2TEXJA) $< > /dev/null || eval "$(RM) $@ ; exit 1"
	#$(RDFORM2TEXJA) $< | $(TEX2BODY) >> $@ || $(RM) $@
	#$(RDFORM2TEXJA) $< | $(TEX2BODY) | sed -e 's/^\\chapter.*$$/\\Dchapterhead/' >> $@ || $(RM) $@
	$(RDFORM2TEXJA) $< | $(TEX2BODY) | sed -e "s|COPYRIGHT|\\\\htmladdnormallinkfoot{COPYRIGHT}{$(COPYRIGHTURL)}|" >> $@ || $(RM) $@
#	$(RDFORM2TEXJA) $< | $(TEX2BODY) | sed -e 's/^\\chapter.*$$/\\Dchapterhead/' | sed -e 's/^COPYRIGHT.*$$/$(Copyright)/' >> $@ || $(RM) $@

create-configure:
	@if [ ! -x /usr/bin/autoconf ]; then \
		echo -n "autoconf(1) is not valid. "; \
		echo "This is \"configure\" Generater."; \
		exit 1; \
	fi
	@if [ ! -r configure.in ]; then \
		echo -n "configure.in is not exists or not readable. "; \
		echo "This file needed for creating \"configure\"."; \
		exit 1; \
	fi
	@if [ ! -r script/configure/aclocal.m4 ]; then \
		echo -n "aclocal.m4 is not exists or not readable. "; \
		echo "This file needed for creating \"configure\"."; \
		exit 1; \
	fi
	autoconf -B script/configure
	@echo "\"configure\" Generated."

expdir:
	$(SH) ./setup_expdir_nonstd.sh
