#!/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: *//')
fullversion := $(shell \
        dpkg-parsechangelog | sed -n 's/^Version: //p; t e; b; :e; q' \
	)

TAR_DIR=console-data-1999.08.29
export DH_COMPAT=3

# 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)
	( cp -a build-tree/extras/keymaps ${BUILD_TREE} )
	touch ${configure-stamp}

clean:
	dh_testdir
	dh_testroot
	rm -rf console-data.config debian/console-data.templates ${SOURCE_DIR} ${STAMP_DIR}
	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}
	mkdir -p  ${installdir}/usr/share/consolefonts
	# First fix the sfm tables in the lat2u fonts
	for d in ${topdir}/build-tree/extras/consolefonts/lat2u-*.psf ; do \
		psfaddtable $$d ${topdir}/build-tree/${TAR_DIR}/consoletrans/lat2u.sfm > X ; \
		mv X $$d ;  \
	done
	# 
	cp ${topdir}/build-tree/extras/consolefonts/* ${installdir}/usr/share/consolefonts
        # install the eurofonts collection	
	cp ${topdir}/build-tree/consolefonts/* ${installdir}/usr/share/consolefonts
	cp ${topdir}/build-tree/consoletrans/* ${installdir}/usr/share/consoletrans
	# uudecode -o ${installdir}/usr/share/consoletrans/iso13.acm < debian/iso13.acm.uue 
	# gzip ${installdir}/usr/share/consoletrans/iso13*
	gzip -9 <  ${installdir}/usr/share/consoletrans/iso13.sfm \
	  >   ${installdir}/usr/share/consoletrans/iso13.acm.gz
	gzip  -9 -f ${installdir}/usr/share/consolefonts/*.psf
	# move the euro.inc so everyone can use it
	# mv ${installdir}/usr/share/keymaps/i386/include/euro* \
		${installdir}/usr/share/keymaps/include
        # these fonts have a redistribution restriction
	rm -f ${installdir}/usr/share/consolefonts/Agafari-1*
	# And this is a mistake
	rm -f ${installdir}/usr/share/consolefonts/t.psf*
	# No more speakup keymaps
	rm -rf ${installdir}/usr/share/keymaps/i386/qwerty/speakup*

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

	dh_consoledata -i
	dh_installdebconf -i 

	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}/

        # install changelog in non-udeb only
	dh_installchangelogs -p console-data

	dh_compress -i --package console-data
	dh_fixperms -i
	dh_installdeb -i

	dh_perl       --package console-data -d
	dh_gencontrol --package console-data
	dh_builddeb --package console-data
        # md5sums for deb only, not udeb
	dh_md5sums  --package  console-data

        # 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}_${fullversion}_all.udeb debian-installer optional ; 	\
	done
	for P in ${EXTRA_UDEB_PKGS} ; do							\
		 dpkg-distaddfile  $${P}_${fullversion}_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}_${fullversion}_all.udeb ; 				\
	done


binary-arch:


# Below here is fairly generic really

binary:		binary-indep binary-arch

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