#
#	Makefile for the zh-sgmltools package: ripped straight from ipchains.
#	(C) 1998-1999 All rights reserved.
#       EMail complaints to Chih-Wei Huang <cwhuang@linux.org.tw>
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#       MA 02111-1307 USA
#

VERSION	= $(shell cat VERSION)
RELEASE	= $(shell cat RELEASE)
CC	= c++
COPTS   = -O2
CFLAGS	= -Wall -Wunused $(COPTS)
LIB	= -lstdc++
BIN	= /usr/local/bin
MANDIR	= /usr/local/man
INSTALL = install
SCRIPT	= bg5sgmlcheck bg5sgml2html bg5sgml2latex bg5sgml2txt bg5sgml2lyx \
	  gbsgmlcheck gbsgml2html gbsgml2latex gbsgml2txt gbsgml2lyx

.SUFFIXES: .html .txt .tex .lyx .sgml
.sgml.html:
	@PATH=.:$(PATH) ./bg5sgml2html $<
.sgml.txt:
	./bg5sgml2txt $<
.sgml.tex:
	@PATH=.:$(PATH) ./bg5sgml2latex $< --clean \
		--output=tex --output=dvi --output=ps
.sgml.lyx:
	@PATH=.:$(PATH) ./bg5sgml2lyx $< 


all:	mb2a script readme.html readme.txt readme.tex readme.lyx

mb2a:	mb2a.cc
	$(CC) $(CFLAGS) $(LIB) -o mb2a mb2a.cc

script:
	@for i in $(SCRIPT); do [ -f $$i ] || ln zhsgmltools $$i ; done
	@touch script

version:
	@echo This is version $(VERSION), release $(RELEASE).

install:	uninstall
	$(INSTALL) -s -m 0755 -o 0 -g 0 mb2a $(BIN)
	$(INSTALL) -m 0755 -o 0 -g 0 zhsgmltools $(BIN)
	for i in $(SCRIPT); \
		do ln $(BIN)/zhsgmltools $(BIN)/$$i ; \
	done
	@rm -f $(BIN)/zhsgmltools

uninstall:
	for i in $(SCRIPT) zhsgmltools mb2a; do rm -f $(BIN)/$$i; done

doc:	mb2a script
	@PATH=.:$(PATH) ./bg5sgml2html readme
	@if [ -n `type -path lynx` ]; then \
		PATH=.:$(PATH) ./bg5sgml2txt readme ; \
	fi
	@if [ -n `type -path chilatex` ]; then \
		PATH=.:$(PATH) ./bg5sgml2latex readme --clean \
				--output=tex --output=dvi --output=ps ; \
	fi
	@PATH=.:$(PATH) ./bg5sgml2lyx readme

clean:
	rm -f mb2a *.o a.out core tmp@
	rm -rf zh-sgmltools-*
	rm -f *.htm *.html *.tex *.dvi *.ps *.aux *.toc *.log *.txt
	rm -f *.info *.rtf *.man *.lyx
	rm -f $(SCRIPT) script

pack:	clean
	FILE=`ls *`; mkdir zh-sgmltools-$(VERSION)-$(RELEASE); \
	cp -pf $$FILE zh-sgmltools-$(VERSION)-$(RELEASE)
	tar cf - zh-sgmltools-$(VERSION)-$(RELEASE) | \
	gzip -9 > ../zh-sgmltools-$(VERSION)-$(RELEASE).tar.gz
	rm -rf zh-sgmltools-$(VERSION)-$(RELEASE)

buildrpm:
	@echo "%define version $(VERSION)" >> \
		/usr/src/redhat/SPECS/zh-sgmltools.spec
	@echo "%define release $(RELEASE)" >> \
		/usr/src/redhat/SPECS/zh-sgmltools.spec
	@cat zh-sgmltools.spec >> /usr/src/redhat/SPECS/zh-sgmltools.spec
	rpm -ba /usr/src/redhat/SPECS/zh-sgmltools.spec

rpm2:
	@cp -f ../zh-sgmltools-$(VERSION)-$(RELEASE).tar.gz \
		/usr/src/redhat/SOURCES
	@echo '%define packager Chih-Wei Huang <cwhuang@linux.org.tw>' > \
		/usr/src/redhat/SPECS/zh-sgmltools.spec
	@echo '%define distribution Chinese Linux Extension' >> \
		/usr/src/redhat/SPECS/zh-sgmltools.spec

rpm:	pack rpm2 buildrpm

rhcn2:
	@cp -f ../zh-sgmltools-$(VERSION)-$(RELEASE).tar.gz \
		/usr/src/redhat/SOURCES
	@echo '%define packager Red Hat Contrib|Net <rhcn-bugs@redhat.com>' > \
		/usr/src/redhat/SPECS/zh-sgmltools.spec
	@echo '%define distribution Red Hat Contrib|Net' >> \
		/usr/src/redhat/SPECS/zh-sgmltools.spec

rhcn:	pack rhcn2 buildrpm

