#!/usr/bin/make -f

# set the dpkg-architecture vars
export DEB_BUILD_ARCH      := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
export DEB_BUILD_GNU_CPU   := $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
export DEB_BUILD_GNU_SYSTEM:= $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)
export DEB_BUILD_GNU_TYPE  := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_ARCH       := $(shell dpkg-architecture -qDEB_HOST_ARCH)
export DEB_HOST_GNU_CPU    := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
export DEB_HOST_GNU_SYSTEM := $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
export DEB_HOST_GNU_TYPE   := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)


#
# 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
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
TEXPATH     := $(PREFIX)/share/texmf/tex/latex/html

# 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
MANDIR         := $(TMPTOP)/usr/share/man/man1

CONVERT_CMD:= '../latex2html -test_mode -no_math -html_version 4.0,math,frame -accent_images textrm -scalable_fonts'
all build: stamp-build
stamp-build:
	$(checkdir)
	cd docs && ln -sf ../texinputs/html*.sty .
	-test -f stamp-configure || $(MAKE) -f debian/rules configure
	$(MAKE)
	mkdir -p docs/manual
	$(MAKE) -C docs  -f Makefile.frames \
	      L2H=$(CONVERT_CMD) \
	      LATEX2HTMLDIR=$(SRCTOP) manual.dvi;
	$(MAKE) -C docs  -f Makefile.frames \
	      L2H=$(CONVERT_CMD) \
	      LATEX2HTMLDIR=$(SRCTOP) manual.ps;
	$(MAKE) -C docs  -f Makefile.frames \
              L2H=$(CONVERT_CMD) \
	      LATEX2HTMLDIR=$(SRCTOP) manual.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) \
	            --shlibdir=$(LIBDIR) \
                    --libdir=$(LIBDIR) \
                    --with-perl=$(PERL) \
                    --with-iconpath=$(ICONPATH) \
		    --with-texpath=$(TEXPATH) \
                     && touch stamp-configure

clean:
	$(checkdir)
	rm -f  $(FILES_TO_CLEAN) $(STAMPS_TO_CLEAN) docs/html*.sty
	rm -rf $(DIRS_TO_CLEAN)
	[ ! -f docs/Makefile ] || $(MAKE) -C docs clean #htmlclean
	[ ! -f Makefile ] || $(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 .log

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)	$(DOCBASE)
	$(make_directory)	$(TMPTOP)/DEBIAN
	$(make_directory)	$(TMPTOP)/etc
	$(install_file)		debian/changelog     $(DOCDIR)/changelog.Debian
	$(install_file)         debian/buildinfo     $(DOCDIR)/buildinfo.Debian
	$(MAKE)                 install DESTDIR=$(TMPTOP) INSTALLING_DEBIAN=1
	#(cd $(TMPTOP)/$(LIBDIR)/docs; $(MAKE) clean htmlclean)
	(cd $(TMPTOP)/$(LIBDIR); \
            mv l2hconf.pm       $(TMPTOP)/etc/latex2html.conf; \
            ln -s               /etc/latex2html.conf l2hconf.pm )
	$(install_file)         dot.latex2html-init    $(DOCDIR)/
	$(install_file)         Changes    	       $(DOCDIR)/
	$(install_file)         docs/manual.dvi        $(DOCDIR)/
	$(install_file)         docs/manual.ps         $(DOCDIR)/
	$(install_file)         debian/README.foilhtml.Debian $(DOCDIR)/
	$(install_file)         debian/README.Debian $(DOCDIR)/
	$(install_file)         latex2html.1           $(MANDIR)
	$(install_file)         texexpand.1            $(MANDIR)
	pod2man pstoimg         > $(MANDIR)/pstoimg.1
	rm $(TMPTOP)/$(TEXPATH)/url.sty
	-gzip -9fvr		$(DOCDIR)
	-gzip -9fvr		$(MANDIR)
	(cd $(DOCDIR);          ln -s Changes.gz       changelog.gz;)
	$(install_file)         debian/copyright       $(DOCDIR)/copyright
	$(make_directory)	$(TMPTOP)/usr/share/lintian/overrides
	$(install_file)		debian/source.lintian-overrides $(TMPTOP)/usr/share/lintian/overrides/latex2html
	(cd docs/manual/;       tar cf - *.html *.png  *.css) | \
                                (cd $(DOCDIR)/html; tar xf   -)
	(cd $(TMPTOP)/$(LIBDIR); rm -rf docs;)
	chmod ugo+x 		$(TMPTOP)/$(LIBDIR)/makemap
	chmod ugo+x 		$(TMPTOP)/$(LIBDIR)/makeseg/makeseg
#	mkdir -p $(TMPTOP)/usr/share/texmf/tex/latex
#	cd $(TMPTOP)/usr/share/texmf/tex/latex && \
#	  ln -s ../../../latex2html/texinputs latex2html
	patch $(TMPTOP)/usr/bin/latex2html debian/latex2html.patch
	$(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
