#---------------------------------------------------------------------
#     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
#---------------------------------------------------------------------
#= Generate HTML from RD
#
#  Please see http://www.gfd-dennou.org/library/dcmodel/doc/TEBIKI.dcmodel-rd-guide.htm
#
# * Yasuhiro MORIKAWA, Masaki ISHIWATARI
#   * 2007/01/01 (Yasuhiro MORIKAWA) RDOPTSJA and RDOPTSEN are added.
#   * 2006/11/08 (Yasuhiro MORIKAWA) RUBYLIB become valid.
#   * 2006/08/04 (Yasuhiro MORIKAWA) "RDHindexParser" is modified.
#   * 2006/06/22 (Yasuhiro MORIKAWA) "DEFAULTMSGJA", "DEFAULTMSGEN" are added.
#   * 2006/06/21 (Yasuhiro MORIKAWA) "HtmFileJA", "HtmFileEN" are added.
#   * 2006/06/20 (Yasuhiro MORIKAWA) "H1ToTitle" is modified
#   * 2006/06/04 (Yasuhiro MORIKAWA) Modified for XHTML 1.0 Transitional
#   * 2006/06/03 (Yasuhiro MORIKAWA) Modified for XHTML 1.0 Transitional
#   * 2006/03/19 (Yasuhiro MORIKAWA) Method of insert header, footer is modified
#   * 2006/03/19 (Yasuhiro MORIKAWA) "HEADERJA" and "HEADEREN" are added
#   * 2006/03/09 (Yasuhiro MORIKAWA) sample of "FOOTERJA" and "FOOTEREN" are modified
#   * 2006/03/08 (Yasuhiro MORIKAWA) "rd2html-ext-lib" can be used
#   * 2006/02/07 (Yasuhiro MORIKAWA) User parser is added
#   * 2006/01/04 (Yasuhiro MORIKAWA) Target "clean" and "clean.all" are modified
#   * 2006/01/02 (Yasuhiro MORIKAWA) Target "clean" and "clean.all" are modified
#   * 2005/11/06 (Yasuhiro MORIKAWA) Default CSS is modified to local site
#   * 2005/11/06 (Yasuhiro MORIKAWA) URL of reference of rdtool is updated
#   * 2005/10/12 (Yasuhiro MORIKAWA) JAPart and ENPart is modified
#   * 2005/10/12 (Yasuhiro MORIKAWA) FOOTEREN is modified
#   * 2005/10/10 (Yasuhiro MORIKAWA) default FOOTERs are modified
#   * 2005/08/14 (Yasuhiro MORIKAWA) Link to rd-hindex.rb page is added
#   * 2005/08/10 (Yasuhiro MORIKAWA) Ruby and RD Path checker is added
#   * 2005/08/10 (Yasuhiro MORIKAWA) RD Header Index inserter is added
#   * 2005/08/10 (Yasuhiro MORIKAWA) Date parser is response to multi-language
#   * 2005/08/10 (Yasuhiro MORIKAWA) Date parser is added
#   * 2005/07/07 (Yasuhiro MORIKAWA) "SUBDIRS" is deleted
#   * 2005/07/07 (Yasuhiro MORIKAWA) "chmod" and "chgrp" are added
#   * 2005/06/23 (Yasuhiro MORIKAWA) Suffix of JA file is change from "htm.ja" to "htm"
#   * 2005/06/23 (Yasuhiro MORIKAWA) Japanese commends are added
#   * 2005/06/12 (Yasuhiro MORIKAWA) Create
#
######################################################################
#
#== Settings
#
# [JAPANESE] eݒ荀

INCLUDEFILE = 
include $(INCLUDEFILE) # Include file              (optional)
                       # [JA] CN[ht@C (C)

JA      = 1        # 1: Create "htm",    0: Not Create     (selective)
EN      = 0        # 1: Create "htm.en", 0: Not Create     (selective)
                   # [JA] 1 Ȃ΍쐬, 0 Ȃ΍쐬Ȃ (IK{)

RDHINDEX = 2       # Index of Headers (selective)
		   #   0:None, 1-3:Header Level
		   #   Note: CSS item is "ol.rd2html_hindex [ul ..]"
		   #
		   # [JA] wb_̖ڎ (IK{)
		   #   0:쐬Ȃ, 1-3:wb_xɑΉ
		   #   l: CSS ݒ "ol.rd2html_hindex [ul ..]" 

HEADERJA  = "<div class=recipesheading> <a href=\"../index.htm\">-- GPhysVs --</a></div>"
#	     [ <a href=\"$(HtmFileEN)\">English</a> | Japanese ] \
		   # [JA] JA p HTML wb_[ (C)
		   #   $(HtmFileJA)  RD 琶{ HTML t@C
		   #   $(HtmFileEN)  RD 琶p HTML t@C

HEADEREN  = "<div class=recipesheading> <a href=\"../index.htm\">-- GPhys Recipes --</a></div>"
		   # HTML Header  (optional)
		   #   $(HtmFileJA) is Japanese HTML file generated from RD
		   #   $(HtmFileEN) is English HTML file generated from RD

FOOTERJA  = "<hr /> <small> \
             davis Group / GFD Dennou Staff  \
             <img src=\"/library/dcmodel/htmltools/dcstaff_email.png\" \
                  alt=\"dcstaff&\#64;&\#103;&\#102;&\#100;-dennou.org\" \
                  align=\"middle\" /> <br /> \
             "
#             Last Updated: $(UpdateDateJA), Since: $(CreateDateJA) </small>"
		   # [JA] JA p HTML tb^[ (C)
		   #   $(UpdateDateJA) ͓d] RD ŏꂽŏ㕔̓
		   #   (JA part )
		   #   $(CreateDateJA) ͓d] RD ŏꂽŉ̓
		   #   (JA part )

FOOTEREN  = "<hr /> <small> \
             dcmodel Development Group / GFD Dennou Staff  \
             <img src=\"/library/dcmodel/htmltools/dcstaff_email.png\" \
                  alt=\"dcstaff&\#64;&\#103;&\#102;&\#100;-dennou.org\" \
                  align=\"middle\" /> <br /> \
             "
#             Last Updated: $(UpdateDateEN) ($(UpdateUserEN)), Since: $(CreateDateEN) ($(CreateUserEN))</small>"
		   # HTML Footer  (optional)
		   #   $(UpdateDateEN) is date in Dennou RD on top (EN part)
		   #   $(CreateDateEN) is date in Dennou RD at bottom (EN part)

CSS     = style.css
		   # Cascade Style Sheet           (necessary)
		   # [JA] JXP[hX^CV[g (K{)

CHMOD   =       # Set mode          (optional)
		   # [JA] chmod l (C)

CHGRP   =          # Set group         (optional)
		   # [JA] chgrp l (C)

RUBY    = ruby1.8  # Path to Ruby       (necessary)
		   # [JA] Ruby ւ̃pX (K{)

RD      = rd2      # Path to rdtool       (necessary)
		   # [JA] rdtool ւ̃pX (K{)

#RD2LIB  = rd/rd2html-lib        # WCu
RD2LIB  = rd/rd2html-ext-lib    # gCu
		   # Library of rd2html      (selective)
		   # [JA] rd2html Cu (I)

RUBYLIB =
		   # Set "RUBYLIB"         (optional)
		   # [JA] "RUBYLIB" ̐ݒ (C)

#RDOPTS  =
RDOPTS  = --ref-extension --native-inline
		   # Additional rd2 options          (optional)
		   # [JA] rd2 R}hւ̃IvV (C)

RDOPTSJA  = --with-part=HTMLJA:html
		   # Additional rd2 options for JA         (optional)
		   # [JA] JA p rd2 R}hւ̃IvV (C)

RDOPTSEN  = --with-part=HTMLEN:html
		   # Additional rd2 options for EN         (optional)
		   # [JA] EN p rd2 R}hւ̃IvV (C)


OUTCODE = sjis     # character encoding of output [jis|euc|sjis] (necessary)
		   # [JA] rd o͎̕R[h  [jis|euc|sjis]  (K{)

