#!/usr/bin/make -f
# $Id: rules,v 1.1.1.1.2.1 2004/07/16 16:44:36 williams Exp $

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

ifeq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
DEBUG :=
else
DEBUG := -g
endif

DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

CDEF := \
	-Wall -DBSD=1 -DSMALL -D_GNU_SOURCE -DGLOB_BROKEN -DHAVE_VASPRINTF=1 \
	-DIFS_BROKEN -DGCC_BROKEN_NG \
	-D__COPYRIGHT\(x\)= -D__RCSID\(x\)= -D_DIAGASSERT\(x\)=

OPT := -O2 -fstrict-aliasing
OPTSM := -Os -fstrict-aliasing -fomit-frame-pointer -DREALLY_SMALL

ifeq ($(DEB_BUILD_ARCH),i386)
OPTSM += \
	-malign-loops=0 -malign-jumps=0 -malign-functions=0 \
	-mpreferred-stack-boundary=2 -DUSE_NORETURN
endif

export CURDIR

setup: setup-stamp
setup-stamp:
	rm -rf build-small build-udeb
	mkdir build-small build-udeb
	printf ".PATH: %s %s/bltin\nCPPFLAGS+=-I%s\n.include <%s/Makefile>" \
		"$$CURDIR" "$$CURDIR" "$$CURDIR" "$$CURDIR" \
		> build-small/Makefile
	ln build-small/Makefile build-udeb
	chmod u+x debian/bsdyacc
	touch setup-stamp

build: build-stamp
build-stamp: setup-stamp
	dh_testdir

	cd build-small; \
		pmake CFLAGS:='$(DEBUG) $(CDEF) $(OPT)' \
			"YACC:=$$CURDIR/debian/bsdyacc"
	cd build-udeb; \
		pmake CFLAGS:='$(DEBUG) $(CDEF) $(OPTSM)' \
			"YACC:=$$CURDIR/debian/bsdyacc"

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp setup-stamp

	rm -rf build-small build-udeb

	dh_clean

# Build architecture-independent files here.
binary-indep: build
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build
#	dh_testversion
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	install build-small/sh debian/tmp/bin/ash
	install -m 644 sh.1 debian/tmp/usr/share/man/man1/ash.1
	install build-udeb/sh debian/ash-udeb/bin/ash
	ln -s ash debian/ash-udeb/bin/sh

	dh_installdebconf
	dh_installdocs -Nash-udeb
	dh_installexamples
	dh_installmenu
	dh_installcron
#	dh_installmanpages
	dh_installchangelogs -Nash-udeb
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip
endif
	dh_compress
	dh_fixperms
	dh_installdeb -Nash-udeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	for i in ../ash-udeb_*.deb; do mv $$i $${i%deb}udeb; done
	sed '/^[^ ]*\.udeb/d; s/^\(ash-udeb_[^ ]*\.\)deb/\1udeb/' \
		debian/files > debian/files.new
	mv debian/files.new debian/files

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary setup
