#!/usr/bin/make -f

include /usr/share/dpkg/buildflags.mk

export DH_VERBOSE = 1

# We've patched it, so i386 is suitable for any arch:
cmd := \
	cmd-inet/usr.sbin/ifconfig \
	cmd-inet/sbin/netstrategy \
	cmd-inet/usr.bin/netstat \

man := \
	usr/src/man/man1m/arp.1m \
	usr/src/man/man1m/ifconfig.1m \
	usr/src/man/man1m/ndd.1m \
	usr/src/man/man1m/netstat.1m \
	usr/src/man/man1m/netstrategy.1m \
	usr/src/man/man1m/route.1m \


unpack: unpack-stamp
unpack-stamp:
	dh_testdir
	dh_illumos_gate --build \
		usr/src/cmd/cmd-inet \
		$(man) \
		usr/src/lib/gss_mechs/mech_krb5/Makefile.mech_krb5 \
		usr/src/cmd/stat/common \


	# Buggy and unused:
	echo > usr/src/Makefile.msg.targ

	# Use GNU ld:
	sed -i '/LD_ALTEXEC/d' usr/env.sh
	echo 'BDIRECT=' >> usr/src/cmd/Makefile.cmd
	echo 'MAPFILE.NES=' >> usr/src/cmd/Makefile.cmd
	echo 'MAPFILE.PGA=' >> usr/src/cmd/Makefile.cmd
	echo 'MAPFILE.NED=' >> usr/src/cmd/Makefile.cmd

	# Disable ISA dirs:
	echo 'ROOTUSRSBIN32=$$(ROOTUSRSBIN)' >> usr/src/cmd/Makefile.cmd
	echo 'ROOTBIN32=$$(ROOTBIN)' >> usr/src/cmd/Makefile.cmd
	touch $@

patch: patch-stamp
patch-stamp: unpack-stamp
	dh_testdir
	[ ! -f debian/patches/series ] || QUILT_PATCHES=debian/patches quilt push -a || test $$? = 2
	touch $@

unpatch:
	dh_testdir
	[ ! -f debian/patches/series ] || QUILT_PATCHES=debian/patches quilt pop -a -f || test $$? = 2
	rm -f patch-stamp

dirs-stamp: unpack-stamp
	. usr/env.sh; mkdir -p \
		debian/tmp/sbin \
		debian/tmp/usr/sbin \
		debian/tmp/usr/bin
	touch $@

install: install-stamp

install-stamp: build-stamp
	install -m 755 -t debian/tmp/usr/sbin \
		usr/src/cmd/cmd-inet/usr.sbin/arp

	install -m 755 -t debian/tmp/usr/sbin \
		usr/src/cmd/cmd-inet/usr.sbin/ndd

	install -m 755 -t debian/tmp/sbin \
		usr/src/cmd/cmd-inet/usr.sbin/route
	ln -sf ../../sbin/route debian/tmp/usr/sbin/route

	touch $@

build build-arch build-indep: build-stamp

build-stamp: dirs-stamp patch-stamp
	dh_illumos_make --native usr/src/cmd/cmd-inet/usr.sbin -t arp
	dh_illumos_make --native usr/src/cmd/cmd-inet/usr.sbin -t ndd
	dh_illumos_make --native usr/src/cmd/cmd-inet/usr.sbin -t route
	dh_illumos_make --native $(cmd:%=usr/src/cmd/%)
	touch $@

binary binary-arch binary-indep: binary-stamp
binary-stamp: install-stamp
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_install
	dh_strip
	dh_installman
	dh_installdocs
	dh_installexamples
	dh_installchangelogs
	dh_bash-completion
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_strip
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -rf usr .pc
	dh_clean

