#!/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

BUILD_DATE := $(shell dpkg-parsechangelog -S Date)

VENDORFILE = ubuntu

VERSION = $(shell cat etc/debian_version)

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

build:
	touch build

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

binary-arch: build
	rm -rf debian/tmp
	install -d debian/tmp/DEBIAN $(docdir) debian/tmp/etc/dpkg/origins
	install -p -m 644 origins/* debian/tmp/etc/dpkg/origins
	cd debian/tmp && install -d usr/share/$(package)
	install -p -m 644 debian/changelog debian/README \
		debian/README.FHS $(docdir)
	cat debian/copyright.in | sed -e "s&#OSNAME#&$(OSNAME)&g" \
		> $(docdir)/copyright
	chmod 644 $(docdir)/copyright
	sh debian/check-md5sum-etc profile
	sh debian/check-md5sum-etc nsswitch.conf
	sed -e "s/#VENDORFILE#/$(VENDORFILE)/g" debian/postinst.in \
		> debian/tmp/DEBIAN/postinst
	chmod 755 debian/tmp/DEBIAN/postinst
	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
	install -p -m 644 debian/lintian \
		debian/tmp/usr/share/lintian/overrides/$(package)
	ln -s GFDL-1.3 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
	ln -s README $(docdir)/FAQ
	install -p -m 644 etc/* debian/tmp/etc
	install -p -m 755 debian/update-motd.d/* debian/tmp/etc/update-motd.d
ifeq ($(DEB_HOST_GNU_SYSTEM),gnu)
	rmdir debian/tmp/sys
endif
	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
	sed -e "s&#OSNAME#&$(OSNAME)&g" etc/os-release \
		> debian/tmp/etc/os-release
	mv debian/tmp/etc/os-release debian/tmp/usr/lib
	ln -s ../usr/lib/os-release debian/tmp/etc/os-release
	gzip -9n $(docdir)/changelog
	cd debian/tmp && \
		find usr -type f ! -regex "DEBIAN/.*" -print0 |\
		LC_ALL=C sort -z | xargs -0r md5sum > DEBIAN/md5sums
	chown -R root:root debian/tmp
	chmod -R u+w,go=rX debian/tmp
	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`
	cd debian/tmp && chmod 700 root
	dpkg-gencontrol -pbase-files
	find debian/tmp -newermt '$(BUILD_DATE)' -print0 |\
		xargs -0r touch -h --date='$(BUILD_DATE)'
	dpkg-deb --build -Zgzip 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-deb --build debian/lsb-release-udeb \
		../lsb-release-udeb_$(DEBVERSION)_all.udeb

binary: binary-indep binary-arch

build-arch: build

build-indep: build

.PHONY: binary binary-arch binary-indep clean