JACODE  = Shift_JIS   # indicate CHARSET as charset [char encoding] (necessary)
		   # [JA] rd2 R}h CHARSET                 (K{)

DEFAULTMSGJA = "No Document"
		   # [JA] JA p ftHgbZ[W (C)
		   #   RD ɓeꍇɏo郁bZ[W.

DEFAULTMSGEN = "No Document"
		   # Default Messages  (optional)
		   #   Messages that are output when no content
                   #   is included in RD file.


# End Settings  [JA] ݒ荀ڂ܂
######################################################################
#== Commands
# General command
RD2HTM = rd2 -r $(RD2LIB) --with-css=$(CSS) --with-part=HTML:html \
		--out-code=$(OUTCODE) $(RDOPTS)
# For Japanese
RD2JA  = $(RD2HTM) --html-charset=$(JACODE) --with-part=JA:rd $(RDOPTSJA)
# For English
RD2EN  = $(RD2HTM) --html-charset=US-ASCII --with-part=EN:rd $(RDOPTSEN)

######################################################################
#== Filenames to which it is assumed to be used in HEADER**, FOOTER**
#
HtmFileJA = $(<:.rd=.htm)
HtmFileEN = $(<:.rd=.htm.en)


######################################################################
#== Ruby liner script for replacing title in HTML
#
# [JA]  (1 ̂) ɂƂ HTML t@C̈ԏ <h1> ̓e
#      ǂݎ, ̓e <title> ^Oɑ.
#
#      , <title>`</title> (AXL[܂މ\) 
#      <meta http-equiv="content-type" content="`"> w
#      OɊ܂܂ꍇ,  2 ւ.
#
H1ToTitle  = $(RUBY) -e 'body = open(ARGV[0]){|io| io.read}; \
			 if /\<h1.*?\>(.*?)\<\/h1.*?\>/ =~ body then      ;\
			   h1 = $$1 ;\
			   $$KCODE = "$(OUTCODE)".upcase ;\
			   h1 = h1.chomp.gsub!(/\<.*?\>/){|s| s = ""} ;\
			   mod = body.gsub(/\<title\>.*<\/title\>/,         \
			         "\<title\>" + h1 + "<\/title\>") ;\
			 else ;\
			   mod = body ;\
			 end ;\
			 \
			 if /(\<title\>.*?\<\/title\>)/im =~ mod then ;\
			   title = $$1 ;\
			   title_reg = Regexp.new(title.gsub(/\(/, "\\(").gsub(/\)/, "\\)"), 5) ;\
			   if /(\<meta\s+http-equiv=\"content-type\"\s+content=\".*?\"\s+\/\>)/im =~ mod then ;\
			     meta = $$1 ;\
			     meta_reg = Regexp.new(meta.gsub(/\(/, "\\(").gsub(/\)/, "\\)"), 5) ;\
			     mod.sub!(meta_reg, title) ;\
			     mod.sub!(title_reg, meta) ;\
			   end ;\
			 end ;\
			 \
			 out = open(ARGV[0], "w"); out.puts(mod)         ;\
			 out.close'

######################################################################
#== Ruby liner script for add header to existing HTML
#
# [JA] 1 ڂ̈ɂƂ HTML t@C (2 ߂̈)
#       <body> ^Oɑ}.
#
InsHeader  = $(RUBY) -e 'body = open(ARGV[1]){|io| io.read}; \
			 $$KCODE = "$(OUTCODE)".upcase ;\
			 mod = body.sub(/\<body\>/, \
			       "\<body\>\n\n" + ARGV[0].to_s) ;\
			 out = open(ARGV[1], "w"); out.puts(mod)         ;\
			 out.close'

######################################################################
#== Ruby liner script for add footer to existing HTML
#
# [JA] 1 ڂ̈ɂƂ HTML t@C (2 ߂̈)
#       <body> ^Oɑ}.
#
InsFooter  = $(RUBY) -e 'body = open(ARGV[1]){|io| io.read}; \
			 $$KCODE = "$(OUTCODE)".upcase ;\
			 mod = body.sub(/\<\/body\>/, \
			       ARGV[0].to_s + "\n\n\</body\>") ;\
			 out = open(ARGV[1], "w"); out.puts(mod)         ;\
			 out.close'

