#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
#export DH_OPTIONS=-v

PROGRAM_PREFIX := gnu
PACKAGE_PREFIX := gnu-

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DEB_CFLAGS_MAINT_APPEND = -DSYSLOG_SUCCESS -DSYSLOG_FAILURE -DSYSLOG_NON_ROOT

# Renesas SH(sh4) need -mieee option.
ifeq ($(DEB_HOST_ARCH_CPU),sh4)
  DEB_CFLAGS_MAINT_APPEND += -mieee
endif

ifeq ($(DEB_HOST_ARCH),hurd-i386)
  Y2038 = --disable-year2038
endif

ifneq ($(filter nodoc,$(DEB_BUILD_PROFILES)),)
  export MAKEINFO = true
endif

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  cross_build := yes
  CROSS_CONFIGURE_FLAGS = --cache-file=./config.cache
  CROSS_BUILD_FLAGS = run_help2man=$(CURDIR)/man/dummy-man
  CROSS_INSTALL_FLAGS = EXTRA_MANS=
endif

BIN_PROGS = cat chgrp chmod chown cp date dd df dir echo false ln ls mkdir \
	mknod mv pwd readlink rm rmdir vdir sleep stty sync touch true uname \
	mktemp
d=debian/$(PACKAGE_PREFIX)coreutils

override_dh_update_autotools_config:
	# don't do this

override_dh_autoreconf:
	# don't do this

override_dh_auto_configure:
ifeq ($(cross_build),yes)
	echo 'fu_cv_sys_stat_statfs2_bsize=yes' > config.cache
endif
	dh_auto_configure -- --program-prefix=gnu --enable-systemd --enable-install-program=arch --with-openssl=auto-gpl-compat --with-packager=$(DEB_VENDOR) --with-packager-version=$(DEB_VERSION) $(Y2038)

%:
	dh $@

override_dh_auto_clean:
	make distclean || true

override_dh_auto_build:
	dh_auto_build -- $(CROSS_BUILD_FLAGS)

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifneq ($(cross_build),yes)
  ifeq ($(DEB_HOST_ARCH),armhf)
	: # on armhf, the cut-huge-range test fails on the buildds when run
	: # in the test suite. Run it explicitely, and then ignore the
	: # test results when run in the test suite.
	: # XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	-$(MAKE) SHELL=/bin/dash TESTS=tests/cut/cut-huge-range.sh check
	cat tests/cut/cut-huge-range.log
	rm -f tests/cut/cut-huge-range.log tests/cut/cut-huge-range.trs
	: # XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	if dh_auto_test; then \
	  echo "tests succeeded unexpectedly" ; \
	else \
	  echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; \
	  echo "TEST FAILURES (log)"; \
	  cat tests/cut/cut-huge-range.log; \
	  echo "TEST FAILURES (trs)"; \
	  cat tests/cut/cut-huge-range.trs; \
	  echo "ignoring the cut-huge-range test failure" ; \
	fi
  else
	dh_auto_test
  endif
endif
endif

override_dh_auto_install:
	dh_auto_install --destdir=$(d) -- $(CROSS_INSTALL_FLAGS)

override_dh_install-arch: 
	dh_install -a

	# remove stuff provided by other packages
ifeq ($(DEB_HOST_ARCH_OS),linux)
	# kill from procps is linux-specific
	rm -f $(d)/usr/bin/$(PROGRAM_PREFIX)kill $(d)/usr/share/man/man1/$(PROGRAM_PREFIX)kill.1
endif
	rm -f $(d)/usr/bin/$(PROGRAM_PREFIX)hostname $(d)/usr/share/man/man1/$(PROGRAM_PREFIX)hostname.1
	rm -f $(d)/usr/bin/$(PROGRAM_PREFIX)uptime $(d)/usr/share/man/man1/$(PROGRAM_PREFIX)uptime.1

ifeq ($(PROGRAM_PREFIX),)
	# the [ program doesn't have its own man page yet
	ln -s test.1 $(d)/usr/share/man/man1/[.1
endif

	# gnu thinks chroot is in bin, debian thinks it's in sbin
	install -d $(d)/usr/sbin $(d)/usr/share/man/man8
	mv $(d)/usr/bin/$(PROGRAM_PREFIX)chroot $(d)/usr/sbin/$(PROGRAM_PREFIX)chroot
	sed s/\"1\"/\"8\"/1 $(d)/usr/share/man/man1/$(PROGRAM_PREFIX)chroot.1 > $(d)/usr/share/man/man8/$(PROGRAM_PREFIX)chroot.8
	rm $(d)/usr/share/man/man1/$(PROGRAM_PREFIX)chroot.1

	# some build environments will leave a dangling info dir
	rm -f $(d)/usr/share/info/dir

ifneq ($(filter nodoc,$(DEB_BUILD_PROFILES)),)
	# Remove info files and manual pages if building with <nodoc>
	rm -rf $(d)/usr/share/info/
	rm -rf $(d)/usr/share/man/
endif

