#!/usr/bin/make -f

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

PACKAGE="procps"
DEBROOT=$(CURDIR)/debian/tmp

DEB_HOST_ARCH_OS	?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)
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))
CROSS=CC=$(DEB_HOST_GNU_TYPE)-gcc
else
CROSS=
endif

PROCLIB = $(shell basename proc/libproc-*.so)
CFLAGS=-g

%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean
	if [ -d static ] ; then rm -rf static ; fi

override_dh_auto_build:
	dh_testdir

	[ -d static ] || mkdir static
	$(MAKE) $(CROSS) lib64=lib m64= SHARED=0 CFLAGS="$(CFLAGS)" proc/libproc.a 
	mv proc/libproc.a static
	$(MAKE) clean
	$(MAKE) $(CROSS) W_SHOWFROM=\-DW_SHOWFROM lib64=lib m64= CFLAGS="$(CFLAGS)"
	( cd static && ln -s /lib/$(PROCLIB) libproc.so )

	touch build-stamp

override_dh_auto_install:
	$(MAKE) $(CROSS) lib64=lib ln_f="ln -sf" ldconfig=echo DESTDIR=$(DEBROOT) install

	# Build up sysctl.d
	install -d $(DEBROOT)/etc/sysctl.d/
	install --mode 644 -o root -g root debian/sysctl.d/*.conf debian/sysctl.d/README $(DEBROOT)/etc/sysctl.d/
ifneq (,$(wildcard debian/sysctl.d/*.conf.$(DEB_HOST_ARCH)))
	# If a non-arch-specific default exists, install the arch-specific
	# version of the conf in place of it, otherwise, build up a general
	# 10-arch-specific.conf file.
	for archconf in debian/sysctl.d/*.conf.$(DEB_HOST_ARCH); do \
	    conf=$$(basename $$archconf .$(DEB_HOST_ARCH)); \
	    if [ -f debian/sysctl.d/$$conf ]; then \
		install --mode 644 -o root -g root $$archconf $(DEBROOT)/etc/sysctl.d/$$conf; \
	    else \
		cat $$archconf >> $(DEBROOT)/etc/sysctl.d/10-arch-specific.conf; \
	    fi; \
	done
endif

	# Rename w as there are two of them
	(cd $(DEBROOT)/usr/bin && mv w w.procps )
	(cd $(DEBROOT)/usr/share/man/man1 && mv w.1 w.procps.1 )

ifneq ($(DEB_HOST_ARCH_OS), linux)
	rm -f \
		$(DEBROOT)/usr/bin/slabtop \
		$(DEBROOT)/usr/share/man/man1/slabtop.1 \
		$(DEBROOT)/sbin/sysctl \
		$(DEBROOT)/usr/share/man/man8/sysctl.8 \
		$(DEBROOT)/usr/share/man/man5/sysctl.conf.5 \
		$(NULL)
endif
ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
	rm -f \
		$(DEBROOT)/bin/kill \
		$(DEBROOT)/usr/share/man/man1/kill.1 \
		$(NULL)
endif
ifeq ($(DEB_HOST_ARCH_OS), hurd)
	rm -f \
		$(DEBROOT)/usr/bin/pmap \
		$(DEBROOT)/usr/share/man/man1/pmap.1 \
		$(NULL)
    # Rename kill as there are two of them
	(cd $(DEBROOT)/bin && mv kill kill.procps )
	(cd $(DEBROOT)/usr/share/man/man1 && mv kill.1 kill.procps.1 )

	# Rename vmstat as there are two of them
	(cd $(DEBROOT)/usr/bin && mv vmstat vmstat.procps )
	(cd $(DEBROOT)/usr/share/man/man8 && mv vmstat.8 vmstat.procps.8 )
	
    # Rename uptime as there are two of them
	(cd $(DEBROOT)/usr/bin && mv uptime uptime.procps )
	(cd $(DEBROOT)/usr/share/man/man1 && mv uptime.1 uptime.procps.1 )

	# Rename ps as there are two of them
	(cd $(DEBROOT)/bin && mv ps ps.procps )
	(cd $(DEBROOT)/usr/share/man/man1 && mv ps.1 ps.procps.1 )
endif

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_installinit:
ifeq ($(DEB_HOST_ARCH_OS), linux)
	dh_installinit --error-handler=: --update-rcd-params='start 17 S .'
endif

