#! /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
vafilt = $(subst $(2)=,,$(filter $(2)=%,$(1)))
DPKG_VARS := $(shell dpkg-architecture)
DEB_BUILD_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH		?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH)
DEB_HOST_ARCH_OS	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH_OS)
DEB_HOST_GNU_CPU	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_CPU)
DEB_HOST_GNU_SYSTEM	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_SYSTEM)
DEB_HOST_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_TYPE)
DEB_HOST_MULTIARCH	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_MULTIARCH)

VERSION		:= 4.2
#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	= yes

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
  CC	= $(DEB_HOST_GNU_TYPE)-gcc
else
  CC	= gcc
endif

ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  STRIP = :
else
  ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
    STRIP = $(DEB_HOST_GNU_TYPE)-strip
  else
    STRIP = strip
  endif
endif


dpkg_buildflags = DEB_BUILD_MAINT_OPTIONS="hardening=+all" DEB_CFLAGS_MAINT_APPEND="-Wall" dpkg-buildflags
CFLAGS := $(shell $(dpkg_buildflags) --get CFLAGS)
CPPFLAGS := $(shell $(dpkg_buildflags) --get CPPFLAGS)
LDFLAGS := $(shell $(dpkg_buildflags) --get LDFLAGS)

SHELL	= /bin/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 =

p	= bash
p_stat	= bash-static
p_bins	= bash-builtins
p_doc	= bash-doc

d	= debian/$(p)
d_stat	= debian/$(p_stat)
d_bins	= debian/$(p_bins)
d_doc	= debian/$(p_doc)

termcap_lib := $(if $(wildcard /usr/lib/libtinfo.so /usr/lib/$(DEB_HOST_MULTIARCH)/libtinfo.so), \
		 -ltinfo, \
		 -lncurses)

conf_args = \
	--enable-largefile \
	--prefix=/usr \
	--infodir=/usr/share/info \
	--mandir=/usr/share/man \
	--without-bash-malloc
ifeq ($(with_installed_rl),yes)
    conf_args += --with-installed-readline
endif
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
    conf_args += --build $(DEB_HOST_GNU_TYPE)
else
    conf_args += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

static_conf_args := $(conf_args) \
	--enable-static-link \

#build: bash-build static-build check
build: before-build bash-build static-build check
build-arch: build
build-indep: build

before-build:
	dh_testdir
#	dh_update_autotools_config
	dh_autotools-dev_updateconfig
	mkdir -p stamps
	: # 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) $(LDFLAGS) $(CPPFLAGS) -o clear_console \
	    debian/clear_console.c $(termcap_lib)
endif

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

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

# ---------------------------------------------------------------------------
# build static bash

static-build:
	$(MAKE) -f debian/rules do-build-static \
	    bash_src=. \
	    build=static \
	    configure_args="$(static_conf_args)"
static-configure:
	$(MAKE) -f debian/rules do-configure-static \
	    bash_src=. \
	    build=static \
	    configure_args="$(static_conf_args)"

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

bash-doc-build: stamps/stamp-build-bash-doc
stamps/stamp-build-bash-doc:
	rm -f bash/doc/bashref.info
	$(MAKE) -C build-bash/doc info html MAN2HTML=/usr/bin/man2html
	$(MAKE) -C build-bash/doc bash.pdf bashref.pdf
	touch stamps/stamp-build-bash-doc

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

check: stamps/stamp-check
stamps/stamp-check: bash-build
	@echo BEGIN test
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	-sh debian/locale-gen
	LOCPATH=$(CURDIR)/locales \
		time $(MAKE) -C build-bash test 2>&1 | tee build-bash/test-protocol
  endif
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-*
	rm -f debian/README.Debian
	rm -rf locales
	rm -f clear_console
	dh_autotools-dev_restoreconfig
	dh_clean

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


install: bash-install

bash-install: bash-build stamps/stamp-install-bash
stamps/stamp-install-bash: before-build stamps/stamp-build-bash
	dh_testdir
	dh_testroot
	dh_prep -p$(p) -p$(p_doc) -p$(p_bins)
	dh_installdirs -p$(p) \
		bin \
		etc/skel \
		usr/share/doc/$(p)
	dh_installdirs -p$(p_doc) \
		usr/share/doc/$(p)
	dh_installdirs -p$(p_bins) \
		usr/share/doc/$(p)/examples/loadables

ifeq ($(with_gfdl),yes)
# XXXXX
#	cp -p build-bash/doc/*.info bash/doc/
endif

	: # install it
	$(MAKE) -C build-bash install \
		YACC="$(YACC)" \
		DESTDIR=$(CURDIR)/$(d)
	mv $(d)/usr/bin/bash $(d)/bin/.
	chmod 755 $(d)/usr/bin/bashbug
	$(ID) debian/bashbug.1 $(d)/usr/share/man/man1/
ifneq ($(with_gfdl),yes)
	mkdir -p $(d)/usr/share/man/man1
	cp -p bash/doc/bash.1 $(d)/usr/share/man/man1/bash.1
