#! /usr/bin/make -f

p   = minicom
tmp = debian/tmp
DIR:=$(shell pwd)

define checkdir
	test -f src/minicom.c
endef

build:
# Builds the binary package.
	$(checkdir)
	( cd src && $(MAKE) )
	touch build

# Architecture independant files.
binary-indep:   build
	$(checkdir)

# Make a binary package (.deb file)
binary-arch:
	-rm -rf $(tmp)
	install -d -o root -g root -m 755 $(tmp)
	install -d -o root -g root -m 755 $(tmp)/DEBIAN
	install -d -o root -g root -m 755 $(tmp)/etc
	install -d -o root -g root -m 755 $(tmp)/usr/bin
	install -d -o root -g root -m 755 $(tmp)/usr/man/man1
	install -d -o root -g root -m 755 $(tmp)/usr/doc/$(p)
	( cd src && make install R=$(DIR)/$(tmp) )
	gzip -9f $(tmp)/usr/man/man*/*
	install -g root -m 644 debian/changelog $(tmp)/usr/doc/$(p)
	install -g root -m 644 -o root Copyright $(tmp)/usr/doc/$(p)/copyright
	install -g root -m 644 -o root debian/conffiles $(tmp)/DEBIAN/conffiles
	install -g root -m 755 -o root debian/preinst $(tmp)/DEBIAN/preinst
	install -g root -m 755 -o root debian/postinst $(tmp)/DEBIAN/postinst
	install -g root -m 644 -o root terminfo/README $(tmp)/usr/doc/minicom
	install -g root -m 644 -o root terminfo/minicom $(tmp)/usr/doc/minicom/minicom.terminfo
	install -g root -m 644 -o root doc/minirc.dfl $(tmp)/etc
	dpkg-shlibdeps src/minicom
	dpkg-gencontrol
	dpkg --build $(tmp) ..
	rm -rf $(tmp)

clean:	checkroot
	-( cd src && $(MAKE) clobber )
	rm -f build
	rm -rf $(tmp)
	find . \( -name '*~' -o -name '*.bak' -o -name '*.orig' \) -print0 | \
		xargs -0r rm --

binary:	binary-indep binary-arch

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary binary-arch binary-indep clean checkroot
