#!/usr/bin/make -f
# This file is public domain software, originally written by Joey Hess.

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

TAR_DIR = coreutils-6.10

# the dbs rules
include /usr/share/dbs/dbs-build.mk
include /usr/share/dbs/dpkg-arch.mk

# work around dpkg changes
DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU 2>/dev/null)
DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null)
ifeq ($(DEB_HOST_ARCH_CPU),)
  DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
  ifeq ($(DEB_HOST_ARCH_CPU),x86_64)
    DEB_HOST_ARCH_CPU := amd64
  endif
endif
ifeq ($(DEB_HOST_ARCH_OS),)
  DEB_HOST_ARCH_OS := $(subst -gnu,,$(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM))
  ifeq ($(DEB_HOST_ARCH_OS),gnu)
    DEB_HOST_ARCH_OS := hurd
  endif
endif

# implement no optimization build option
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS = "-g -DSYSLOG_SUCCESS -DSYSLOG_FAILURE -DSYSLOG_NON_ROOT -O0"
else
CFLAGS = "-g -DSYSLOG_SUCCESS -DSYSLOG_FAILURE -DSYSLOG_NON_ROOT -O2"
endif
LDFLAGS = 

# determine whether to build with SELinux
ifeq ($(DEB_HOST_ARCH_OS),linux)
  SELINUX_OPTS := --enable-selinux
else
  SELINUX_OPTS := 
endif

# programs to include/not include
ifeq ($(DEB_HOST_ARCH_OS),hurd)
  BUILDPROG_OPTS := --enable-install-program=su
else
  BUILDPROG_OPTS := --enable-no-install-program=hurd
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
d=debian/coreutils

default: binary

configure: configure-stamp
configure-stamp: $(patched)
	dh_testdir

	#cd $(BUILD_TREE) && autoreconf

	cd $(BUILD_TREE) && CFLAGS=$(CFLAGS) \
		LDFLAGS=$(LDFLAGS) ./configure \
		--build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) \
		--prefix=/usr -v \
		--infodir=/usr/share/info --mandir=/usr/share/man \
		--enable-no-install-program=hostname \
		--enable-no-install-program=uptime \
		--enable-no-install-program=mktemp \
		$(SELINUX_OPTS) $(BUILDPROG_OPTS)

	touch configure-stamp

build: configure-stamp build-stamp
build-stamp: $(patched)
	dh_testdir

	cd $(BUILD_TREE) && $(MAKE)
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	cd $(BUILD_TREE) && $(MAKE) check
endif

	touch build-stamp

clean:
	dh_testdir
	rm -f build-stamp configure-stamp

	rm -rf $(STAMP_DIR) $(SOURCE_DIR) debian/buildinfo

	dh_clean

install:
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd $(BUILD_TREE) && $(MAKE) install DESTDIR=$(CURDIR)/$(d)

	# some things go in root rather than usr
	for f in $(BIN_PROGS); do \
		mv $(d)/usr/bin/$$f $(d)/bin/$$f; \
	done
	
	# backward compatability
	ln -s /usr/bin/md5sum $(d)/usr/bin/md5sum.textutils
	ln -s /usr/share/man/man1/md5sum.1 $(d)/usr/share/man/man1/md5sum.textutils.1

	# some things we don't do for hurd
ifneq ($(DEB_HOST_ARCH_OS),hurd)
		# touch used to be in /usr/bin, don't break scripts
		ln -s /bin/touch $(d)/usr/bin/touch
endif

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

	# 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/chroot $(d)/usr/sbin/chroot
	sed s/\"1\"/\"8\"/1 $(d)/usr/share/man/man1/chroot.1 > $(d)/usr/share/man/man8/chroot.8
	rm $(d)/usr/share/man/man1/chroot.1

	cp $(BUILD_TREE)/AUTHORS \
		$(BUILD_TREE)/NEWS $(BUILD_TREE)/README \
		$(BUILD_TREE)/THANKS \
		$(BUILD_TREE)/TODO \
		$(d)/usr/share/doc/coreutils

	dh_install

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_buildinfo
	dh_installexamples -i
	dh_installinfo -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a $(BUILD_TREE)/ChangeLog 
	dh_installdocs -a
	dh_buildinfo -a
	dh_installexamples -a
	dh_installinfo -a $(BUILD_TREE)/doc/coreutils.info
	dh_installman -a
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -a
endif
	dh_link -a
	dh_compress -a
	dh_fixperms -a
ifeq ($(DEB_HOST_ARCH_OS),hurd)
	chmod u+s $(d)/usr/bin/su
endif
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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