#= Makefile for dcpam top directory
#
# Authors::   Yasuhiro MORIKAWA
# Version::   $Id: Makefile,v 1.21 2012-09-08 15:32:01 yot Exp $
# Tag Name::  $Name:  $
# Copyright:: Copyright (C) GFD Dennou Club, 2004-2007. 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)
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
#
all:
	cd src || exit 1; $(MAKE)
	@echo ""
	@echo "        You have successfully built dcpam5."
#	@echo '    +--------------------------------------------------------'
#	@echo "    |   You have successfully built dcpam5."
#	@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 ""

lib:
	cd src || exit 1; $(MAKE) $@

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

install: all
	cd src || exit 1; $(MAKE) $@

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

clean: clean.lib
	cd src || exit 1; $(MAKE) $@

clean.all: clean.lib
	cd src || exit 1; $(MAKE) $@
	@test -d $(BINDIR) && $(RMDIR) $(BINDIR) 2> /dev/null || true
	@test -d $(LIBDIR) && $(RMDIR) $(LIBDIR) 2> /dev/null || true
	@test -d $(MODDIR) && $(RMDIR) $(MODDIR) 2> /dev/null || true

clean.lib:
	-$(RM) $(LIBDIR)/$(DCPAMLIB)
	-$(RM) $(MODDIR)/$(CLEAN_MODS)

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

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

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 src || exit 1; $(MAKE) $@

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

clean.depend: 
	@for i in src ; 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
	$(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:
	@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
