#!/usr/bin/make -f
#
# debian/rules file for the hermit boot loader/downloader.
#

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

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

package-name := hermit-at
package-dir := $(shell pwd)/debian/$(package-name)
install-prefix := $(package-dir)/usr
install-opt := INSTALLPREFIX=$(install-prefix)

configure:
	dh_testdir

build: configure
	dh_testdir
	$(MAKE)

clean:
	dh_testdir
	dh_testroot
	$(MAKE) scrub
	$(MAKE) TARGET=armadillo scrub
	$(MAKE) TARGET=armadilloj scrub 
	$(MAKE) TARGET=armadillo9 scrub
	$(MAKE) TARGET=armadillo2x0 scrub
	$(MAKE) TARGET=armadillo3x0 scrub
	$(MAKE) TARGET=armadillo5x0 scrub
	$(MAKE) TARGET=suzaku scrub
	dh_clean

#
# Yes, this really does build (part of) the package two additional times.
#
install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs usr/{lib,bin} usr/lib/hermit
	$(MAKE) install $(install-opt) MANPREFIX=$(install-prefix)/share/man
	$(MAKE) scrub

	t=armadillo ;                                                      \
	for i in none ttyAM1 notty boot ;                                  \
	do                                                                 \
		if [ "none" = "$$i" ] ; then                               \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=    install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		else                                                       \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=$$i install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		fi;                                                        \
	done

	t=armadilloj ;                                                     \
	for i in ctype ;                                                   \
	do                                                                 \
		if [ "none" = "$$i" ] ; then                               \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=    install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		else                                                       \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=$$i install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		fi;                                                        \
	done

	t=armadillo9 ;                                                     \
	for i in none eth ttyAM1 notty boot boot-eth ;                     \
	do                                                                 \
		if [ "none" = "$$i" ] ; then                               \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=    install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		else                                                       \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=$$i install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		fi;                                                        \
	done

	t=armadillo2x0 ;                                                   \
	for i in none eth ttyAM1 notty boot boot-eth ;                     \
	do                                                                 \
		if [ "none" = "$$i" ] ; then                               \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=    install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		else                                                       \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=$$i install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		fi;                                                        \
	done

	t=armadillo3x0 ;                                                   \
	for i in none eth ttyAM1 notty boot boot-eth ;                     \
	do                                                                 \
		if [ "none" = "$$i" ] ; then                               \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=    install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		else                                                       \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=$$i install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		fi;                                                        \
	done

	t=armadillo5x0 ;                                                   \
	for i in none fx zero boot ;                                       \
	do                                                                 \
		if [ "none" = "$$i" ] ; then                               \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=    install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		else                                                       \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=$$i install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		fi;                                                        \
	done

	t=suzaku ;                                                         \
	for i in microblaze powerpc ;                                      \
	do                                                                 \
		if [ "none" = "$$i" ] ; then                               \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=    install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		else                                                       \
		$(MAKE) $(install-opt) TARGET=$$t PROFILE=$$i install-$$t; \
		$(MAKE) $(install-opt) TARGET=$$t scrub;                   \
		fi;                                                        \
	done

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman doc/hermit.1
#	dh_installchangelogs ChangeLog
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# 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
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdocs README doc/README.*
	dh_installman doc/hermit.1
#	dh_installchangelogs ChangeLog
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
#	dh_makeshlibs
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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