#!/usr/bin/make -f

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

OSNAME = "GNU/`uname | sed -e 's/GNU\///'`"
ifeq ($(DEB_HOST_GNU_SYSTEM),linux)
  OSNAME=GNU/Linux
endif
ifeq ($(DEB_HOST_GNU_SYSTEM),gnu)
  OSNAME=GNU/Hurd
endif

VERSION = $(shell cat etc/debian_version)

DEBVERSION := $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2)

build:
	cd debian && sed -e "s&#VERSION#&$(VERSION)&" preinst.in > preinst
	touch build

clean:
	rm -f build debian/preinst
	rm -f `find . -name "*~"`
	rm -rf debian/tmp debian/lsb-release-udeb debian/files* core debian/substvars

	# update md5sums list for profile
	LIST="`md5sum share/profile | cut -f 1 -d\  | cat share/profile.md5sums - | sort -u`"; \
	echo "$$LIST" > share/profile.md5sums

binary-arch: build
	rm -rf debian/tmp
	install -d debian/tmp/DEBIAN $(docdir)
	cd debian/tmp && install -d usr/share/$(package)
	install -p -m 644 debian/changelog debian/FAQ \
		debian/README.FHS debian/README.base $(docdir)
	cat debian/copyright.in | sed -e "s&#OSNAME#&$(OSNAME)&g" \
		> $(docdir)/copyright
	install -m 755 debian/remove-base $(docdir)
	cd debian && install -m 755 preinst postinst tmp/DEBIAN
	cd debian && install -m 644 conffiles tmp/DEBIAN
	cd debian/tmp && install -d `cat ../directory-list`
	install -p -m 644 share/* debian/tmp/usr/share/base-files
	install -p -m 644 licenses/* debian/tmp/usr/share/common-licenses
	ln -s GFDL-1.2 debian/tmp/usr/share/common-licenses/GFDL
	ln -s LGPL-3 debian/tmp/usr/share/common-licenses/LGPL
	ln -s GPL-3 debian/tmp/usr/share/common-licenses/GPL
	install -p -m 644 etc/* debian/tmp/etc
	rm -f debian/tmp/etc/motd
ifeq ($(DEB_HOST_GNU_SYSTEM),gnu)
	rmdir debian/tmp/proc
endif
	sed -e "s&#OSNAME#&$(OSNAME)&g" etc/motd \
		> debian/tmp/usr/share/base-files/motd
	sed -e "s&#OSNAME#&$(OSNAME)&g" share/info.dir \
		> debian/tmp/usr/share/base-files/info.dir
	sed -e "s&#OSNAME#&$(OSNAME)&g" etc/issue \
		> debian/tmp/etc/issue
	sed -e "s&#OSNAME#&$(OSNAME)&g" etc/issue.net \
		> debian/tmp/etc/issue.net
	gzip -9 $(docdir)/changelog
	chown -R root:root debian/tmp
	cd debian/tmp && chown root:src     usr/src
	cd debian/tmp && chown root:staff   var/local
	cd debian/tmp && chmod 755  `find . -type d`
	cd debian/tmp && chmod 1777 `cat ../1777-dirs`
	cd debian/tmp && chmod 2775 `cat ../2775-dirs`
	dpkg-gencontrol -pbase-files -isp
ifeq ($(shell dpkg-architecture -qDEB_BUILD_ARCH),amd64)
	sed -i -e 's/^Depends:/Depends: libc6 \(>= 2.4-1ubuntu3\),/' \
		debian/tmp/DEBIAN/control
endif
	dpkg --build debian/tmp ..

binary-indep: build
	rm -rf debian/lsb-release-udeb
	install -d debian/lsb-release-udeb/DEBIAN debian/lsb-release-udeb/etc
	install -p -m 644 etc/lsb-release debian/lsb-release-udeb/etc
	chown -R root:root debian/lsb-release-udeb
	cd debian/lsb-release-udeb && chmod 755 `find . -type d`
	dpkg-gencontrol -plsb-release-udeb \
		-Tdebian/lsb-release-udeb.substvars -Pdebian/lsb-release-udeb \
		-isp -fdebian/files~
	dpkg-distaddfile lsb-release-udeb_$(DEBVERSION)_all.udeb \
		debian-installer extra
	dpkg --build debian/lsb-release-udeb \
		../lsb-release-udeb_$(DEBVERSION)_all.udeb

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean
