#!/usr/bin/make -f

SOURCE = freebsd-quota
VERSION = $(shell dpkg-parsechangelog | sed -n '/^Version/ {s/.*: //; s/-.*//; p;}')
SVNLOC = "http://svn.freebsd.org/base/release/$(VERSION).0"

PATH := /usr/lib/freebsd:$(PATH)
DESTDIR = $(CURDIR)/debian/tmp
ORIGDIR = $(SOURCE)-$(VERSION)
TARNAME = $(SOURCE)_$(VERSION).orig.tar.gz

CFLAGS = $(shell dpkg-buildflags --get CPPFLAGS) -I. -D_GNU_SOURCE \
         $(shell dpkg-buildflags --get CFLAGS) -Wall -pipe -fPIC \
         $(shell pkg-config --cflags libbsd-overlay)
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
PMAKE = CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
        NO_WERROR=1 NOGCCERROR=1 NOSHARED=NO NO_SHARED=NO \
        DESTDIR=$(DESTDIR) make

PROGS_DEP = \
  sbin/fsck \
  sbin/fsck_ffs \
  $(nil)
PROGS = \
  libexec/rpc.rquotad \
  sbin/quotacheck \
  usr.bin/quota \
  usr.sbin/edquota \
  usr.sbin/quotaon \
  usr.sbin/repquota \
  $(nil)

get-orig-source:
	rm -rf $(ORIGDIR)
	for nn in $(PROGS_DEP) $(PROGS); do \
		svn export $(SVNLOC)/$$nn $(ORIGDIR)/$$nn; \
	done
	tar --numeric-owner --owner 0 --group 0 \
		-czf $(CURDIR)/../$(TARNAME) $(ORIGDIR)
	rm -rf $(ORIGDIR)

build-indep:

build-arch:
	for prog in $(PROGS); do \
	  $(PMAKE) -C $$prog; \
	done

build: build-indep build-arch

clean:
	dh_testdir
	dh_testroot

	for prog in $(PROGS); do \
	  $(PMAKE) -C $$prog clean; \
	done

	dh_clean

.PHONY: clean build-indep build-arch build

install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep -a
	dh_installdirs -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installinit -a -- start 13 S . stop 06 0 6 .
	dh_installman -a
	dh_install -a

binary-indep:
# Nothing to do.

binary-arch: install-arch
	dh_testdir -a
	dh_testroot -a
	dh_link -a
	dh_lintian -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

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