#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.

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

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

default: build

SHELL		= /bin/bash -e	# brace expansion used in rules file
pwd		:= $(shell pwd)

# which binary packages are built?
# The name of the source package // better: set SNAPSHOT explicitely
PKGSOURCE := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2;exit 0}')
DPKGVER   := 1

CROSS_TOOL   := h8300-hitachi-hms
#  attempt at being platform independent...
CONFIG_HOST  := ${DEB_BUILD_GNU_CPU}-pc-${DEB_BUILD_GNU_SYSTEM}-gnu

srcdir		:= $(pwd)/src
stampdir	:= stamps

unpack_stamp	:= $(stampdir)/01-unpack-stamp
configure_stamp	:= $(stampdir)/02-configure-stamp
build_stamp	:= $(stampdir)/03-build-stamp
install_stamp	:= $(stampdir)/04-install-stamp

BIN_PKGNM	:= gcc-h8300-hms
DEV_PKGNM	:= $(BIN_PKGNM)-dev
DOC_PKGNM	:= $(BIN_PKGNM)-doc

BIN_ROOT	:= $(pwd)/debian/$(BIN_PKGNM)
DEV_ROOT	:= $(pwd)/debian/$(DEV_PKGNM)
DOC_ROOT	:= $(pwd)/debian/$(DOC_PKGNM)

include debian/rules.unpack

stamp-dir:
	if [ -d $(stampdir) ]; then true; else mkdir $(stampdir); fi

configure: unpack $(configure_stamp)

$(configure_stamp): stamp-dir
	dh_testdir
	#  configure gcc build for our cross target
	$(srcdir)/configure --target=$(CROSS_TOOL) --prefix=/usr --exec-prefix=/usr/$(CROSS_TOOL) --host=$(CONFIG_HOST) --enable-languages='c,c++' --enable-target-optspace --with-headers= --with-libs=
	#  we are building without libs so adjust our CFLAGS to indicate this
	mv -f Makefile Makefile.old
	cat Makefile.old | awk '{ if ( $$1 == "CFLAGS" ) { print $$0 " -Dinhibit_libc"; } else { print $0; } }' >Makefile
	rm -f Makefile.old
	touch $@

build: configure $(build_stamp)

$(build_stamp): stamp-dir
	dh_testdir
	#  add our binutils-h8300-hms dir so we can build!
	#   (since we can't use curr env)
	export PATH=/usr/h8300-hitachi-hms/bin:$$PATH;$(MAKE) cross
	touch $@

clean:
	dh_testdir
	dh_testroot
	# Add here commands to clean up after the build process.
	-if [ -f Makefile ]; then $(MAKE) clean; fi
	-rm -rf Makefile config.cache config.status etc gcc \
	        h8300-hitachi-hms libiberty mt-frag src stamps \
	        texinfo 
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/gcc-h8300-hms.
	$(MAKE) prefix=$(BIN_ROOT)/usr mandir=$(BIN_ROOT)/usr/share/man infodir=$(BIN_ROOT)/usr/share/info exec_prefix=$(BIN_ROOT)/usr/$(CROSS_TOOL) install 


# Build architecture-independent files here.
#  no activity here by default
binary-indep: build install

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	# Need this version of debhelper for DH_OPTIONS to work.
	dh_testversion 1.1.17
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
	#  we DO NOT want any info docs in this cross-gcc package
	rm -rf $(BIN_ROOT)/usr/share/info
#	dh_installexamples
#	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
#	dh_installcron
#	dh_instmallmanpages
#	dh_installinfo
#	dh_undocumented
	dh_installchangelogs 
	dh_strip -Xlibgcc.a -Xlibiberty.a
	dh_link
	dh_compress 
	dh_fixperms
#	dh_suidregister
	dh_installdeb
#	dh_makeshlibs
#	dh_perl
	dh_shlibdeps 
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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