######################################################################
#== Ruby liner script for getting JA part
#
# [JA]  () ܂͈ꍇW͂瓾ꂽ
#      ̂, =begin, =end łꂽ =begin JA, =end JA
#      łꂽWo͂ɏo͂.
#
JAPart = $(RUBY) -e 'inbody = false ;\
		     puts "=begin" ;\
		     lines = readlines ;\
		     lines.each {|line| ;\
		       inbody = true  if /^=begin\s*$$/ =~ line ;\
		       inbody = true  if /^=begin\s+JA\s*$$/ =~ line ;\
		       inbody = false if /^=end\s*$$/ =~ line ;\
		       inbody = false if /^=end\s+JA\s*$$/ =~ line ;\
		       next if line =~ /^=(begin|end)\s*$$/ ;\
		       next if line =~ /^=(begin\s+|end\s+)\w+\s*$$/ ;\
		       if inbody  ;\
			 puts line ;\
		       end ;\
		     } ;\
		     puts "=end"'

######################################################################
#== Ruby liner script for getting EN part
#
# [JA]  () ܂͈ꍇW͂瓾ꂽ
#      ̂, =begin, =end łꂽ =begin EN, =end EN
#      łꂽWo͂ɏo͂.
#
ENPart = $(RUBY) -e 'inbody = false ;\
		     puts "=begin" ;\
		     lines = readlines ;\
		     lines.each {|line| ;\
		       inbody = true  if /^=begin\s*$$/ =~ line ;\
		       inbody = true  if /^=begin\s+EN\s*$$/ =~ line ;\
		       inbody = false if /^=end\s*$$/ =~ line ;\
		       inbody = false if /^=end\s+EN\s*$$/ =~ line ;\
		       next if line =~ /^=(begin|end)\s*$$/ ;\
		       next if line =~ /^=(begin\s+|end\s+)\w+\s*$$/ ;\
		       if inbody  ;\
			 puts line ;\
		       end ;\
		     } ;\
		     puts "=end"'


######################################################################
#== Ruby liner script for getting creation date in Dennou RD format
#
# [JA]  () ܂͈ꍇW͂瓾ꂽ
#      ̂, "* 2005/08/10 () ..." ̂悤ɋLqA
#      񂩂ԉ̓tWo͂ɏo͂.
#
CDateParser = $(RUBY) -e 'lines = readlines  ;\
			 start = false  ;\
			 date = "" ;\
			 lines.each{ |line|  ;\
			   if /^\s*\#*\s*\*\s+(\d{2,4}[\/\-]\d{1,2}[\/\-]\d{1,2})\s+\(.+\)/ =~ line then  ;\
			     start = true ;\
			     date = $$1 ;\
			   else ;\
			     if start then ;\
			       $$stdout.print date ;\
			       exit ;\
			     end ;\
			   end  ;\
			 }  ;\
			 $$stdout.print "unknown"  ;\
			 exit'

CreateDateJA = `$(JAPart) $< | $(CDateParser)`
CreateDateEN = `$(ENPart) $< | $(CDateParser)`

######################################################################
#== Ruby liner script for getting update date in Dennou RD format
#
# [JA]  () ܂͈ꍇW͂瓾ꂽ
#      ̂, "* 2005/08/10 () ..." ̂悤ɋLqA
#      񂩂ԏ̓tWo͂ɏo͂.

UDateParser = $(RUBY) -e 'lines = readlines ;\
			 lines.each{ |line|  ;\
			   if /^\s*\#*\s*\*\s+(\d{2,4}[\/\-]\d{1,2}[\/\-]\d{1,2})\s+\(.+\)/ =~ line then  ;\
			     $$stdout.print $$1  ;\
			     exit  ;\
			   end  ;\
			 }  ;\
			 $$stdout.print "unknown"  ;\
			 exit'

UpdateDateJA = `$(JAPart) $< | $(UDateParser)`
UpdateDateEN = `$(ENPart) $< | $(UDateParser)`


