#!/usr/bin/make -f

package=sysutils
tempdir=$(shell pwd)/debian/tmp
insdirs=$(tempdir)/$(package)/DEBIAN \
	$(tempdir)/$(package)/usr/bin \
	$(tempdir)/$(package)/usr/share/doc/$(package) \
	$(tempdir)/$(package)/usr/share/man/man1 \
	$(tempdir)/$(package)/usr/share/man/man8 \
	$(tempdir)/$(package)/usr/sbin

binary: binary-indep binary-arch

binary-arch: debian/control checkroot build $(insdirs)
	install -m 644 debian/sysutils.1 $(tempdir)/$(package)/usr/share/man/man1
	install -m 755 bogo-1.2/bogomips $(tempdir)/$(package)/usr/bin
	install -m 644 bogo-1.2/bogomips.1 $(tempdir)/$(package)/usr/share/man/man1
	install -m 755 memtester-2.93.1/memtest $(tempdir)/$(package)/usr/sbin
	install -m 644 memtester-2.93.1/memtest.1 $(tempdir)/$(package)/usr/share/man/man1
	install -m 755 procinfo-18/procinfo $(tempdir)/$(package)/usr/bin
	install -m 755 procinfo-18/lsdev.pl $(tempdir)/$(package)/usr/bin/lsdev
	install -m 644 procinfo-18/procinfo.8 $(tempdir)/$(package)/usr/share/man/man8
	install -m 644 procinfo-18/lsdev.8 $(tempdir)/$(package)/usr/share/man/man8/lsdev.8
	install -m 755 tofrodos-1.4/fromdos $(tempdir)/$(package)/usr/bin/fromdos
	install -m 644 tofrodos-1.4/fromdos.1 $(tempdir)/$(package)/usr/share/man/man1/fromdos.1
	ln -s fromdos $(tempdir)/$(package)/usr/bin/todos
	ln -s fromdos $(tempdir)/$(package)/usr/bin/dos2unix
	ln -s fromdos $(tempdir)/$(package)/usr/bin/unix2dos


	install -m 644 debian/changelog $(tempdir)/$(package)/usr/share/doc/$(package)/changelog
	find $(tempdir)/$(package)/usr/share/doc -type f | xargs -r gzip -v9	
	install -m 644 debian/copyright $(tempdir)/$(package)/usr/share/doc/$(package)
	find $(tempdir)/$(package)/usr/share/man -type f | xargs -r gzip -v9
	ln -s fromdos.1.gz $(tempdir)/$(package)/usr/share/man/man1/todos.1.gz
	ln -s fromdos.1.gz $(tempdir)/$(package)/usr/share/man/man1/dos2unix.1.gz
	ln -s fromdos.1.gz $(tempdir)/$(package)/usr/share/man/man1/unix2dos.1.gz
	dh_fixperms
	dh_perl
	dh_installdeb -P$(tempdir)/$(package)
	dpkg-shlibdeps -p$(package) procinfo-18/procinfo memtester-2.93.1/memtest bogo-1.2/bogomips tofrodos-1.4/fromdos
	dpkg-gencontrol -P$(tempdir)/$(package) -p$(package)
	dpkg --build $(tempdir)/$(package) ..

binary-indep: debian/control checkroot build $(insdirs)

CC = gcc
CFLAGS = -O2 -fomit-frame-pointer
LDFLAGS = -s

build: debian/control
	cd procinfo-18 && make CFLAGS="$(CFLAGS) -s" LDFLAGS="$(LDFLAGS)" LDLIBS="-lncurses"
	cd memtester-2.93.1 && make CFLAGS="$(CFLAGS) -s" LDFLAGS="$(LDFLAGS)"
	cd bogo-1.2 && make CFLAGS="$(CFLAGS) -s -finline-functions -Wall" LDFLAGS="$(LDFLAGS)"
	cd tofrodos-1.4 && make CFLAGS="$(CFLAGS) -c -s -Wall" LDFLAGS="$(LDFLAGS)"
	touch build

checkroot: debian/control
	test "root" = "$(shell whoami)"

clean: debian/control
	cd bogo-1.2 && make clean
	cd memtester-2.93.1 && make clean
	cd procinfo-18 && make clean
	cd tofrodos-1.4 && make clean
	for i in build debian/files debian/substvars `find -name "*~"`; do \
		if [ -e $$i ]; then \
			rm -f $$i; \
		fi; \
	done
	for i in $(tempdir); do \
		if [ -d $$i ]; then \
			rm -rf $$i; \
		fi; \
	done
	dh_clean

$(insdirs):
	if [ -d $@ ]; then \
		rm -rf $@; \
	fi
	install -d -m 755 $@
	chmod g-s $@

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

#Local variables:
#mode: makefile
#End:
