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

# 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)

unpack: unpack-stamp
unpack-stamp:
	tar xjf /usr/src/toolchain/newlib-1.10.0.tar.bz2
	for i in $(CURDIR)/patches/* ; do \
		patch -p0 < $$i ; \
	done
	mv newlib* src
	mkdir build
	touch unpack-stamp

configure: configure-stamp
configure-stamp: unpack-stamp
	dh_testdir

	# Add here commands to configure the package.
	cd build && \
	../src/configure --prefix=/usr \
                         --target=m68hc11 \
                         --build=$(DEB_BUILD_GNU_TYPE) \
                         --host=$(DEB_HOST_GNU_TYPE)
	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir
	cd build && CFLAGS="-Os -Wall -mrelax" $(MAKE)
	tar cfz $(CURDIR)/example.tar.gz example
	touch build-stamp

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
 
	# Main install
	cd build && $(MAKE) install prefix=$(CURDIR)/debian/newlib-m68hc1x/usr

	# Change hardlinks to symlinks
	ln -sf libc.a $(CURDIR)/debian/newlib-m68hc1x/usr/m68hc11/lib/libg.a
	ln -sf libc.a $(CURDIR)/debian/newlib-m68hc1x/usr/m68hc11/lib/mshort/libg.a
	ln -sf libc.a $(CURDIR)/debian/newlib-m68hc1x/usr/m68hc11/lib/mshort/fshort-double/libg.a
	ln -sf libc.a $(CURDIR)/debian/newlib-m68hc1x/usr/m68hc11/lib/fshort-double/libg.a
	
	ln -sf libc.a $(CURDIR)/debian/newlib-m68hc1x/usr/m68hc11/lib/m68hc12/libg.a
	ln -sf libc.a $(CURDIR)/debian/newlib-m68hc1x/usr/m68hc11/lib/m68hc12/mshort/libg.a
	ln -sf libc.a $(CURDIR)/debian/newlib-m68hc1x/usr/m68hc11/lib/m68hc12/mshort/fshort-double/libg.a
	ln -sf libc.a $(CURDIR)/debian/newlib-m68hc1x/usr/m68hc11/lib/m68hc12/fshort-double/libg.a

	# Strip files
	find $(CURDIR)/debian/newlib-m68hc1x -name "*.o" -or -name "*.a" -exec /usr/bin/m68hc11-strip --strip-debug {} \;

	# Override file
	install -p -d -o root -g root -m 755 $(CURDIR)/debian/newlib-m68hc1x/usr/share/lintian/overrides
	install -p -o root -g root -m 644 $(CURDIR)/debian/overrides $(CURDIR)/debian/newlib-m68hc1x/usr/share/lintian/overrides/newlib-m68hc1x

clean:
	dh_testdir
	dh_testroot
	make -C example/ clean
	rm -f $(CURDIR)/example.tar.gz
	rm -rf src build patch-stamp*
	rm -f build-stamp configure-stamp unpack-stamp 

	dh_clean


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
#	dh_installexamples
#	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
	dh_installman
#	dh_installinfo
	dh_installchangelogs
	dh_link
	dh_strip --exclude=.o --exclude=.a
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	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


