#!/usr/bin/make -f

# for "debian/rules get-orig-source"
ORIGTGZ_DATE:=          '2016-04-02 18:30Z'
ORIGTGZ_SOURCE:=	-D${ORIGTGZ_DATE}
ORIGTGZ_DESTINATION:=	0.34~cvs20160402.1

# determine whether we are cross-compiling
DEB_BUILD_GNU_TYPE:=$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE:=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifeq (${DEB_BUILD_GNU_TYPE},${DEB_HOST_GNU_TYPE})
CROSS:=0
else
CROSS:=1
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  DEBUG =DEBUG=1
else
  DEBUG =
endif

ARCH ?=$(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq ($(ARCH),amd64)
  ARCH =x86_64
else
ifeq ($(ARCH),powerpc)
  ARCH =ppc
else
ifeq ($(ARCH),powerpcspe)
  ARCH =ppc
else
ifeq ($(ARCH),hppa)
  ARCH =parisc
else
ifneq (,$(findstring arm,$(ARCH)))
  ARCH =arm
# DEBUG =DEBUG=1
endif
endif
endif
endif
endif

VERSION =$(shell head -n1 debian/changelog |sed -e 's/.*(\(.*\)).*/\1/')

%:
	dh $@

override_dh_auto_configure:
override_dh_auto_build:
	@# See Debian bug #523086
	mv -f parisc/strstr.S parisc/strstr.S.disabled
	dh_auto_build -- prefix=/usr/lib/diet \
		MYARCH='$(ARCH)' VERSION='$(VERSION)' $(DEBUG)
ifeq (${CROSS},0)
	bin-'$(ARCH)'/diet -v -Os gcc -nostdinc -o ttt test/select.c
	./ttt
	rm -f ttt
	bin-'$(ARCH)'/diet -v -Os gcc -nostdinc -static -o ttt debian/test/tc523086.c
	./ttt
	rm -f ttt
	bin-'$(ARCH)'/diet -v -Os gcc -nostdinc -static -o ttt debian/test/t-ascii1.c
	./ttt
	rm -f ttt
endif

override_dh_auto_clean:
	dh_auto_clean -- MYARCH='$(ARCH)'
	rm -f debian/dietlibc-dev.postinst debian/dietlibc-dev.prerm
	@# See Debian bug #523086
	[ ! -f parisc/strstr.S.disabled ] || \
		mv -f parisc/strstr.S.disabled parisc/strstr.S

override_dh_auto_install:
	mkdir -p debian/tmp/etc
	dh_auto_install -- prefix=/usr/lib/diet MYARCH='$(ARCH)' VERSION='$(VERSION)' $(DEBUG)
	sed -e 's/|ARCH|/$(ARCH)/g' <debian/dietlibc-dev.postinst.in \
	  >debian/dietlibc-dev.postinst
	sed -e 's/|ARCH|/$(ARCH)/g' <debian/dietlibc-dev.prerm.in \
	  >debian/dietlibc-dev.prerm

override_dh_strip:
	@# Don't generate automatic debug symbol packages, because
	@# dietlibc's Makefile currently already strips on an
	@# individual object file level to save as much space as
	@# possible. TODO: investigate if we can achieve the same
	@# resulting size after dh_strip without sacrificing debug
	@# symbol support. (In that case, reenable them.)
ifeq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	dh_strip --no-automatic-dbgsym
else
	dh_strip
endif

_ANONCVSROOT:=		:pserver:cvs@cvs.fefe.de:/cvs
_CVSEXPORT:=		env CVS_RSH=ssh TZ=UTC cvs -qd ${_ANONCVSROOT} -z9 export
_RESET_TIMES:=          find dietlibc-${ORIGTGZ_DESTINATION} -type f | xargs touch -d${ORIGTGZ_DATE}
_PACK:=			find dietlibc-${ORIGTGZ_DESTINATION} -type f | LC_ALL=C.UTF-8 sort | \
			    paxcpio -oC512 -Hustar -Mdist
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} dietlibc
	# fixup files with CR-LF line endings
	cd $@.tmp; find dietlibc -type f -print0 | \
	    xargs -0 perl -pi -e 's/\r$$//' --
	# then rename and pack it up
	mv $@.tmp/dietlibc $@.tmp/dietlibc-${ORIGTGZ_DESTINATION}
	cd $@.tmp; ${_RESET_TIMES}
	cd $@.tmp; ${_PACK} | xz -7 >../dietlibc_${ORIGTGZ_DESTINATION}.orig.tar.xz
	rm -rf $@.tmp
	# there we are...
	ls -l dietlibc_${ORIGTGZ_DESTINATION}.orig.tar.xz
