#!/usr/bin/make -f

CC =gcc
STRIP =strip

DEB_BUILD_MAINT_OPTIONS =hardening=+all
CFLAGS =$(shell DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) \
  dpkg-buildflags --get CFLAGS)
LDFLAGS =$(shell DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) \
  dpkg-buildflags --get LDFLAGS)
CPPFLAGS =$(shell DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) \
  dpkg-buildflags --get CPPFLAGS)

BUILD_DATE := $(shell dpkg-parsechangelog | sed -n -e 's/^Date: //p')
DEB_HOST_GNU_TYPE =$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE =$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
  CC =$(DEB_HOST_GNU_TYPE)-gcc
  STRIP =$(DEB_HOST_GNU_TYPE)-strip
endif

ifneq (,$(findstring diet,$(DEB_BUILD_OPTIONS)))
  CC =diet -v -Os gcc
  CFLAGS =-nostdinc -Wall
  CPPFLAGS =
  LDFLAGS =
endif
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  STRIP =: strip
endif

DIR =$(shell pwd)/debian/dash
DIRA =$(shell pwd)/debian/ash

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- --enable-fnmatch --disable-lineno

PREINST_OBJECTS = debian/dash.preinst.o debian/dash.preinst-lib.o

$(PREINST_OBJECTS): %.o: %.c debian/dash.preinst.h
	$(CC) -c -o $@ $(CFLAGS) $<

debian/dash.preinst: $(PREINST_OBJECTS)
	$(CC) -o debian/dash.preinst $(CFLAGS) $(PREINST_OBJECTS)
	$(STRIP) -R .comment -R .note debian/dash.preinst

override_dh_auto_build: debian/dash.preinst po-templates
	dh_auto_build

po-templates:
	po2debconf debian/dash.templates.in >debian/dash.templates

.PHONY: po-templates  \
	  override_dh_auto_configure override_dh_auto_build
