#!/usr/bin/make -f
# $MirOS: contrib/hosted/tg/deb/mksh/debian/rules,v 1.103 2012/06/30 17:24:36 tg Exp $

LC_ALL:=C
export LC_ALL

# for "debian/rules get-orig-source"
ORIGTGZ_SOURCE:=	-D'2012/06/30 16:00'
ORIGTGZ_DESTINATION:=	40.9.20120630
# 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:
	# goodbye dh_testdir
	test -f sh.h
	test -x debian/rules
	-rm -f $@
	(sh debian/meat ${ORIGTGZ_SOURCE} && :>$@) ${_shutup}
	test -e $@

clean:
	# goodbye dh_testdir
	test -f sh.h
	test -x debian/rules
	-rm -rf builddir
	-rm -rf debian/.*_stamp debian/B debian/mksh debian/pdksh
	# goodbye dh_clean
	-rm -f debian/files debian/substvars

binary-indep: build-indep
	# goodbye dh_testdir
	test -f sh.h
	test -x debian/rules
	# goodbye dh_prep
	-rm -f debian/substvars
	# goodbye dh_installdirs
	-rm -rf debian/pdksh
	mkdir -p debian/pdksh/bin debian/pdksh/usr/share/doc \
	    debian/pdksh/usr/share/lintian/overrides \
	    debian/pdksh/usr/share/man/man1
	# goodbye dh_installchangelogs
	# goodbye dh_installdocs
	ln -s mksh debian/pdksh/usr/share/doc/pdksh
	# goodbye dh_lintian
	cp -a debian/pdksh.lintian \
	    debian/pdksh/usr/share/lintian/overrides/pdksh
	# goodbye dh_link
	ln -s lksh debian/pdksh/bin/pdksh
	ln -s lksh.1.gz debian/pdksh/usr/share/man/man1/pdksh.1.gz
	# goodbye dh_fixperms
	chmod 644 $$(find debian/pdksh -type f)
	chmod 755 $$(find debian/pdksh -type d)
	# goodbye dh_installdeb
	-rm -rf debian/B
	mkdir -p debian/pdksh/DEBIAN debian/B/c
	set -e; for f in postinst preinst; do \
		cp -a debian/pdksh.$$f debian/B/c/$$f; \
	done
	# goodbye dh_gencontrol
	dpkg-gencontrol -ppdksh -Pdebian/pdksh -isp
	mv debian/pdksh/DEBIAN/control debian/B/c/
	rm -rf debian/pdksh/DEBIAN
	# goodbye dh_md5sums
	(cd debian/pdksh && find . -type f | sed s,^./,, | sort | \
	    xargs md5sum) >debian/B/c/md5sums
	# goodbye dh_builddeb
	(cd debian/pdksh && find . | sort | paxcpio \
	    -oC512 -Hustar -Minodes -Mlinks -Muidgid -Mgslash) | \
	    gzip -n9 >debian/B/data.tar.gz
	cd debian/B/c && chmod 644 * && chmod 755 postinst preinst
	(cd debian/B/c && find . | sort | paxcpio \
	    -oC512 -Hustar -Minodes -Mlinks -Muidgid -Mgslash) | \
	    gzip -n9 >debian/B/control.tar.gz
	echo 2.0 >debian/B/debian-binary
	set -e; while read fn rest; do \
		case $$fn in \
		pdksh_*) ;; \
		*) continue ;; \
		esac; \
		cd debian/B && \
		    paxtar -A -M dist -cf "../../../$$fn" \
		    debian-binary control.tar.gz data.tar.gz; \
		break; \
	done <debian/files

binary-arch: build-arch
	# goodbye dh_testdir
	test -f sh.h
	test -x debian/rules
	# goodbye dh_prep
	-rm -f debian/substvars
	# goodbye dh_installdirs
	-rm -rf debian/mksh
	mkdir -p debian/mksh/bin debian/mksh/etc/skel \
	    debian/mksh/usr/share/doc/mksh/examples \
	    debian/mksh/usr/share/lintian/overrides \
	    debian/mksh/usr/share/man/man1 \
	    debian/mksh/usr/share/menu debian/mksh/usr/share/pixmaps
	# goodbye dh_installchangelogs
	cp -a debian/changelog debian/mksh/usr/share/doc/mksh/changelog.Debian
	gzip -n9 debian/mksh/usr/share/doc/mksh/changelog.Debian
	# goodbye dh_installdocs
	cp -a debian/copyright debian/mksh/usr/share/doc/mksh/copyright
	cp builddir/README.Debian.gz debian/mksh/usr/share/doc/mksh/
	cp -a debian/mtest.t check.pl check.t debian/mksh/usr/share/doc/mksh/
	gzip -n9 debian/mksh/usr/share/doc/mksh/mtest.t
	gzip -n9 debian/mksh/usr/share/doc/mksh/check.pl
	gzip -n9 debian/mksh/usr/share/doc/mksh/check.t
	# goodbye dh_installexamples
	cp -a dot.mkshrc debian/mksh/usr/share/doc/mksh/examples/
	gzip -n9 debian/mksh/usr/share/doc/mksh/examples/dot.mkshrc
	# goodbye dh_install
	cp builddir/full/mksh debian/mksh/bin/mksh
	cp builddir/static/mksh debian/mksh/bin/mksh-static
	cp builddir/legacy/lksh debian/mksh/bin/lksh
	cp -a dot.mkshrc debian/mksh/etc/mkshrc
	cp -a debian/.mkshrc debian/mksh/etc/skel/
	cp -a debian/mksh*.xpm debian/mksh/usr/share/pixmaps/
	# goodbye dh_installmenu
	cp -a debian/mksh.menu debian/mksh/usr/share/menu/mksh
	# goodbye dh_installman
	cp -a debian/lksh.1 mksh.1 debian/mksh/usr/share/man/man1/
	gzip -n9 debian/mksh/usr/share/man/man1/lksh.1
	gzip -n9 debian/mksh/usr/share/man/man1/mksh.1
	ln -sf mksh.1.gz debian/mksh/usr/share/man/man1/mksh-static.1.gz
	# goodbye dh_lintian
	cp -a debian/mksh.lintian \
	    debian/mksh/usr/share/lintian/overrides/mksh
	# goodbye dh_strip
ifeq (,$(findstring nostrip,${DEB_BUILD_OPTIONS}))
	strip -s -R .note -R .comment \
	    debian/mksh/bin/lksh debian/mksh/bin/mksh \
	    debian/mksh/bin/mksh-static
endif
	# goodbye dh_fixperms
	chmod 644 $$(find debian/mksh -type f)
	chmod 755 $$(find debian/mksh -type d) \
	    debian/mksh/bin/lksh debian/mksh/bin/mksh \
	    debian/mksh/bin/mksh-static
	# goodbye dh_installdeb
	-rm -rf debian/B
	mkdir -p debian/mksh/DEBIAN debian/B/c
	set -e; for f in conffiles postinst postrm preinst prerm; do \
		cp -a debian/mksh.$$f debian/B/c/$$f; \
	done
	# goodbye dh_shlibdeps
	dpkg-shlibdeps -edebian/mksh/bin/lksh -edebian/mksh/bin/mksh
	# goodbye dh_gencontrol
	dpkg-gencontrol -pmksh -Pdebian/mksh -isp
	mv debian/mksh/DEBIAN/control debian/B/c/
	rm -rf debian/mksh/DEBIAN
	# goodbye dh_md5sums
	cfcmd="grep -Fxv"; while read n; do \
		cfcmd="$$cfcmd -e .$$n"; \
	done <debian/B/c/conffiles; (cd debian/mksh && find . -type f | \
	    $$cfcmd | sed s,^./,, | sort | xargs md5sum) >debian/B/c/md5sums
	# goodbye dh_builddeb
	(cd debian/mksh && find . | sort | paxcpio \
	    -oC512 -Hustar -Minodes -Mlinks -Muidgid -Mgslash) | \
	    gzip -n9 >debian/B/data.tar.gz
	cd debian/B/c && chmod 644 * && chmod 755 postinst postrm preinst prerm
	(cd debian/B/c && find . | sort | paxcpio \
	    -oC512 -Hustar -Minodes -Mlinks -Muidgid -Mgslash) | \
	    gzip -n9 >debian/B/control.tar.gz
	echo 2.0 >debian/B/debian-binary
	set -e; while read fn rest; do \
		case $$fn in \
		mksh_*) ;; \
		*) continue ;; \
		esac; \
		cd debian/B && \
		    paxtar -A -M dist -cf "../../../$$fn" \
		    debian-binary control.tar.gz data.tar.gz; \
		break; \
	done <debian/files
	#-
	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
