#!/usr/bin/make -f
# $Id: rules,v 1.8 2022/01/08 19:53:07 tom Exp $
#
# 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)

PACKAGES.arch  = byacc byacc2

QA_FIX		= DEB_BUILD_MAINT_OPTIONS="hardening=+all qa=+bug reproducible=+all"

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

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

BYACC_DIR = $(CURDIR)/debian/byacc
BTYACC_DIR = $(CURDIR)/debian/byacc2

BYACC_TMP = t/byacc
BTYACC_TMP = t/byacc2

verbose = # -v

configure = \
	CPPFLAGS="$(CPPFLAGS)" \
	CFLAGS="$(CFLAGS)" \
	LDFLAGS="$(LDFLAGS)" \
	../../configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--sysconfdir=/etc \
		--enable-stdnoreturn \
		--program-transform-name='s,^yacc,byacc,'

all: build

config: config-arch
build: build-indep build-arch
install: install-arch
binary: binary-indep binary-arch

config-arch:  $(PACKAGES.arch:%=config-%-stamp)
build-arch:   $(PACKAGES.arch:%=build-%-stamp)
install-arch: $(PACKAGES.arch:%=install-%-stamp)

build-indep:

config-byacc-stamp:
	dh_testdir

	rm -rf $(BYACC_TMP)
	mkdir -p $(BYACC_TMP)
	cd $(BYACC_TMP); $(configure)

	touch $@

config-byacc2-stamp:
	dh_testdir

	rm -rf $(BTYACC_TMP)
	mkdir -p $(BTYACC_TMP)
	cd $(BTYACC_TMP); $(configure) \
		--enable-btyacc \
		--with-max-table-size=123456 \
		--program-transform-name='s,^yacc,byacc2,'

	touch $@

build-byacc-stamp: config-byacc-stamp
	dh_testdir

	cd $(BYACC_TMP); $(MAKE)

	touch $@

build-byacc2-stamp: config-byacc2-stamp
	dh_testdir

	cd $(BTYACC_TMP); $(MAKE)

	touch $@

install-byacc-stamp: build-byacc-stamp
	dh_testdir
	dh_testroot
	dh_installdirs

	cd $(BYACC_TMP); $(MAKE) install DESTDIR=$(BYACC_DIR)
	sed -i '/- an LALR/s,^..N,byacc,' $(BYACC_DIR)/usr/share/man/man1/byacc.1

	touch $@

install-byacc2-stamp: build-byacc2-stamp
	dh_testdir
	dh_testroot
	dh_installdirs

	cd $(BTYACC_TMP); $(MAKE) install DESTDIR=$(BTYACC_DIR)
	sed -i '/- an LALR/s,^..N,byacc2,' $(BTYACC_DIR)/usr/share/man/man1/byacc2.1

	touch $@

clean:
	dh_testdir
	dh_testroot

	dh_clean $(verbose)

	rm -rf t

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_lintian           $(verbose) $(PACKAGES.arch:%=-p%)
	dh_installdocs	     $(verbose) $(PACKAGES.arch:%=-p%)
	dh_installexamples   $(verbose) $(PACKAGES.arch:%=-p%)
	dh_installchangelogs $(verbose) $(PACKAGES.arch:%=-p%) CHANGES

# Build architecture-dependent files here.
binary-arch: build install
ifneq ($(PACKAGES.arch),)
	rm -f $(PACKAGES.arch:%=install-%-stamp)
	dh_testdir
	dh_testroot
	dh_lintian           $(verbose) $(PACKAGES.arch:%=-p%)
	dh_strip	     $(verbose) $(PACKAGES.arch:%=-p%)
	dh_compress	     $(verbose) $(PACKAGES.arch:%=-p%)
	dh_fixperms	     $(verbose) $(PACKAGES.arch:%=-p%)
	dh_installdeb	     $(verbose) $(PACKAGES.arch:%=-p%)
	dh_shlibdeps	     $(verbose) $(PACKAGES.arch:%=-p%)
	dh_gencontrol	     $(verbose) $(PACKAGES.arch:%=-p%)
	dh_md5sums	     $(verbose) $(PACKAGES.arch:%=-p%)
	dh_builddeb	     $(verbose) $(PACKAGES.arch:%=-p%)
endif

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