######################################################################
#== Ruby liner script for getting creator in Dennou RD format
#
# [JA]  () ܂͈ꍇW͂瓾ꂽ
#      ̂, "* 2005/08/10 () ..." ̂悤ɋLqA
#      񂩂ԉ̎Wo͂ɏo͂.
#
CUserParser = $(RUBY) -e 'lines = readlines  ;\
			 start = false  ;\
			 user = "" ;\
			 lines.each{ |line|  ;\
			   if /^\s*\#*\s*\*\s+(\d{2,4}[\/\-]\d{1,2}[\/\-]\d{1,2})\s+\((.+?)\)/ =~ line then  ;\
			     start = true ;\
			     user = $$2 ;\
			   else ;\
			     if start then ;\
			       $$stdout.print user ;\
			       exit ;\
			     end ;\
			   end  ;\
			 }  ;\
			 $$stdout.print "unknown"  ;\
			 exit'

CreateUserJA = `$(JAPart) $< | $(CUserParser)`
CreateUserEN = `$(ENPart) $< | $(CUserParser)`


######################################################################
#== Ruby liner script for getting updater in Dennou RD format
#
# [JA]  () ܂͈ꍇW͂瓾ꂽ
#      ̂, "* 2005/08/10 () ..." ̂悤ɋLqA
#      񂩂ԏ̎Wo͂ɏo͂.

UUserParser = $(RUBY) -e 'lines = readlines ;\
			 lines.each{ |line|  ;\
			   if /^\s*\#*\s*\*\s+(\d{2,4}[\/\-]\d{1,2}[\/\-]\d{1,2})\s+\((.+?)\)/ =~ line then  ;\
			     $$stdout.print $$2  ;\
			     exit  ;\
			   end  ;\
			 }  ;\
			 $$stdout.print "unknown"  ;\
			 exit'

UpdateUserJA = `$(JAPart) $< | $(UUserParser)`
UpdateUserEN = `$(ENPart) $< | $(UUserParser)`


######################################################################
#== Ruby liner script for getting RD Header index
#
# Reference: http://www.rubyist.net/~rubikitch/computer/rd-hindex/
#
# [JA] W͓ڎ擾, 1 ڂ̈t@CƂ
#      󂯎, ̃t@C == Oɖڎgݍ񂾓e
#      Wo͂ɏo͂.
#      2 ڂ͖̈ڎƂĎ荞ރwb_̃x̎wł.
#      1 ` 3 ܂łL, 0 ╶Ȃǂꂽꍇ,
#      ڎ͑gݍ܂Ȃ.
#      3 ڂ̈ part ̎w, { JA part ɂꍇ
#      JA ȂǂƎw肷.
#      Ȃ, ڎ ol ɂ =begin HTML <ol class="rd2html_hindex">
#      =end HTMLƂ^O.
#
#      Ql: http://www.rubyist.net/~rubikitch/computer/rd-hindex/
#

RDHindexParser = $(RUBY) -e \
		'rdlevel = ARGV[1].to_i ;\
		rdhindex = "=begin HTML\n<hr />\n<ol class=\"rd2html_hindex\">\n=end HTML\n" ;\
		rdhindex << "=begin\n" ;\
		while line = $$stdin.gets ;\
		  next if line =~ /^=(begin|end)$$/ ;\
		  if line =~ /^(=+)\s*(.+)$$/ ;\
		    level = $$1.length ; content = $$2 ;\
		    next if level == 1 || level > rdlevel + 1 ;\
		    rdhindex << "  " * (level - 2) + "* ((<\"\#{content}\">))\n" ;\
		  end ;\
		end ;\
		rdhindex << "=end\n" ;\
		rdhindex << "=begin HTML\n</ol>\n<hr />\n=end HTML\n" ;\
		rdhindex = "" if rdlevel == 0 ;\
		 ;\
		body = open(ARGV[0]){|io| io.read} ;\
		lines = body.split(/\n/) ;\
		finished = false ; inbody = false ;\
		results = "" ; rdbegin = "" ; rdend = "" ;\
		lines.each{ |line| ;\
		  if line =~ /^=begin\s*$$/ ;\
		    rdbegin = "=begin" ; rdend   = "=end" ;\
		  elsif line =~ /^=begin\s+(.*)/ ;\
		    rdbegin = "=begin " + $$1 ; rdend = "=end " + $$1 ;\
		  end ;\
		  if line =~ /^=end$$/ || line =~ /^=end\s+(.*)/ ;\
		    rdbegin = "" ; rdend   = "" ;\
		  end ;\
		  inbody = true if line =~ /^=begin\s*$$/ || line =~ /^=begin\s+\#{ARGV[2]}$$/ ;\
		  inbody = false if line =~ /^=end(\s+\w+)*$$/ ;\
		  if /^==\s+/ =~ line && !(finished) && inbody then ;\
		    results << rdend + "\n" + rdhindex + "\n" + rdbegin + "\n" ;\
		    results << line + "\n" ;\
		    finished = true ;\
		  else ;\
		    results << line + "\n" ;\
		  end ;\
		  } ;\
		puts results'