endif
	rm -f $(d)/usr/share/doc/bash/*.html
	rm -f $(d)/usr/share/info/*.info
	rm -f $(d)/usr/share/info/dir*

	: # extra links
	ln -sf bash $(d)/bin/rbash

	: # skeleton files
	$(ID) debian/etc.bash.bashrc $(d)/etc/bash.bashrc
	$(ID) debian/skel.bashrc $(d)/etc/skel/.bashrc
	$(ID) debian/skel.profile $(d)/etc/skel/.profile
	$(ID) debian/skel.bash_logout $(d)/etc/skel/.bash_logout

	: # clean_console
ifneq (,$(findstring $(DEB_HOST_ARCH_OS), linux freebsd))
	$(IX) clear_console $(d)/usr/bin/
	$(ID) debian/clear_console.1 $(d)/usr/share/man/man1/
endif

	: # /etc/shells integration
	$(ID) -D debian/shells.d/bash $(d)/usr/share/debianutils/shells.d/bash

	: # files for the bash-doc package

	dh_installexamples -p$(p_doc) examples/*
	rm -rf $(d_doc)/usr/share/doc/$(p)/examples/loadables
	mkdir -p $(d_doc)/usr/share/doc/$(p_doc)
	ln -sf ../$(p)/examples $(d_doc)/usr/share/doc/$(p_doc)/examples

	rm -rf $(d_doc)/usr/share/doc/$(p)/examples/obashdb
	cd $(d_doc)/usr/share/doc/$(p)/examples && chmod 755 \
		misc/aliasconv.*sh misc/cshtobash

	cd $(d_doc)/usr/share/doc/$(p)/examples && chmod 644 \
		scripts/shprompt

	: # files for the bash-builtins package
	mv $(d)/usr/include $(d_bins)/usr/.
	mv $(d)/usr/lib $(d_bins)/usr/.
	$(ID) examples/loadables/{README,*.c} \
		$(d_bins)/usr/share/doc/$(p)/examples/loadables
	$(ID) build-bash/examples/loadables/Makefile \
		$(d_bins)/usr/share/doc/$(p)/examples/loadables
	ln -sf bash $(d_bins)/usr/share/doc/$(p_bins)

	sed -i \
		-e 's@-ffile-prefix-map=[^ ]*[ ]*@@g' \
		-e 's@-fdebug-prefix-map=[^ ]*[ ]*@@g' \
		-e 's@^BUILD_DIR =.*@BUILD_DIR = /path/to/build-dir@' \
		$(d)/usr/bin/bashbug \
		$(d_bins)/usr/lib/bash/Makefile.inc \
		$(d_bins)/usr/share/doc/$(p)/examples/loadables/Makefile

#	cat debian/README stamps/stamp-patch > debian/README.Debian
	cat debian/README > debian/README.Debian

	touch stamps/stamp-install-bash

binary-doc: bash-install bash-doc-build
	dh_testdir
	dh_testroot
	mkdir -p $(d_doc)/usr/share/doc/$(p)
ifeq ($(with_gfdl),yes)
	dh_installdocs -p$(p_doc) 
	cp -p build-bash/doc/bashref.{html,pdf} $(d_doc)/usr/share/doc/$(p)/.
	mkdir -p $(d_doc)/usr/share/info
	cp -p build-bash/doc/bash.info $(d_doc)/usr/share/info/.
	dh_link -p$(p_doc) \
	    /usr/share/doc/$(p)/bashref.html /usr/share/doc/$(p_doc)/bashref.html \
	    /usr/share/doc/$(p)/bashref.pdf /usr/share/doc/$(p_doc)/bashref.pdf
else
	dh_installdocs -p$(p_doc) -X.doc-base
	rm -f $(d_doc)/usr/share/doc-base/bashref
endif
	rm -f $(d_doc)/usr/share/info/dir*
	cp -p build-bash/doc/bash.html build-bash/doc/bash.pdf \
	    $(d_doc)/usr/share/doc/$(p)/
	dh_link -p$(p_doc) \
	    /usr/share/doc/$(p)/bash.html /usr/share/doc/$(p_doc)/bash.html \
	    /usr/share/doc/$(p)/bash.pdf /usr/share/doc/$(p_doc)/bash.pdf
	dh_installchangelogs -p$(p_doc) CWRU/changelog
	dh_compress -p$(p_doc) -Xexamples -X.pdf
	dh_fixperms -p$(p_doc)
	dh_installdeb -p$(p_doc)
	dh_gencontrol -p$(p_doc)
	dh_md5sums -p$(p_doc)
	dh_builddeb -p$(p_doc)

binary-bash: bash-install
	dh_testdir
	dh_testroot
	dh_installchangelogs -p$(p)
	dh_installdocs -p$(p) \
		CHANGES NEWS COMPAT doc/INTRO POSIX \
		debian/{README.Debian,README.abs-guide,README.commands} \
		debian/inputrc.arrows
	install -D -m 644 debian/bash.overrides \
		debian/bash/usr/share/lintian/overrides/bash
	install -D -m 644 debian/bash-builtins.overrides \
		debian/bash-builtins/usr/share/lintian/overrides/bash-builtins
	dh_installman -p$(p) doc/rbash.1 debian/bash-builtins.7
	dh_installmenu -p$(p)
	dh_strip -p$(p)
	dh_compress -p$(p)
	dh_fixperms -p$(p)
	dh_shlibdeps -p$(p) -- -dPre-Depends $(d)/bin/bash
	dh_installdeb -p$(p)
	dh_gencontrol -p$(p)
	dh_md5sums -p$(p)
	dh_builddeb -p$(p)

# Even though it contains only headers and example files,
# bash-builtins is NOT arch-independent because the config.h* files
# differ on different archs.
binary-builtins: bash-install
	dh_testdir
	dh_testroot
	dh_strip -p$(p_bins)
	dh_compress -p$(p_bins) -Xexamples
	dh_fixperms -p$(p_bins)
	dh_installdeb -p$(p_bins)
	dh_gencontrol -p$(p_bins)
	dh_md5sums -p$(p_bins)
	dh_builddeb -p$(p_bins)

binary-static: static-build
	dh_testdir
	dh_testroot
	dh_prep -p$(p_stat)
	dh_installdirs -p$(p_stat) \
		bin \
		usr/share/man/man1
	cp -p build-static/bash $(d_stat)/bin/bash-static
	cp -p doc/bash.1 $(d_stat)/usr/share/man/man1/bash-static.1
	$(ID) -D debian/shells.d/bash-static $(d_stat)/usr/share/debianutils/shells.d/bash-static
	dh_installdocs -p$(p_stat)
	dh_installchangelogs -p$(p_stat)
	install -D -m 644 debian/bash-static.overrides \
		debian/bash-static/usr/share/lintian/overrides/bash-static
	dh_strip -p$(p_stat)
	dh_compress -p$(p_stat)
	dh_fixperms -p$(p_stat)
	dh_installdeb -p$(p_stat)
	dh_gencontrol -p$(p_stat) -- \
		'-Vglibc:Source=$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W libc-bin)'
	dh_md5sums -p$(p_stat)
	dh_builddeb -p$(p_stat)

binary-indep: binary-doc
binary-arch: binary-bash binary-builtins binary-static
binary:	binary-indep 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
ifneq (,$(profiled_build))
	$(MAKE) -C build-$(build) \
		CFLAGS='$(CFLAGS) -fprofile-generate' \
		YACC="$(YACC)" \
		TERMCAP_LIB="$(termcap_lib)" \
		deb_builddir=build-$(build)/ \
		$(debflags)
	-sh debian/locale-gen
	cp debian/run-my-gprof bash/tests/
	LOCPATH=$(CURDIR)/locales \
	    time $(MAKE) -C build-$(build) TESTSCRIPT=run-my-gprof test 2>&1 \
		| tee build-bash/profile-protocol
	$(MAKE) -C build-$(build) clean
	rm -f build-$(build)/lib/malloc/malloc*.gc??
	$(MAKE) -C build-$(build) \
		CFLAGS='$(CFLAGS) -fprofile-use' \
		YACC="$(YACC)" \
		TERMCAP_LIB="$(termcap_lib)" \
		deb_builddir=build-$(build)/ \
		$(debflags)
else
	$(MAKE) -C build-$(build) \
		YACC="$(YACC)" \
		TERMCAP_LIB="$(termcap_lib)" \
		deb_builddir=build-$(build)/ \
		$(debflags)
endif
ifeq ($(with_gfdl),yes)
	$(MAKE) -C build-$(build)/doc \
		bash.info
endif
	touch stamps/stamp-build-$(build)

do-configure-$(build): stamps/stamp-configure-$(build)
stamps/stamp-configure-$(build):
	dh_testdir
	rm -rf build-$(build)
	mkdir build-$(build)
	cd build-$(build) && \
	    CC="$(CC)" \
	    CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
	    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)

update-patches:
	export QUILT_PATCHES=$(patchdir); \
	export QUILT_REFRESH_ARGS="--no-timestamps --no-index -p ab"; \
	export QUILT_DIFF_ARGS="--no-timestamps --no-index -p ab"; \
	while quilt push; do quilt refresh; done

#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 xf bash-$(VERSION)*.tar.xz
#	mv bash-$(VERSION) $(bash_src)
#	rm -f bash/y.tab.?
#	cp -p /usr/share/misc/config.* $(bash_src)/.
#	cp -p /usr/share/misc/config.* $(bash_src)/support/.
#	touch stamps/stamp-unpack-$(bash_src)

remove-non-dfsg-files:
	rm -f doc/article.*
	rm -f doc/FAQ
	rm -f doc/aosa-bash*.pdf
	rm -f doc/rose94.*

.NOTPARALLEL: build
.PHONY: unpack binary binary-arch binary-indep clean \
    build bash-build static-build \
    check \
    bash-configure static-configure \
    binary-doc binary-bash binary-builtins binary-static \
    install bash-install

# Local Variables:
# mode: makefile
# end:
