#!/usr/bin/make -f
# $MirOS: contrib/hosted/tg/deb/mksh/debian/rules,v 1.89 2012/04/14 17:00:17 tg Exp $

LC_ALL:=C
export LC_ALL

# for "debian/rules get-orig-source"
ORIGTGZ_SOURCE:=	-D'2012/04/14 16:30'
ORIGTGZ_DESTINATION:=	40.9.20120414
# built from R40-stable branch: R40c+
#ORIGTGZ_SOURCE:=	-rmksh-R40stable -D'2011-11-26 18:30'
# built from R40e tag
#ORIGTGZ_SOURCE:=	-rmksh-R40e
ORIGTGZ_PRINTF:=	-rmksh-R40e
#ORIGTGZ_DESTINATION:=	40.5

# shut up build log checkers that don’t know what they are doing
# Debian #492377, #572252; as well *buntu
_shutup:= 2>&1 | sed \
	    -e 's!conftest.c:\([0-9]*\(:[0-9]*\)*\): error:!cE(\1) -!g' \
	    -e 's!conftest.c:\([0-9]*\(:[0-9]*\)*\): warning:!cW(\1) -!g' \

build-indep:

build-arch: debian/.build_stamp

debian/.build_stamp:
	dh_testdir
	sh debian/meat ${ORIGTGZ_SOURCE} ${_shutup}
	mv builddir/static/mksh builddir/static/mksh-static
	@:>$@

clean:
	dh_testdir
	-rm -f debian/.*_stamp
	-rm -rf builddir
	dh_clean

binary-indep: build-indep

binary-arch: build-arch
	dh_testdir
	dh_testroot
	if test -x "$$(which dh_prep)"; then dh_prep; else dh_clean -k; fi
	dh_installchangelogs
	dh_installdocs
	# we install this gzip’d already
	rm -f debian/mksh/usr/share/doc/mksh/README.Debian
	dh_installexamples
	dh_install
	dh_installmenu
	dh_installman
	install -m 644 dot.mkshrc debian/mksh/etc/mkshrc
	dh_link
	dh_strip
	dh_compress
	cd debian/mksh/usr/share/man/man1 && ln -sf mksh.1.gz mksh-static.1.gz
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	gzip -d <builddir/README.Debian.gz | sed \
	    -e '1,/^Details/d' -e '/^Actual/d' -e '/^]/d'

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

_ANONCVSROOT:=		_anoncvs@anoncvs.mirbsd.org:/cvs
_CVSEXPORT:=		env CVS_RSH=ssh TZ=UTC cvs -qd ${_ANONCVSROOT} export

get-orig-source:
	rm -rf $@.tmp
	mkdir $@.tmp
	# to get a Debian .orig.tar.gz we need to pull the source from CVS
	cd $@.tmp; ${_CVSEXPORT} ${ORIGTGZ_SOURCE} mksh
	rm -f $@.tmp/mksh/Makefile	# MirBSD only
	# and add an implementation of the printf shell builtin from UCB
	cd $@.tmp; ${_CVSEXPORT} ${ORIGTGZ_PRINTF} src/usr.bin/printf/printf.c
	# then mix those together, rename and pack it up
	mv $@.tmp/src/usr.bin/printf/printf.c $@.tmp/mksh/
	mv $@.tmp/mksh $@.tmp/mksh-${ORIGTGZ_DESTINATION}.orig
	cd $@.tmp; find mksh-${ORIGTGZ_DESTINATION}.orig -type f | sort | \
	    paxcpio -oC512 -Hustar -Mdist | gzip -n9 \
	    >../mksh_${ORIGTGZ_DESTINATION}.orig.tar.gz
	rm -rf $@.tmp
	# there we are...
	ls -l mksh_${ORIGTGZ_DESTINATION}.orig.tar.gz