######################################################################
#== Postprocessor for RDHindexParser
#
# [JA] RDHindexParser  XHTML 1.0 Transitional ᔽƂȂĂ܂
#      C㏈XNvg.
#      W͂ HTML 󂯎, ᔽڂu,
#      ̌ʂWo͂֕Ԃ.

RDHindexPostProcessor = $(RUBY) -e \
		'lines = readlines ;\
		$$KCODE = "$(OUTCODE)".upcase ;\
		exit! if lines.empty? ;\
		body = lines.join ;\
		body.gsub!(/(\<ol class=\"rd2html_hindex\"\>)\s*\<ul\>/im, "\\1" ) ;\
		body.gsub!(/\<\/ul\>\s*\<\/ol\>/im, "\</ol\>") ;\
		puts body'

######################################################################
#== Search "rd" files
__AllRDFiles__ = $(wildcard *.rd)
RDFiles = $(__AllRDFiles__:%.v.rd=)
GenerateFiles = $(HtmByJA) $(HtmByEN)
HtmByJA = $(RDFiles:.rd=.htm)
HtmByEN = $(RDFiles:.rd=.htm.en)

##########################################################################
#== Rules
#
all: pathcheck $(GenerateFiles) index.htm index.en.htm


help:
	@echo ""
	@echo "  This Makefile needs following programs."
	@echo ""
	@echo "      Ruby   <URL:http://www.ruby-lang.org>"
	@echo "      rdtool <URL:http://raa.ruby-lang.org/project/rdtool/>"
	@echo ""
	@echo ""
	@echo "  make [all] :"
	@echo "             Create HTML files from RD "
	@echo ""


%.htm: %.rd
	@if [ $(JA) != "0" ] ; then \
	    echo -n "Insert RD Header Index Level:$(RDHINDEX) Part:JA .." ;\
	    $(JAPart) $< | $(RDHindexParser) $< "$(RDHINDEX)" JA > $<.rdhindex.tmp ;\
	    echo ' done' ;\
	    echo "$(RD2JA) $< > $@ || rm -f $@" ;\
	    RUBYLIB=$(RUBYLIB) $(RD2JA) $<.rdhindex.tmp | $(RDHindexPostProcessor) > $@ || rm -f $@ ;\
	    rm -f $<.rdhindex.tmp ;\
	    if [ ! -f $@ ] && [ ! X$(DEFAULTMSGJA) = X"" ]; then \
		echo -e "=begin JA\n$(DEFAULTMSGJA)\n=end" | $(RD2JA) > $@ || rm -f $@ ;\
	    fi ;\
	    if [ -f $@ ]; then \
		echo -n 'Copy <h1> to <title> ..' ; $(H1ToTitle) $@  ; echo 'done' ;\
		if [ ! X$(HEADERJA) = X"" ] ; then \
		    echo -n 'Add header ..' ;\
		    $(InsHeader) $(HEADERJA) $@ ;\
		    echo ' done' ;\
		fi ;\
		if [ ! X$(FOOTERJA) = X"" ] ; then \
		    echo -n 'Add footer ..' ;\
		    $(InsFooter) $(FOOTERJA) $@ ;\
		    echo ' done' ;\
		fi ;\
	    fi ;\
	    if [ -f $@ ] && [ ! X$(CHMOD) = X"" ]; then \
		echo -n chmod $(CHMOD) $@ .. ; chmod $(CHMOD) $@  ; echo ' done' ;\
	    fi ;\
	    if [ -f $@ ] && [ ! X$(CHGRP) = X"" ]; then \
		echo -n chgrp $(CHGRP) $@ .. ; chgrp $(CHGRP) $@  ; echo ' done' ;\
	    fi ;\
	fi


