#!/usr/bin/make -f

package = base-files
docdir = debian/tmp/usr/share/doc/$(package)

OSNAME = "GNU/`uname`"
ifeq ($(DEB_HOST_GNU_SYSTEM),linux)
  OSNAME=GNU/Linux
endif
ifeq ($(DEB_HOST_GNU_SYSTEM),gnu)
  OSNAME=GNU/Hurd
endif

build:
	$(checkdir)
	touch build

clean:
	$(checkdir)
	-rm -f build
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp debian/files* core debian/substvars

binary-arch: checkroot build
	$(checkdir)
	-rm -rf debian/tmp
	install -d debian/tmp/DEBIAN $(docdir)
	cd debian/tmp && install -d usr/share/$(package)
	install -p -m 644 debian/copyright debian/changelog \
		debian/README.FHS debian/README.base $(docdir)
	install -m 755 debian/remove-base $(docdir)
	cd debian && install -m 755 preinst postinst prerm tmp/DEBIAN
	cd debian && install -m 644 conffiles tmp/DEBIAN
	cd debian/tmp && install -d `cat ../directory-list`
	cd debian/tmp && chown root.src     usr/src
	# cd debian/tmp && chown root.mail    var/spool/mail
	cd debian/tmp && chown root.staff   var/local
	cd debian/tmp && chown root.disk    boot
	cd debian/tmp && chown root.staff   home
	cd debian/tmp && chmod 755  `find . -type d`
	cd debian/tmp && chmod 1777 `cat ../1777-dirs`
	cd debian/tmp && chmod 2775 `cat ../2775-dirs`
	install -p -m 644 share/* debian/tmp/usr/share/base-files
	install -p -m 644 licenses/* debian/tmp/usr/share/common-licenses
	ln -s LGPL-2.1 debian/tmp/usr/share/common-licenses/LGPL
	ln -s ../../share/common-licenses/GPL \
		debian/tmp/usr/doc/copyright/GPL
	ln -s ../../share/common-licenses/LGPL-2 \
		debian/tmp/usr/doc/copyright/LGPL
	ln -s ../../share/common-licenses/BSD \
		debian/tmp/usr/doc/copyright/BSD
	ln -s ../../share/common-licenses/Artistic \
		debian/tmp/usr/doc/copyright/Artistic 
	install -p -m 644 etc/* debian/tmp/etc
ifeq ($(DEB_HOST_GNU_SYSTEM),gnu)
	rmdir debian/tmp/proc
endif
	cat etc/motd | sed -e "s&#OSNAME#&$(OSNAME)&g" > debian/tmp/etc/motd
	cat share/info.dir | sed -e "s&#OSNAME#&$(OSNAME)&g" > debian/tmp/usr/share/base-files/info.dir
	gzip -9 $(docdir)/changelog
	dpkg-gencontrol -isp
	dpkg --build debian/tmp ..

binary-indep: checkroot build
	$(checkdir)

define checkdir
	test -f debian/rules
endef

binary: binary-indep binary-arch

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

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