#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

INSTDIR=$(CURDIR)/debian/chktex

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
TMPCFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

CFLAGS = -Wall -fstack-protector --param=ssp-buffer-size=4 -Wformat $(CPPFLAGS)
INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755

#ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#	CFLAGS += -O0
#else
#	CFLAGS += -O2
#endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

config config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	echo $(LDFLAGS)
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr --mandir=\$${prefix}/share/man --sysconfdir=/etc \
		--enable-emacs-hack=\$${prefix}/share/emacs/site-lisp/ 

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp:  config.status
	dh_testdir

	# Add here commands to compile the package.
	# build source
	$(MAKE) CFLAGS="$(CFLAGS) $(CXXFLAGS)" LDFLAGS="$(LDFLAGS) -lpcreposix -lpcre" chktex ChkTeX.dvi
	# build html documentation
	$(MAKE) html

	touch build-stamp

clean: 
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || [ ! -f config.status ] || $(MAKE) clean
	-rm -f ChkTeX.dvi ChkTeX.tex ChkTeX.readme config.status stamp-h
	-rm -f config.h Makefile Readme.header
	-rm -f deweb rmeheader
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/chktex.
	$(MAKE) install DESTDIR=$(INSTDIR)

	mkdir -p $(INSTDIR)/usr/share/doc/chktex/html/
	cp $(CURDIR)/HTML/ChkTeX/*.html $(CURDIR)/HTML/ChkTeX/*.css $(INSTDIR)/usr/share/doc/chktex/html/

	cp chktex.1 chkweb.1 $(INSTDIR)/usr/share/man/man1/


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
	dh_installchangelogs NEWS
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install 

get-orig-source:
	mkdir -p ../tarballs
	uscan --verbose --force-download --destdir=../tarballs

