#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

%:
	dh $@ --with autotools-dev

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_BUILD_GNU_TYPE      := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE       := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

CFLAGS = $(shell dpkg-buildflags --get CFLAGS) -U_FORTIFY_SOURCE

TARGET = h8300-hitachi-coff

# TODO package name should be changed in the future
p_cross = binutils-h8300-hms

override_dh_auto_clean:
	dh_clean
	$(RM) -r builddir-$(TARGET)

override_dh_auto_configure:
	mkdir builddir-$(TARGET)
	cd builddir-$(TARGET) && \
	    env CC="$(CC)" CFLAGS="$(CFLAGS)" ../configure --prefix=/usr \
                         --infodir=/usr/share/info \
                         --mandir=/usr/share/man \
	        							 --build=$(DEB_BUILD_GNU_TYPE) \
												 --host=$(DEB_HOST_GNU_TYPE) \
												 --target=$(TARGET) 

override_dh_auto_build:
	cd builddir-$(TARGET) && $(MAKE)

override_dh_auto_install:
	# Add here commands to install the package into debian/$(p_cross)
	cd builddir-$(TARGET) && \
		$(MAKE) install prefix=$(CURDIR)/debian/$(p_cross)/usr \
		                mandir=$(CURDIR)/debian/$(p_cross)/usr/share/man \
										infodir=$(CURDIR)/debian/$(p_cross)/usr/share/info
	
	for i in $(CURDIR)/debian/$(p_cross)/usr/bin/$(TARGET)-* ; do \
		ln -s `basename $$i` $(CURDIR)/debian/$(p_cross)/usr/bin/h8300-hms-`echo $$i | sed 's/.*$(TARGET)-//'` ; \
		ln -s `basename $$i`.1 $(CURDIR)/debian/$(p_cross)/usr/share/man/man1/h8300-hms-`echo $$i | sed 's/.*$(TARGET)-//'`.1 ; \
	done
	
	# Files also in main binutils pkg
	rm -rf $(CURDIR)/debian/$(p_cross)/usr/include
	rm -rf $(CURDIR)/debian/$(p_cross)/usr/lib
	rm -rf $(CURDIR)/debian/$(p_cross)/usr/share/info
	rm -rf $(CURDIR)/debian/$(p_cross)/usr/share/locale

	# install documentation
	install -p -o root -g root -m 644 binutils/NEWS \
		$(CURDIR)/debian/$(p_cross)/usr/share/doc/$(p_cross)/NEWS
	install -p -o root -g root -m 644 binutils/MAINTAINERS \
		$(CURDIR)/debian/$(p_cross)/usr/share/doc/$(p_cross)/MAINTAINERS
	for pkg in bfd gas gprof ld; do \
		for f in ChangeLog NEWS README PORTING MAINTAINERS TODO ; do \
			[ -f $$pkg/$$f ] || continue ; \
			install -p -o root -g root -m 644 $$pkg/$$f \
				$(CURDIR)/debian/$(p_cross)/usr/share/doc/$(p_cross)/$$pkg/$$f ; \
		done ; \
	done
	
	# Override files
	install -p -o root -g root -m 644 $(CURDIR)/debian/overrides.lintian \
		$(CURDIR)/debian/$(p_cross)/usr/share/lintian/overrides/$(p_cross)

