#!/usr/bin/make -f
# -*- makefile -*-
#
# debian/rules file for package `console-tools'

# turn around bug in dpkg-parsechangelog
export LC_ALL=C

package=console-data
version=$(shell LC_ALL=C dpkg-parsechangelog | grep ^Version: | sed 's/^Version: [0-9]:*//')
fullversion := $(shell \
        dpkg-parsechangelog | sed -n 's/^Version: //p; t e; b; :e; q' | \
	sed 's/.*://' \
	)

TAR_DIR=console-data-1.01
export DH_COMPAT=5

# the dbs rules
include /usr/share/dbs/dbs-build.mk
#
ifeq (,$(DEB_BUILD_GNU_TYPE))
 include /usr/share/dbs/dpkg-arch.mk
endif
#
ARCH=$(shell dpkg --print-architecture)

# All the small .udebs that get built
EXTRA_UDEB_PKGS    = 	console-keymaps-mac	\
		console-keymaps-sun	\
		console-keymaps-acorn	\
		console-keymaps-atari	\
		console-keymaps-amiga	\
		console-keymaps-usb	\
		console-keymaps-dec
OPTIONAL_UDEB_PKGS = console-keymaps-at
UDEB_PKGS=${OPTIONAL_UDEB_PKGS} ${EXTRA_UDEB_PKGS}

topdir=$(shell pwd)
installdir=${topdir}/debian/console-data
docdir=${installdir}/usr/share/doc/${package}
exampledir=${docdir}/examples
sharedir=${installdir}/usr/share/console

build-stamp=${STAMP_DIR}/build-stamp
build-udeb-stamp =${STAMP_DIR}/build-udeb-stamp
configure-stamp=${STAMP_DIR}/configure-stamp

build: ${build-stamp} ${configure-stamp}
configure: ${configure-stamp}

${build-stamp}: ${configure-stamp} $(patched)
	dh_testdir
	make -C ${BUILD_TREE}
	chmod +x debian/gen-acorn-keymaps
	touch ${build-stamp}

build-udeb: ${build-stamp}
	dh_testdir
	# Ensure the acorn keymaps get built. Cheaper in space terms than shipping
	# ps2-to-riscpc on the boot floppies. 
	debian/gen-acorn-keymaps
	touch ${build-udeb-stamp}

${configure-stamp}: $(patched)
	dh_testdir
	( cd ${BUILD_TREE} && ./configure --with-main_compressor=gzip )
	touch ${configure-stamp}

clean:
	dh_testdir
	dh_testroot
	rm -rf console-data.config debian/console-data.templates ${SOURCE_DIR} ${STAMP_DIR} debian/sort-tmp debian/locales
	rm -rf debian/substvars
	dh_clean

binary-indep: build build-udeb
	dh_testdir -i
	dh_testroot -i
	dh_clean -i -k
	dh_installdirs -i

	make -C ${BUILD_TREE} install DESTDIR=${installdir}

        # Install the udeb files
	for P in ${UDEB_PKGS} ; do  dh_install -p $$P ; done

	dh_consoledata -i
        # For convenience of programs that want a list of supported keymaps
	chmod u+x debian/list-keymaps.pl
	debian/list-keymaps.pl debian/console-data.keymaps > ${sharedir}/lists/console-data.keymap-list
	# Needed for ubiquity
	for f in debian/console-keymaps-*.templates; do \
	         echo >> debian/console-data.templates ; \
	         cat $$f >> debian/console-data.templates ; \
	done
	dh_install -pconsole-data debian/udeb/console-keymaps* usr/share/console/lists/

	dh_installdebconf -i 
	# Sort keymaps (including in translations)
	# MUST be after dh_installdebconf
	sh debian/sort-keymaps
	install -d ${installdir}/usr/share/locale
	for PO in debian/po/*.po; do \
		L="$$(basename $$PO .po)"; \
		install -d ${installdir}/usr/share/locale/$$L/LC_MESSAGES; \
		msgfmt -o ${installdir}/usr/share/locale/$$L/LC_MESSAGES/console-data.mo $$PO; \
	done

	dh_installdocs -p console-data debian/README.us-intl
	install -d -m755 ${exampledir}
	mv $(docdir)/README.us-intl ${docdir}/keymaps/
	mv ${docdir}/keymaps/hypermap.m4 ${exampledir}/

	dh_installchangelogs 
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i

	dh_perl       --package console-data -d
	dh_gencontrol --package console-data
	dh_builddeb --package console-data
	dh_md5sums 

        # Build udebs, avoid automagic problem. Don't use dh_gencontrol guesses
        # instead, register file manually with distaddfile 
	chmod +x ./debian/trim_keymaps.sh
	for P in ${OPTIONAL_UDEB_PKGS} ; do							\
		dpkg-distaddfile  $${P}_${version}_all.udeb debian-installer optional ; 	\
	done
	for P in ${EXTRA_UDEB_PKGS} ; do							\
		 dpkg-distaddfile  $${P}_${version}_all.udeb debian-installer extra ;	\
	done
	for P in ${UDEB_PKGS}; do 								\
		./debian/trim_keymaps.sh ${topdir}/debian/$${P} ; 				\
		dh_link -p $${P} ;								\
		find debian/$${P} -name 'postrm' -exec rm {} \;	 ;				\
		dh_gencontrol --package $${P} -- -fdebian/files~ ;				\
		dh_builddeb --package $${P} 				 			\
			--filename=$${P}_${version}_all.udeb ; 				\
	done


binary-arch:


# Below here is fairly generic really

binary:		binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean checkroot