%.htm.en: %.rd
	@if [ $(EN) != "0" ] ; then \
	    echo -n "Insert RD Header Index Level:$(RDHINDEX) Part:EN .." ;\
	    $(ENPart) $< | $(RDHindexParser) $< "$(RDHINDEX)" EN > $<.rdhindex.tmp ;\
	    echo ' done' ;\
	    echo "$(RD2EN) $< > $@ || rm -f $@" ;\
	    RUBYLIB=$(RUBYLIB) $(RD2EN) $<.rdhindex.tmp | $(RDHindexPostProcessor) > $@ || rm -f $@ ;\
	    rm -f $<.rdhindex.tmp ;\
	    if [ ! -f $@ ] && [ ! X$(DEFAULTMSGEN) = X"" ]; then \
		echo -e "=begin EN\n$(DEFAULTMSGEN)\n=end" | $(RD2EN) > $@ || rm -f $@ ;\
	    fi ;\
	    if [ -f $@ ]; then \
		echo -n 'Copy <h1> to <title> ..' ; $(H1ToTitle) $@  ; echo ' done' ;\
		if [ ! X$(HEADEREN) = X"" ] ; then \
		    echo -n 'Add header ..' ;\
		    $(InsHeader) $(HEADEREN) $@ ;\
		    echo ' done' ;\
		fi ;\
		if [ ! X$(FOOTEREN) = X"" ] ; then \
		    echo -n 'Add footer ..' ;\
		    $(InsFooter) $(FOOTEREN) $@ ;\
		    echo ' done' ;\
		fi ;\
	    fi ;\
	    if [ -f $@ ] && [ ! X$(CHMOD) = X"" ]; then \
		echo -n chmod $(CHMOD) $@ .. ; chmod $(CHMOD) $@  ; echo ' done' ;\
	    fi ;\
	    if [ -f $@ ] && [ ! X$(CHGRP) = X"" ]; then \
		echo -n chgrp $(CHGRP) $@ .. ; chgrp $(CHGRP) $@  ; echo ' done' ;\
	    fi ;\
	fi

clean:
	@if [ ! X"$(wildcard *~ *.bak)" = X"" ]; then \
		echo rm $(wildcard *~ *.bak) ;\
		rm $(wildcard *~ *.bak) ;\
	fi

clean.all: clean
	@if [ ! X"$(wildcard $(GenerateFiles))" = X"" ]; then \
		echo rm $(wildcard $(GenerateFiles)) ;\
		rm $(wildcard $(GenerateFiles)) ;\
	fi

pathcheck:
	@echo -n "Ruby path check .. "
	@if ! which $(RUBY) > /dev/null 2>&1 ; then \
	    echo "" ;\
	    echo "   Error: RUBY=\"$(RUBY)\" is invalid path." ;\
	    echo "" ;\
	    echo "   Please install Ruby (see http://www.ruby-lang.org), " ;\
	    echo "   or set valid path." ;\
	    echo "" ;\
	    exit 1 ;\
	fi
	@echo " done"

	@echo -n "RD path check .. "
	@if ! which $(RD) > /dev/null 2>&1 ; then \
	    echo "" ;\
	    echo "   Error: RD=\"$(RD)\" is invalid path." ;\
	    echo "" ;\
	    echo "   Please install RDtool (see http://raa.ruby-lang.org/project/rdtool/), " ;\
	    echo "   or set valid path." ;\
	    echo "" ;\
	    exit 1 ;\
	fi
	@echo " done"

######################################################################
#== Mode setting for Emacs
#Local Variables:
#mode: makefile
#End:
#
