#---------------------------------------------------------------------
#     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
#---------------------------------------------------------------------
#= Makefile for dcpam top directory
#
#   * Developers: Yasuhiro MORIKAWA
#   * Version: $Id: Makefile,v 1.19 2005/06/22 08:57:44 morikawa Exp $
#   * Tag Name: $Name: dcpam2-20050623-4 $
#   * Change History: 
#
######################################################################
#
#== Settings

INCLUDEFILE = Config.mk
include $(INCLUDEFILE)

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

######################################################################
#== Commands for Guide files
#
RDFORM2HTM = $(RD2HTM_LIB) $(RD2_LIB_FLAG) $(RD2_CSSFLAG)
RDFORM2JA  = $(RDFORM2HTM) $(RD2_JA_FLAG) \
		--with-part=TOPLINK:rd --with-part=JAUPDATE:rd
RDFORM2EN  = $(RDFORM2HTM) $(RD2_EN_FLAG) \
		--with-part=TOPLINK:rd --with-part=ENUPDATE:rd

GuideFiles = INSTALL CREDITS HISTORY
GuideJA    = $(GuideFiles:%=%.htm)
GuideEN    = $(GuideFiles:%=%.htm.en)

######################################################################
#== Ruby liner script for replacing title in HTML
H1ToTitle  = $(RUBY) -e 'body = open(ARGV[0]){|io| io.read}; \
			 if /\<h1.*?\>(.*?)\<\/h1.*?\>/ =~ body then      ;\
			   h1 = $$1 ;\
			   h1 = h1.chomp.gsub!(/\<.*?\>/){|s| s = ""} ;\
			   mod = body.gsub(/\<title\>.*<\/title\>/,         \
			         "\<title\>" + h1 + "<\/title\>") ;\
			 else ;\
			   mod = body ;\
			 end ;\
			 out = open(ARGV[0], "w"); out.puts(mod)         ;\
			 out.close'

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

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

History     = HISTORY
HistoryTex  = $(History:%=doc/%.tex)
DverGen     = $(RUBY) -e '$$stdin.each{|line| ; \
			    if /^\$$Name: (\S+)/ =~ line then ;\
				$$tag = $$1 ;\
			    elsif /^\$$Date: (\S+) (\S+)/ =~ line then ;\
				$$day = $$1 ; \
			    end } ;\
			    $$tag = $$day.gsub(/\//, "") unless $$tag ;\
			    y, m, d = $$day.split("/") ;\
			    if /dcpam2-(\S+)/i =~ $$tag then ;\
				$$tag = $$1 ;\
			    end ;\
			  $$stdout.print "\\Dver{\#{$$tag}} " ;\
			  $$stdout.print "\\Dmodify{\#{y}}{\#{m}}{\#{d}}\n"'

######################################################################
#== Rules
#
all:
	cd src; $(MAKE)

doc: guide tex
	cd src; $(MAKE) $@
	cd doc; $(MAKE)

install: all
	cd src; $(MAKE) $@

install-doc:
	cd doc ; $(MAKE) install

clean:
	cd src; $(MAKE) $@
	cd practice; $(MAKE) $@

clean-doc: clean-guide clean-tex
	cd doc; $(MAKE) clean
	cd src; $(MAKE) $@
	cd bin; rm -f dycore init
	cd include; rm -f *.mod
	cd lib; rm -f libdcpam.a
	cd practice; $(MAKE) $@

distclean: clean clean-configure

clean-configure:
	-rm -f config.status chkfort.cfg config.cache config.log Config.mk
	@echo removing Config.mk, you should rerun \"configure\" to build dcpam

test-f: all
	cd test; $(MAKE) $@

test: all
	cd test; $(MAKE) $@

tags:
	cd src; $(MAKE) $@



guide: $(GuideJA) $(GuideEN)

clean-guide:
	$(RM) $(GuideJA) $(GuideEN)

tex: $(TexFiles)
	rm -f $(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)
	@$(DverGen) < $(History) >> $(HistoryTex)
	echo "done."

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


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


%.htm: %
	$(RDFORM2JA) $(@:.htm=) > $@ || rm -f $@
	@if [ -f $@ ]; then \
	    echo -n 'Copy <h1> to <title> ..' ; $(H1ToTitle) $@  ; echo 'done' ;\
	fi ;\

%.htm.en: %
	$(RDFORM2EN) $(@:.htm.en=) > $@ || rm -f $@
	@if [ -f $@ ]; then \
	    echo -n 'Copy <h1> to <title> ..' ; $(H1ToTitle) $@  ; echo 'done' ;\
	fi ;\

doc/%.tex: %
	rm -f $@
	echo "%" > $@
	echo "% This file is generated from" $< "automatically." >> $@
	echo "% So do not edit this file directly." >> $@
	echo "%" >> $@
	$(RDFORM2TEXJA) $< > /dev/null || eval "rm -f $@ ; exit 1"
	$(RDFORM2TEXJA) $< | $(TEX2BODY) >> $@ || rm -f $@


#== Rules for check commands
#
rd2_check:
	@$(RD2) --help > /dev/null 2>&1 || \
	    echo -e \
		"\n" \
		"    \"rd2\" is not found.\n" \
		"    Please get from http://www2.pos.to/~tosh/ruby/rdtool/\n"
	@$(RD2) --help > /dev/null 2>&1 || exit 1

rd2htm_lib_check:
	@$(RD2HTM_LIB) --help > /dev/null 2>&1 || \
	    echo -e \
		"\n" \
		"    \"rd2html-lib.rb\" is not found.\n" \
		"    Please get from http://www2.pos.to/~tosh/ruby/rdtool/\n"
	@$(RD2HTM_LIB) --help > /dev/null 2>&1 || exit 1

rd2htm_ext_lib_check:
	@$(RD2HTM_EXT_LIB) --help > /dev/null 2>&1 || \
	    echo -e \
		"\n" \
		"    \"rd2html-ext-lib.rb\" is not found.\n" \
		"    Please get from http://www.rubyist.net/~rubikitch/computer/rd2html-ext/\n"
	@$(RD2HTM_EXT_LIB) --help > /dev/null 2>&1 || exit 1
