# -*- makefile -*-
# $Copyleft: (c) 2001 Nobuyoshi Nakada <nobu.nokada@softhome.net> $
# $Id: Makerules,v 1.5 2001/04/13 06:40:58 nobu Exp $

RD2 = rd2
RMI2HTML = rmi2html
RD2RDO = rd2rdo
RD2HTML = rd2html
RD2MAN = rd2man
RD2RMI = rd2rmi
RD2RDOFLAGS =
CSS = rd.css
PARTS = example:rb
RD2FLAGS = $($*-RD2FLAGS)
RD2HTMLFLAGS = $(filter-out %=,--with-css=$(CSS) --with-part=$(PARTS) \
	--html-charset=$(CHARSET) --out-code=$(CHARSET)) \
	$($(basename $*)-RD2HTMLFLAGS) \
	$(if $(suffix $*),$($(suffix $*)-RD2HTMLFLAGS))
RD2MANFLAGS = $(filter-out %=,--out-code=$(CHARSET)) $($*-RD2MANFLAGS)
CHARSET = $(CHARSET-$(subst .,,$(suffix $*)))
RLOG2RDFLAGS = $(if $(wildcard $<,v $(<D)/RCS/$(<F),v),-zLT,--cvs) -2 -h='= ChangeLog of $<'
MANIFESTFLAG = -m $(firstword $(wildcard MANIFEST) $<)
REVISIONFLAG = -f $<
MKTARFLAGS = $(MANIFESTFLAG) $(REVISIONFLAG)
ARCHIVE = $(HOME)/public_html/archive

SRCS = $(PKG:=.rb)
DOCS = $(SRCS) $(wildcard $(SRCS:.rb=.*.rd))
RMIS = $(SRCS:.rb=.rmi)

CHARSET-ja_JP.ujis = euc-jp
CHARSET-ja_JP.sjis = shift_jis
CHARSET-ja_JP = $(CHARSET-ja_JP.ujis)
CHARSET-ja = $(CHARSET-ja_JP)
.ja-RD2HTMLFLAGS = -rrd/jfold

all: help

clean::
	rm -f *.rdo *.rmi
	rm -f $(subst .rb,.html,$(DOCS:.rd=.html))
	rm -f $(subst .rb,.1,$(DOCS:.rd=.1))
ifdef CHANGELOG
	rm -f $(CHANGELOG:=.*)
endif
ifdef RMI
	rm -f $(RMI:=.*)
endif

doc: html man
html: $(DOCS:.rb=.html) $(DOCS:.rd=.html)
ifdef CHANGELOG
html: $(CHANGELOG:=.html)
endif
ifdef RMI
html: $(RMI:=.html)
endif
man: $(DOCS:.rb=.1) $(DOCS:.rd=.1)
dist: $(PKG).tarball
install::
	ruby -rftools -rrbconfig -e 'ARGV.each{|f|File.install(f,Config::CONFIG["sitedir"])}' $(SRCS)

help::
	@echo 'Available targets:'
	@echo '  clean: 	clean work files.'
	@echo '  doc: 		format documents.'
	@echo '  +-html: 	HTML format only.'
	@echo '  +-man: 	MAN format only.'
	@echo '  dist:		make tarball.'
	@echo '  install: 	install file.'

%.tarball: %.rb %.rlog
	mktarball $(MKTARFLAGS) -o $(ARCHIVE) $(<:.rb=) $< $(<:.rb=.rlog)

%.rdo: %.rd
	$(RD2RDO) $(RD2FLAGS) $(RD2RDOFLAGS) -o $* $<
%.rmi: %.rb
	$(RD2RMI) $(RD2FLAGS) -o $* $<
%.html: %.rdo
	$(RD2HTML) $(RD2FLAGS) $(RD2HTMLFLAGS) --from-rdo -o $* $<
%.1: %.rdo
	$(RD2MAN) $(RD2FLAGS) $(RD2MANFLAGS) --from-rdo -o $* $<
%.html: %.rd
	$(RD2HTML) $(RD2FLAGS) $(RD2HTMLFLAGS) -o $* $<
%.1: %.rd
	$(RD2MAN) $(RD2FLAGS) $(RD2MANFLAGS) -o $* $<
%.html: %.rb
	$(RD2HTML) $(RD2FLAGS) $(RD2HTMLFLAGS) -o $* $<
%.1: %.rb
	$(RD2MAN) $(RD2FLAGS) $(RD2MANFLAGS) -o $* $<
%.rlog: %.rb
	rlog2rd $(RLOG2RDFLAGS) $< > $@

ifdef RMI
$(RMI:=.html): $(RMIS)
	$(RMI2HTML) $(RMIS) > $@
endif
ifdef CHANGELOG
$(CHANGELOG:=.html): $(SRCS:.rb=.rlog) $(RMIS)
	$(RD2HTML) $(RD2FLAGS) $(RD2HTMLFLAGS) $(addprefix --with-rmi=,$(RMIS)) -o $(@:.html=) $<
else
%.rd: %.rb
	{ sed -n -e '/^=begin/,/^=end/p' $<; rlog2rd $<;} > $@
endif

%.rd.en: %.rb
	sed -n -e '1i\' -e '# -*- rd -*-' -e '/^=begin/,/^=end/p' $< > $@

.DELETE_ON_ERROR:
