#! /usr/bin/make -f
%:
	dh $@

DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CC := gcc
else
CC := $(DEB_HOST_GNU_TYPE)-gcc
endif

export DEB_CFLAGS_MAINT_APPEND := -Wall -W -Os -fomit-frame-pointer

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

TERMNAMES = a/ansi d/dumb s/screen

ifeq ($(DEB_HOST_ARCH_OS),linux)
TERMNAMES += l/linux
endif

ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
TERMNAMES += c/cons25 x/xterm
endif

ifeq ($(DEB_HOST_ARCH_OS),hurd)
TERMNAMES += h/hurd m/mach-gnu-color
endif

TERMS = $(foreach term,$(TERMNAMES),\
          $(firstword $(wildcard /lib/terminfo/$(term)) \
                      $(wildcard /usr/share/terminfo/$(term))))

override_dh_auto_build:
	$(MAKE) CC=$(CC) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"

override_dh_auto_install:
	for file in $(TERMS); do \
		mkdir -p debian/di-utils-terminfo/`dirname $$file`; \
		cp -pL $$file debian/di-utils-terminfo/$$file; \
		chmod 644 debian/di-utils-terminfo/$$file; \
	done
	tic vt102-di -o debian/di-utils-terminfo/lib/terminfo

	mkdir -p debian/di-utils/bin
	cp -pL list-devices-$(DEB_HOST_ARCH_OS) debian/di-utils/bin/list-devices
	chmod 755 debian/di-utils/bin/list-devices
