#!/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.62

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)

HERE=$(shell pwd)
THERE=/tmp

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

GCC_MKFILE=gnu-toplev/gcc/Makefile.in
SKIP_SHLIBS=/cris/gcc-lib/cris/3.2.1/linux/

LMKFLAGS=-s

LCC=gcc

#//#LGCC=$(wildcard /usr/bin/gcc-2.95)
#//#ifeq ($(LGCC),)
#//#	LGCC=/usr/bin/gcc
#//#endif
#//#LCC += $(LGCC)


checkprepare: checkprepare-stamp
checkprepare-stamp:
	@( \
		debian/run-me-as-root check || { \
			echo -e "\n\n*** As ROOT, you need to first either:"; \
			echo -en "***	 run "; \
			echo -e "\"debian/run-me-as-root symlink\""; \
			echo -e "*** or:"; \
			echo -en "***	 manually move away the "; \
			echo -e "/usr/local/{cris,info,man} directories to"; \
			echo -en "***	 /usr/local/{cris,info,man}.orig and "; \
			echo -e "create the symlinks\n\n"; \
			debian/run-me-as-root -h; \
			exit 1; \
		} \
	)
	@touch checkprepare-stamp


download: checkprepare 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

#	These lines added because gcc 3.2 does no longer support a build
#	'prefix' (which is PITA).
#	This hack must be prepended by another hack. We need to replace
#	the real cris, info and man directories under /usr/local with
#	symlinks pointing to the ones created here below.
	@( \
		[ -d "$(BUILDPREFIX)" ] || mkdir $(BUILDPREFIX); \
		[ -d "$(BUILDPREFIX)$(BINDIR)" ]  || \
			mkdir -p $(BUILDPREFIX)$(BINDIR); \
		[ -d "$(BUILDPREFIX)$(INFODIR)" ] || \
			mkdir -p $(BUILDPREFIX)$(INFODIR); \
		[ -d "$(BUILDPREFIX)$(MANDIR)" ] || \
			mkdir -p $(BUILDPREFIX)$(MANDIR); \
	)

#	Sanitize Makefile.in 'install-info --dir-file=', partly due to
#	a bug in Debian's install-info flavour but also because that info
#	directory file will be waisted anyway.
	@( \
		if [ -f $(GCC_MKFILE) ];then \
			mv $(GCC_MKFILE) $(GCC_MKFILE).original; \
			sed -e 's,install-info[[:space:]]\+--dir-file=[^;]\+;,true;,' \
				$(GCC_MKFILE).original > $(GCC_MKFILE); \
		fi; \
	)

#	We save a few minutes by letting make run a few parallel jobs.
#	Build time:
#	  about 40-45 min on an "Intel(R) Pentium(R) 4 CPU 1.70GHz"
#	  around 80 min on an "AMD Athlon(tm) Processor 1.0 GHz"
	MAKEFLAGS="$(LMKFLAGS)" CC="$(LCC)" \
		nice -n 19 ./install-cris-tools \
		$(BINDIR) \
		$(MANDIR) \
		$(INFODIR) \
		$(JUNKDIR) < /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 "CVS|debian"`; \
		if [ "$$FF" ]; then \
			for F in $$FF; do rm -fr $$F; done; \
		fi; \
		rm -f debian/postinst debian/prerm; \
	)
	dh_clean


binary-indep: build


binary-arch: build
	dh_testdir
	dh_testroot
	dh_installdirs

#	Keep this verification script for now
#	debian/check-symlinks $(BUILDPREFIX) $(BINDIR)
#	Another hack related to gcc 3.2
	debian/check-symlinks / $(BINDIR)

	# Cleanup the info 'dir' file generated under the build process.
	# When installed, the package will generate that stuff.
	-rm -v -f $(BUILDPREFIX)$(INFODIR)/dir

#	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 it's done under the build process,
#	and probably that 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.
#	But this needs a hack, because dh_shlibdeps excludes only files
#	located in the 'debian/tmp' directory (only woody?), and we need
#	to exclude stuff in $(BUILDPREFIX).
	@( \
		mv $(BUILDPREFIX) debian/tmp; \
		ln -s debian/tmp $(BUILDPREFIX); \
	)

#	Exclude .../cris/gcc-lib/cris/3.2.1/linux/ from shared libraries list
#	(cris architecture specific).
	dh_shlibdeps -X$(SKIP_SHLIBS)
	@( \
		rm -f $(BUILDPREFIX); \
		mv debian/tmp $(BUILDPREFIX); \
	)

#	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 -X$(SKIP_SHLIBS) -X/cris-axis-linux-gnu/lib/
	dh_md5sums
	dh_builddeb

	@( \
		echo -en "\n\n*** Don't forget to run "; \
		echo -e "\"debian/run-me-as-root unlink\" as ROOT or"; \
		echo -en "*** manually restore the order under "; \
		echo "/usr/local"; \
	)


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