#---------------------------------------------------------------------
#     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
#---------------------------------------------------------------------
#= Makefile for dcpam top directory
#
#   * Developers: Yasuhiro MORIKAWA
#   * Version: $Id: Makefile,v 1.4 2006/09/09 04:55:25 morikawa Exp $
#   * Tag Name: $Name: dcpam3-20061118 $
#   * 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.all:
	cd src; $(MAKE) $@
	cd practice; $(MAKE) $@
	-$(RMDIR) bin lib include


clean-doc: clean-guide clean-tex
	cd doc; $(MAKE) clean

distclean: clean.all clean-configure

clean-configure:
	-$(RM) -f config.status chkfort.cfg chkgmake.cfg config.cache config.log Config.mk
	@echo ""
	@echo "    Removing Config.mk,"
	@echo "      you should rerun ./configure to build dcpam."
	@echo ""

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) $(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 --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 ChangeLog.bak
	@echo -e "\n" "ChangeLog is Updated." "\n"


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

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

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) $@


#== 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
