#! /usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

unexport LANG LC_ALL LC_CTYPE LC_COLLATE LC_TIME LC_NUMERIC LC_MESSAGES

# architecture dependent variables
DEB_BUILD_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH		:= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS	:= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_GNU_CPU	:= $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
DEB_HOST_GNU_SYSTEM	:= $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
DEB_HOST_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

VERSION		:= 3.1
#PKGVERSION	:= $(shell dpkg-parsechangelog \
#			| sed -n '/Version:/s/\(.* \)\(.*\)-2\(.*\)/\2\3/p')
#PKGVERSION	:= 3.0
#dpkg_ctrl_args := -v$(PKGVERSION) -VBinary-Version=$(PKGVERSION)


with_gfdl	= no

CC	= gcc
CFLAGS	= -g -O2
SHELL	= bash
YACC	= bison -y

IX	= install -o 0 -g 0
ID	= install -o 0 -g 0 -m 644

# built with installed libreadline?
with_installed_rl = no

debflags =

PWD	:= $(shell pwd)
p	= vyatta-bash

d	= debian/$(p)

conf_args = \
	--with-curses \
	--disable-net-redirections \
	--enable-largefile \
	--prefix=/usr \
	--infodir=/usr/share/info \
	--mandir=/usr/share/man
ifeq ($(with_installed_rl),yes)
    conf_args += --with-installed-readline
endif

conf_args += --host=$(DEB_HOST_GNU_TYPE)

build: before-build bash-build

before-build:
	: # see #327477, needed to have HAVE_DEV_STDIN defined
	(test -d /dev/fd && test -r /dev/stdin < /dev/null) \
	  || (test -d /proc/self/fd && test -r /dev/stdin < /dev/null)
ifneq (,$(findstring $(DEB_HOST_ARCH_OS), linux freebsd))
	$(CC) $(CFLAGS) -o clear_console debian/clear_console.c -lncurses
endif

# ---------------------------------------------------------------------------
# build standard bash

bash-build:
	$(MAKE) -f debian/rules do-build-bash \
	    bash_src=bash \
	    bash_patches="$(debian_patches)" \
	    build=bash \
	    configure_args="$(conf_args)"
bash-configure:
	$(MAKE) -f debian/rules do-configure-bash \
	    bash_src=bash \
	    bash_patches="$(debian_patches)" \
	    build=bash \
	    configure_args="$(conf_args)"

# ---------------------------------------------------------------------------

check: stamps/stamp-check
stamps/stamp-check: bash-build
	@echo BEGIN test
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	-sh debian/locale-gen
	LOCPATH=$(PWD)/locales \
		$(MAKE) -C build-bash test 2>&1 | tee build-bash/test-protocol
else
	@echo Suppress 'make' test, because this is cross build
endif
	@echo END test
	touch stamps/stamp-check

# ---------------------------------------------------------------------------

clean:
	dh_testdir
	dh_testroot
	rm -rf stamps build-* bash bashdb
	rm -f debian/README.Debian
	rm -rf locales
	rm -f clear_console
	dh_clean

# ---------------------------------------------------------------------------

install: bash-install

bash-install: bash-build stamps/stamp-install-bash
stamps/stamp-install-bash: stamps/stamp-build-bash
	dh_testdir
	dh_testroot
	dh_clean -k -p$(p)
	dh_installdirs -p$(p) \
		bin

	: # install it
	$(MAKE) -C build-bash install \
		CC='$(CC)' \
		CFLAGS='$(CFLAGS)' \
		YACC="$(YACC)" \
		DESTDIR=$(PWD)/$(d)
	mv $(d)/usr/bin/vbash $(d)/bin/.
	rmdir $(d)/usr/bin

	cat debian/README > debian/README.Debian

	touch stamps/stamp-install-bash

binary-bash: bash-install
	rm -f debian/files
	dh_testdir
	dh_testroot
	dh_installchangelogs -p$(p) bash/CWRU/changelog
	dh_installdocs -p$(p) \
		bash/{CHANGES,NEWS,COMPAT,doc/INTRO,POSIX} \
		debian/{README.Debian,README.abs-guide,README.commands} \
		debian/inputrc.arrows
	dh_strip -p$(p)
	dh_compress -p$(p)
	dh_fixperms -p$(p)
	dh_shlibdeps -p$(p) -- -dPre-Depends $(d)/bin/vbash
	dh_installdeb -p$(p)
	if [ -f "../.VYATTA_DEV_BUILD" ]; then \
		dh_gencontrol -p$(p) -- -v999.dev; \
	else \
		dh_gencontrol -p$(p); \
	fi
	dh_md5sums -p$(p)
	dh_builddeb -p$(p)

binary-arch: binary-bash
binary:	binary-arch

# ---------------------------------------------------------------------------
# common rules for all bash build variations

do-build-$(build): stamps/stamp-build-$(build)
stamps/stamp-build-$(build): stamps/stamp-configure-$(build)
	dh_testdir
	$(MAKE) -C build-$(build) \
		CC='$(CC)' \
		CFLAGS='$(CFLAGS)' \
		YACC="$(YACC)" \
		deb_builddir=build-$(build)/ \
		$(debflags)
	touch stamps/stamp-build-$(build)

do-configure-$(build): stamps/stamp-configure-$(build)
stamps/stamp-configure-$(build): stamps/stamp-unpack-$(bash_src)
	dh_testdir
	rm -rf build-$(build)
	mkdir build-$(build)
	cd build-$(build) && \
	    CC="$(CC)" CFLAGS="$(CFLAGS)" YACC="$(YACC)" \
		../$(bash_src)/configure $(configure_args)
	if ! grep -q '#define HAVE_DEV_STDIN 1' build-$(build)/config.h; then \
	  echo "HAVE_DEV_STDIN not defined, abortig build"; \
	  exit 1; \
	fi
	touch stamps/stamp-configure-$(build)

unpack-$(bash_src): stamps/stamp-unpack-$(bash_src)
stamps/stamp-unpack-$(bash_src):
	mkdir -p stamps
	rm -rf bash-$(VERSION) $(bash_src)
	rm -f stamps/stamp-patch-$(bash_src){,-*}
	tar c --exclude='bash-src.tar' --exclude='debian' --exclude='.git' \
	      --exclude='stamps' --exclude='bash' --exclude='build-bash' \
	      --exclude='build-min' --exclude='build-static' \
	      -f bash-src.tar .
	mkdir -p $(bash_src)
	tar x -C $(bash_src) -f bash-src.tar
	rm -f bash-src.tar
	cp -p /usr/share/misc/config.* $(bash_src)/.
	cp -p /usr/share/misc/config.* $(bash_src)/support/.
	touch stamps/stamp-unpack-$(bash_src)

.PHONY: unpack patch binary binary-arch clean build bash-build \
    check bash-configure binary-bash install bash-install

# Local Variables:
# mode: makefile
# end: