#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# This is the debhelper compatability version to use.
export DH_COMPAT=2


DISTNAME=cris-dist
VERS=1.20

URL=ftp://ftp.axis.se/pub/axis/tools/cris/compiler-kit
TBEXT=tar.gz

SOURCE0=$(DISTNAME)-$(VERS).$(TBEXT)
SOURCE1=$(DISTNAME)-glibc-$(VERS).$(TBEXT)
SOURCE2=$(DISTNAME)-linux-headers-$(VERS).$(TBEXT)

THERE=/tmp

PREFIX=/usr/local
PLIBDIR=$(PREFIX)/cris/lib
BINDIR=$(PREFIX)/cris
MANDIR=$(PREFIX)/man/man1
INFODIR=$(PREFIX)/info
JUNKDIR=./junk
BUILDPREFIX=debian/$(DISTNAME)

download: download-stamp
download-stamp:
	@( \
		SUCCESS=true; \
		printf "\nExpecting to find the following tar-ball(s)"; \
		printf " in $(THERE):\n"; \
		for TB in $(SOURCE0) $(SOURCE1) $(SOURCE2); do \
			printf "\t$$TB ..."; \
			if [ -e $(THERE)/$$TB ]; then \
				printf " found.\n"; \
			else \
				printf " missing.\n"; \
				SUCCESS=false; \
			fi; \
		done; \
		if [ $$SUCCESS = false ]; then \
			printf "\nDownload the missing tar-ball(s) to the"; \
			printf " $(THERE) dir. Command line example:\n"; \
			printf "\twget -q $(URL)/$(SOURCE0)\n\n"; \
			exit 1; \
		fi; \
	)
	@touch download-stamp


unpack: download unpack-stamp
unpack-stamp:
	@( \
		printf "\nUnpacking:\n"; \
		printf "\t$(THERE)/$(SOURCE0) ..."; \
		tar xzf $(THERE)/$(SOURCE0); \
		printf " done.\n"; \
		printf "\t$(THERE)/$(SOURCE1) ..."; \
		tar xzfC $(THERE)/$(SOURCE1) $(DISTNAME)-$(VERS); \
		printf " done.\n"; \
		printf "\t$(THERE)/$(SOURCE2) ..."; \
		tar xzfC $(THERE)/$(SOURCE2) $(DISTNAME)-$(VERS); \
		printf " done.\n"; \
		printf " done.\n"; \
		mv -f $(DISTNAME)-$(VERS)/* .; \
		rmdir $(DISTNAME)-$(VERS); \
	)
	@touch unpack-stamp


build: unpack build-stamp
build-stamp:
	dh_testdir
	./install-cris-tools \
		$(PLIBDIR) \
		$(BINDIR) \
		$(MANDIR) \
		$(INFODIR) \
		$(JUNKDIR) \
		$(BUILDPREFIX) < /dev/null
	@touch build-stamp


clean:
	dh_testdir
	dh_testroot
	@printf "\nThe source tar-ball(s) in $(THERE) will not be removed.\n\n"
	@-rm -rf $(JUNKDIR)
	@-rm -rf $(DISTNAME)-$(VERS)
	@-rm -f *-stamp
	@-( \
		FF=`ls -1 | egrep -v "debian|$${DISTNAME}\.tar\.gz"`; \
		if [ "$$FF" ]; then \
			for F in $$FF; do rm -fr $$F; done; \
		fi; \
	)
	dh_clean


binary-indep: build


binary-arch: build
	dh_testdir
	dh_testroot
	dh_installdirs

#	Keep this verification scrpt for now
	debian/check-symlinks $(BUILDPREFIX) $(BINDIR)

#	Can't use dh_installinfo to do this, but there's always another way ...
	sed	-e "s,#PACKAGE#,$(DISTNAME),g" \
		-e "s,#INFODIR#,$(INFODIR),g" \
		debian/postinst.template > debian/postinst
	sed	-e "s,#PACKAGE#,$(DISTNAME),g" \
		-e "s,#INFODIR#,$(INFODIR),g" \
		debian/prerm.template > debian/prerm

	dh_installdocs NEWS README TODO
	dh_installchangelogs ChangeLog

#	We don't need this because the install script is doing that
#	and probably needs the control over what gets stripped.
#	dh_strip

	dh_compress
	dh_installdeb

	# NOTE: Careful here! Make sure the exclusion below is still valid!

#	dh_shlibdeps will detect .so libraries, will run ldd on them.
#	ldd will exit with status 1 (not a dynamic executable) and
#	everything will go down the drains.
#	Stop that pain by excluding the .so libraries.
	dh_shlibdeps -X$(BUILDPREFIX)$(PLIBDIR)/gcc-lib/cris/2.96/linux/

#	Tweak the screwed up dependencies dh_shlibdeps (dpkg-shlibdeps)
#	dreams about.
	(SV=`find debian -maxdepth 1 -name "*substvars*"`; \
	for S in $$SV; do \
		if [ -e $$S ]; \
		then \
			mv $$S $$S.hide; \
			cat $$S.hide | sed 's#axis-cris-dist-bin, ##' > $$S; \
			rm -f $$S.hide; \
		fi; \
	done;)

	dh_gencontrol
	dh_makeshlibs
	dh_md5sums
	dh_builddeb


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