#!/usr/bin/make -f
############################ -*- Mode: Makefile -*- ###########################
## rules ---
## Author	    : Manoj Srivastava ( srivasta@tiamat.datasync.com )
## Created On	    : Mon Apr  7 22:14:47 1997
## Created On Node  : tiamat.datasync.com
## Last Modified By : Manoj Srivastava
## Last Modified On : Tue Feb  8 19:33:30 2000
## Last Machine Used: glaurung.green-gryphon.com
## Update Count	    : 144
## Status	    : Unknown, Use with caution!
## HISTORY	    :
## Description	    :
## $Id: rules,v 1.29 2000/02/10 00:59:36 srivasta Exp $
###############################################################################

#
# VERSION=$(shell LC_ALL=C dpkg-parsechangelog | grep ^Version: | \
#                          sed 's/^Version: *//')
#

FILES_TO_CLEAN = debian/files debian/buildinfo debian/substvars        \
                 latex2html.bak

STAMPS_TO_CLEAN = stamp-configure stamp-build stamp-binary
DIRS_TO_CLEAN   = debian/tmp  docs/manual
thisdir         =$(shell pwd)

install_file   = /usr/bin/install -p    -o root -g root  -m 644
install_program= /usr/bin/install -p    -o root -g root  -m 755
install_game   = /usr/bin/install -p    -o root -g games -m 2755
make_directory = /usr/bin/install -p -d -o root -g root  -m 755

package = $(shell grep Source debian/control | sed 's/^Source: //')

CC          := gcc
CFLAGS      := -O2
LDFLAGS     := -s
PERL        := /usr/bin/perl
PREFIX      := /usr
BINDIR      := $(PREFIX)/bin
LIBDIR      := $(PREFIX)/share/latex2html
ICONPATH    := $(PREFIX)/share/latex2html/icons
ICONSTORAGE := /var/www/$(ICONPATH)

# Location of the source dir
SRCTOP := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi;)
TMPTOP         := $(SRCTOP)/debian/tmp
DOCDIR 	       := $(TMPTOP)/usr/share/doc/$(package)
DOCBASE        := $(TMPTOP)/usr/share/doc-base
MENUDIR        := $(TMPTOP)/usr/lib/menu/
MANDIR         := $(TMPTOP)/usr/share/man/man1
OLDSHADOWDIR   := $(TMPTOP)/var/www/usr/lib
SHADOW_DIR     := $(TMPTOP)/var/www/usr/share/$(package)/
SHADOW_ICONDIR := $(TMPTOP)/var/www/usr/share/$(package)/icons

all build: stamp-build
stamp-build:
	$(checkdir)
	-test -f stamp-configure || $(MAKE) -f debian/rules configure
	$(MAKE)
	mkdir -p docs/manual
	$(MAKE) -C docs \
              L2H='../latex2html -test_mode -no_math -html_version 3.2,math' \
	      LATEX2HTMLDIR=$(SRCTOP) manual.dvi
	$(MAKE) -C docs \
              L2H='../latex2html -test_mode -no_math -html_version 3.2,math' \
	      LATEX2HTMLDIR=$(SRCTOP) manual.ps
	$(MAKE) -C docs \
              L2H='../latex2html -test_mode -no_math -html_version 3.2,math' \
	      LATEX2HTMLDIR=$(SRCTOP) html
	dpkg -l gcc perl dpkg 'libc*' binutils ldso make dpkg-dev |\
         awk '$$1 == "ii" { printf("%s-%s\n", $$2, $$3) }' > \
          debian/buildinfo
	touch stamp-build

configure: stamp-configure

stamp-configure:
	$(checkdir)
	./configure --verbose --prefix=$(PREFIX) \
                    --libdir=$(LIBDIR) \
                    --with-perl=$(PERL) \
                    --with-iconpath=$(ICONPATH) \
                    --with-iconstorage=$(ICONSTORAGE) \
                     && touch stamp-configure

clean:
	$(checkdir)
	rm -f  $(FILES_TO_CLEAN) $(STAMPS_TO_CLEAN)
	rm -rf $(DIRS_TO_CLEAN)
	-$(MAKE) -C docs clean htmlclean
	-$(MAKE) distclean
	rm -f core `find . \( -name '*.orig' -o -name '*.rej' -o \
	  -name '*~' -o -name '*.bak' -o -name '#*#' -o \
	  -name '.*.orig' -o -name '.*.rej' -o -name '.SUMS' -o \
	  -size 0 \) -print` TAGS

binary-indep:	build stamp-binary

binary-arch: build

stamp-binary:
	@test 0 = $$(id | sed -e 's/(.*$$//' -e 's/^uid=//') || \
		(echo need root priviledges; exit 1)
	$(checkdir)
	test -f stamp-build ||	$(MAKE) -f debian/rules build
	-rm -rf			$(TMPTOP)
	$(make_directory)	$(DOCDIR)/html
	$(make_directory)       $(MANDIR)
	$(make_directory)	$(MENUDIR)
	$(make_directory)	$(DOCBASE)
	$(make_directory)       $(SHADOW_ICONDIR)
	$(make_directory)       $(OLDSHADOWDIR)
	$(make_directory)	$(TMPTOP)/DEBIAN
	$(make_directory)	$(TMPTOP)/etc
	$(install_file)		debian/changelog     $(DOCDIR)/changelog.Debian
	$(install_file)		debian/README.debian $(DOCDIR)/README.debian
	$(install_file)         debian/buildinfo     $(DOCDIR)/buildinfo.Debian
	$(MAKE)                 INSTALLING_DEBIAN=1 install
	(cd $(OLDSHADOWDIR);    ln -s ../share/$(package) .)
	(cd $(TMPTOP)/$(LIBDIR)/docs; $(MAKE) clean htmlclean)
	(cd $(TMPTOP)/$(LIBDIR); \
            mv l2hconf.pm       $(TMPTOP)/etc/latex2html.config; \
            ln -s               /etc/latex2html.config l2hconf.pm )
	(cd $(SHADOW_ICONDIR);  rm -f *; touch .deleteme)
	$(install_file)         dot.latex2html-init    $(DOCDIR)/
	$(install_file)         Changes    	       $(DOCDIR)/
	$(install_file)         docs/manual.dvi        $(DOCDIR)/
	$(install_file)         docs/manual.ps         $(DOCDIR)/
	$(install_file)         latex2html.1           $(MANDIR)
	$(install_file)         texexpand.1            $(MANDIR)
	pod2man pstoimg         > $(MANDIR)/pstoimg.1
	gzip -9fqr		$(DOCDIR)
	gzip -9fqr		$(MANDIR)
	(cd $(DOCDIR);          ln -s Changes.gz       changelog.gz;)
	$(install_file)         debian/copyright       $(DOCDIR)/copyright
	(cd docs/manual/;       tar cf - *.html *.png  *.css) | \
                                (cd $(DOCDIR)/html; tar xf   -)
	(cd $(TMPTOP)/$(LIBDIR); rm -rf docs;)
	chmod ugo+x 		$(TMPTOP)/$(LIBDIR)/cweb2html/cweb2html
	chmod ugo+x 		$(TMPTOP)/$(LIBDIR)/cweb2html/makemake.pl
	chmod ugo+x 		$(TMPTOP)/$(LIBDIR)/makemap
	chmod ugo+x 		$(TMPTOP)/$(LIBDIR)/makeseg/makeseg
	$(make_directory)       $(TMPTOP)/usr/lib/menu
	$(install_file)         debian/menuentry       $(MENUDIR)/$(package)
	$(install_file)         debian/docentry        $(DOCBASE)/$(package)
	$(install_file)		debian/conffiles       $(TMPTOP)/DEBIAN/conffiles
	$(install_program)      debian/postinst        $(TMPTOP)/DEBIAN/postinst
	$(install_program)      debian/postrm          $(TMPTOP)/DEBIAN/postrm
	$(install_program)      debian/prerm           $(TMPTOP)/DEBIAN/prerm
	$(install_program)      debian/preinst         $(TMPTOP)/DEBIAN/preinst
	dpkg-gencontrol -isp
	chown -R root.root	$(TMPTOP)
	chmod -R go=rX		$(TMPTOP)
	dpkg --build		$(TMPTOP) ..
	touch stamp-binary

define checkdir
	test -f debian/rules -a -f latex2html.pin
endef

# Below here is fairly generic really

binary:		binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

.PHONY: binary binary-arch binary-indep clean checkroot
