#= Makefile for dcpam top directory
#
# Authors::   Yasuhiro MORIKAWA
# Version::   $Id: Makefile,v 1.9 2008-03-25 00:02:13 morikawa Exp $
# Tag Name::  $Name: dcpam4-20080427 $
# Copyright:: Copyright (C) GFD Dennou Club, 2004-2007. All rights reserved.
# License::   See COPYRIGHT[link:COPYRIGHT]
#
######################################################################
#
#== Settings

INCLUDEFILE = 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)
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("/") ;\
			    m = m.gsub(/^0+/, "") ;\
			    d = d.gsub(/^0+/, "") ;\
			    if /dcpam\d-(\S+)/i =~ $$tag then ;\
				$$tag = $$1 ;\
			    end ;\
			  $$stdout.print "\\Dver{\#{$$tag}} " ;\
			  $$stdout.print "\\Dmodify{\#{y}}{\#{m}}{\#{d}}\n"'

#Copyright := `cat COPYRIGHT`

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

dcpamdircheck:
	@if [ ! X"`pwd`" = X"$(DCPAMDIR)" ]; then \
	    echo "" ; \
	    echo "    \$$DCPAMDIR in Config.mk is not conformed to " ; \
	    echo "    parent working directory \"`pwd`\"." ; \
	    echo "    You should rerun ./configure to build dcpam." ; \
	    echo "" ; \
	    exit 1; \
	fi

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

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

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

clean: clean.lib
	cd src; $(MAKE) $@
	#cd practice; $(MAKE) $@

clean.all: clean.lib
	cd src; $(MAKE) $@
	#cd practice; $(MAKE) $@
	-$(RMDIR) bin lib include

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

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

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
	-$(RM) -f config.cache config.log Config.mk
	-$(RM) -f autom4te.cache/*
	-$(RMDIR) autom4te.cache
	@echo ""
	@echo "    Removing Config.mk,"
	@echo "      you should rerun ./configure to build dcpam."
	@echo ""

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

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

tags:
	cd src; $(MAKE) $@

guide: dcpamdircheck
	@for file in $(GuideFiles) ; do \
	    echo $(CP) $${file} $${file}.rd ;\
	    $(CP) $${file} $${file}.rd ;\
	done
	$(MAKE) -f Makefile.rd2html
	$(RM) $(GuideRDFiles)
	cd doc ; $(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: dcpamdircheck $(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 -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) | sed -e 's/^\\chapter.*$$/\\Dchapterhead/' >> $@ || $(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 aclocal.m4 ]; then \
		echo -n "aclocal.m4 is not exists or not readable. "; \
		echo "This file needed for creating \"configure\"."; \
		exit 1; \
	fi
	/usr/bin/autoconf
	@echo "\"configure\" Generated."
