#!/usr/bin/make -f
# based on the sample debian/rules file for GNU hello by Ian Jackson.

#export DH_VERBOSE=1

pkg = atari-bootstrap
tmp = debian/tmp
lib = $(tmp)/usr/lib/$(pkg)
sbin = $(tmp)/usr/sbin

# The binaries are only rebuilt if a cross compiler is available; otherwise,
# the precompiled binaries ataboot{,-bootp} are used
# If the binaries are build on a m68k system, /usr/src/linux is expected to
# point to newest kernel headers (>= 2.1.42 required!)
# If cross compiling on a non-m68k system, the complicated sed command
# determines the location of kernel headers from the default include dir of
# m68k-linux-gcc. It is expected that include/linux there is a *symlink* into
# the kernel source dir.
build:
	$(checkdir)
	@set -e; if type -p m68k-mint-gcc >/dev/null; then \
		echo "m68k-mint-gcc cross compiler available, building bootstrap"; \
		inc=/usr/src/linux/include; \
		if [ `arch` != "m68k" ] && type -p m68k-linux-gcc >/dev/null; then \
			inc=`cat /dev/null | m68k-linux-gcc -v -E - 2>&1 | \
			sed -n '/starts here:$$/,/&End of search/s/^ \(\/.*\)/\1/p' | \
			tail -1`/linux/..; \
		fi; \
		$(MAKE) -C src KERNELINC=$$inc; \
		mv src/bootstrap ataboot; \
		$(MAKE) -C src clean; \
		$(MAKE) -C src USE_BOOTP=y KERNELINC=$$inc; \
		mv src/bootstrap ataboot-bootp; \
		$(MAKE) -C rawwrite; \
		mv rawwrite/rawwrite.ttp rawwrite.ttp; \
	else \
		echo "No cross compiler available -- not rebuilding binaries"; \
	fi
	touch build

clean:
	$(checkdir)
	-rm -f build
	$(MAKE) -C src clean
	$(MAKE) -C rawwrite clean
	if [ ! -f ataboot -o ! -f ataboot-bootp -o ! -f rawwrite.ttp ]; then \
		echo "Precompiled binaries don't exist!"; \
		echo "Do a make in toplevel dir on a machine with m68k-mint-gcc first!"; \
		exit 1; \
	fi
	-rm -f img*Atari
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp debian/files* core debian/substvars

binary-indep: build
	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	install -d $(lib) $(sbin)
	# install bootstraps
	install -m 644 ataboot $(lib)/ataboot.ttp
	install -m 644 ataboot-bootp $(lib)/ataboot-bootp.ttp
	gzip -9f $(lib)/ataboot*
	# install rawwrite.ttp
	install -m 644 rawwrite.ttp $(lib)
	gzip -9f $(lib)/rawwrite.ttp
	# install config script
	install -m 755 atabootconfig $(sbin)
	# build and install boot disk images
	./builddisk 1440
	./builddisk 720
	install -m 644 img*Atari $(lib)
	gzip -9f $(lib)/img*
	# install README for lib dir
	install -m 644 debian/README.lib $(lib)/README
	dh_installdocs src/ataboot.txt
	dh_installchangelogs src/CHANGES
	dh_installmanpages
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	#dh_shlibdeps
	dh_gencontrol
	#dh_du
	dh_md5sums
	dh_builddeb

define checkdir
	test -f src/bootstrap.c -a -f debian/rules
endef

binary:		binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean
