#!/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/gcc.tar.bz2
	for i in $(CURDIR)/patches/* ; do \
		patch -p0 < $$i ; \
	done
	mv gcc-* 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 \
                         --infodir=/usr/share/info \
                         --mandir=/usr/share/man \
                         --with-gnu-as \
                         --with-gnu-ld \
                         --enable-nls \
                         --without-included-gettext \
                         --disable-checking \
                         --enable-languages=c,c++ \
                         --build=$(DEB_BUILD_GNU_TYPE) \
                         --host=$(DEB_HOST_GNU_TYPE) \
                         --target=m68hc11
	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir
	cd build && $(MAKE)
	tar cfz $(CURDIR)/example.tar.gz example
	touch build-stamp

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/gcc-m68hc1x
	cd build && $(MAKE) install \
            prefix=$(CURDIR)/debian/gcc-m68hc1x/usr \
            infodir=$(CURDIR)/debian/gcc-m68hc1x/usr/share/info \
            mandir=$(CURDIR)/debian/gcc-m68hc1x/usr/share/man 
	    
	# Remove uneeded hardlink
	rm $(CURDIR)/debian/gcc-m68hc1x/usr/bin/m68hc11-gcc-3.3.3*
	
	# Change hardlinks to symlinks
	ln -sf m68hc11-c++ $(CURDIR)/debian/gcc-m68hc1x/usr/bin/m68hc11-g++
  
  	# Rename manpages and create symlinks
	rm $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc11-g++.1
	mv $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/cpp.1 $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc11-cpp.1
	mv $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/gcov.1 $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc11-gcov.1
	ln -s m68hc11-gcc.1.gz $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc11-g++.1.gz
	ln -s m68hc11-gcc.1.gz $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc11-c++.1.gz

	# Also create m68hc12-*
	ln -s m68hc11-gccbug $(CURDIR)/debian/gcc-m68hc1x/usr/bin/m68hc12-gccbug
	ln -s m68hc11-gcov $(CURDIR)/debian/gcc-m68hc1x/usr/bin/m68hc12-gcov
	ln -s m68hc11-cpp $(CURDIR)/debian/gcc-m68hc1x/usr/bin/m68hc12-cpp
	cp debian/m68hc12-gcc $(CURDIR)/debian/gcc-m68hc1x/usr/bin
	cp debian/m68hc12-c++ $(CURDIR)/debian/gcc-m68hc1x/usr/bin
	ln -s m68hc12-c++ $(CURDIR)/debian/gcc-m68hc1x/usr/bin/m68hc12-g++
	ln -s m68hc11-cpp.1.gz $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc12-cpp.1.gz
	ln -s m68hc11-gcc.1.gz $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc12-gcc.1.gz
	ln -s m68hc11-g++.1.gz $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc12-c++.1.gz
	ln -s m68hc11-g++.1.gz $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc12-g++.1.gz
	ln -s m68hc11-gcov.1.gz $(CURDIR)/debian/gcc-m68hc1x/usr/share/man/man1/m68hc12-gcov.1.gz

	# Files also in main gcc-3.3 pkg
	cd $(CURDIR)/debian/gcc-m68hc1x/usr/ && \
		rm -rf share/man/man7/fsf-funding.7* \
		     share/man/man7/gfdl.7* \
		     share/man/man7/gpl.7* \
		     share/locale \
		     share/info \
		     lib/libiberty.a

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

	# Override file
	install -p -o root -g root -m 644 $(CURDIR)/debian/overrides $(CURDIR)/debian/gcc-m68hc1x/usr/share/lintian/overrides/gcc-m68hc1x
				
clean:
	dh_testdir
	dh_testroot
	make -C example/ clean
	rm -f $(CURDIR)/example.tar.gz
	rm -rf src build
	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
