#!/usr/bin/make -f

install         := install -o root -g root
install_exec    := $(install) -m 0755 -s
install_nonex   := $(install) -m 0644
install_dir     := $(install) -m 0755 -d
install_script  := $(install) -m 0755
install_symlink := ln -s

gzip           := gzip -9
strip_lib      := strip --strip-unneeded

tmpdir := $(shell pwd)/debian/tmp

# These must not be :='s!
rootdir = $(tmpdir)/$(package)
ctldir = $(rootdir)/DEBIAN
bindir = $(rootdir)/usr/bin
docdir = $(rootdir)/usr/share/doc/$(package)

build: hello hello++

binary: binary-arch binary-indep

define prebinary
        $(RM) -r $(rootdir)
        $(install_dir) $(ctldir)
        $(install_dir) $(docdir)
        $(install_script) debian/prerm $(ctldir)
        $(install_script) debian/postinst $(ctldir)
        $(install_nonex) debian/copyright $(docdir)
        $(install_nonex) debian/changelog $(docdir)/changelog
        $(gzip) $(docdir)/changelog
endef

define postbinary
        chmod -R g-s $(rootdir)
        dpkg-gencontrol -isp -p$(package) -P$(rootdir)
        dpkg --build $(rootdir) ..
endef

binary-arch: package=hello-bess
binary-arch: build
	$(prebinary)
	$(install_dir) $(bindir)
	$(install_exec) hello $(bindir)
	$(install_exec) hello++ $(bindir)
	dpkg-shlibdeps $(bindir)/*
	$(postbinary)

binary-indep:
#	Nothing to do

clean:
	$(RM) hello hello++ debian/files debian/substvars
	$(RM) -r debian/tmp