#!/usr/bin/make -f

SHELL = /bin/bash

xkbdir = /usr/share/X11/xkb
export xkbdir
pre = debian/preprocessor

manprocessor = sed \
	-e 's|^\([.a-zA-Z][a-zA-Z]*\) /usr/local/etc|\1 /etc|' \
	-e 's|^\([.a-zA-Z][a-zA-Z]*\) /usr/local|\1 /usr|'

# One font per fontset
udeb-fonts = Armenian-Fixed16 CyrAsia-Fixed16 CyrKoi-TerminusBold16	\
CyrSlav-TerminusBold16 Georgian-Fixed16 Greek-VGA16 Hebrew-VGA16	\
Lao-Fixed16 Lat15-TerminusBold16 Lat2-TerminusBold16 Lat38-VGA16	\
Lat7-TerminusBold16 Thai-Fixed16
# These are Linux-only
udeb-fonts-big = Arabic-VGA16 Ethiopian-Goha16 Uni1-VGA16 Uni2-VGA16	\
Uni3-Fixed16 Vietnamese-Fixed16

# One fontset per charmap
udeb-scm = $(addprefix Fonts/, ARMSCII-8_Armenian.scm			\
CP1251_CyrSlav.scm CP1255_Hebrew.scm GEORGIAN-ACADEMY_Georgian.scm	\
GEORGIAN-PS_Georgian.scm IBM1133_Lao.scm ISO-8859-1_Lat15.scm		\
ISO-8859-2_Lat2.scm ISO-8859-3_Lat38.scm ISO-8859-4_Lat7.scm		\
ISO-8859-4_Lat15.scm ISO-8859-5_CyrSlav.scm ISO-8859-7_Greek.scm	\
ISO-8859-8_Hebrew.scm ISO-8859-9_Lat15.scm ISO-8859-10_Lat15.scm	\
ISO-8859-11_Thai.scm ISO-8859-13_Lat7.scm ISO-8859-14_Lat38.scm		\
ISO-8859-15_Lat15.scm ISO-8859-16_Lat2.scm KOI8-R_CyrKoi.scm		\
KOI8-U_CyrKoi.scm TIS-620_Thai.scm)

udeb-fonts-linux = $(addprefix Fonts/, $(addsuffix .psf.gz, \
			$(udeb-fonts) $(udeb-fonts-big)))
udeb-fonts-freebsd = $(addprefix Fonts/, $(addsuffix .fnt, $(udeb-fonts)))

main_build:
	rm Keyboard/KeyboardNames.pl
	$(MAKE) build-all xkbdir=$(xkbdir)
	touch main_build

debian/kbdnames.gz: main_build
	( cd Keyboard \
	&& ./kbdnames-maker KeyboardNames.pl \
		| grep -v '^C[*]' | grep -v '[*]model[*]' | sort | gzip -9 ) >$@

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: main_build debian/kbdnames.gz

.PHONY : clean
clean:
	dh_testdir
	debconf-updatepo
	$(MAKE) maintainer-clean
	-rm -f build main_build
	-rm -f debian/kbdnames.gz
	-rm -f debian/*~
	dh_clean

.PHONY : install
install: install-linux install-freebsd install-main install-bdf2psf install-linux-udeb install-freebsd-udeb install-udeb

.PHONY : install-linux
install-linux: build
	dh_testdir
	dh_testroot
	dh_prep -p console-setup-linux
	$(MAKE) etcdir=debian/console-setup-linux/etc \
		prefix=debian/console-setup-linux/usr install-common-linux

.PHONY : install-freebsd
install-freebsd: build
	dh_testdir
	dh_testroot
	dh_prep -p console-setup-freebsd
	$(MAKE) etcdir=debian/console-setup-freebsd/etc \
		prefix=debian/console-setup-freebsd/usr install-common-freebsd

.PHONY : install-main
install-main: build
	dh_testdir
	dh_testroot
	dh_prep -pkeyboard-configuration
	dh_prep -p console-setup
	dh_prep -p console-setup-mini
	$(MAKE) etcdir=debian/console-setup/etc \
		prefix=debian/console-setup/usr install-common
	$(MAKE) etcdir=debian/console-setup/etc \
		prefix=debian/console-setup/usr install-ckbcomp
	$(MAKE) etcdir=debian/console-setup-mini/etc \
		prefix=debian/console-setup-mini/usr install-common
	$(MAKE) etcdir=debian/console-setup-mini/etc \
		prefix=debian/console-setup-mini/usr install-ckbcomp-mini
	$(pre) --mini debian/console-setup-mini/bin/setupcon
	$(pre) --mini debian/console-setup-mini/usr/bin/ckbcomp-mini
	ls debian/console-setup*/usr/share/man/*/* \
		| xargs -n 1 $(manprocessor) -i
	install -d debian/keyboard-configuration/usr/share/console-setup/
	mv debian/console-setup/etc/default/keyboard \
		debian/keyboard-configuration/usr/share/console-setup/
	install -d debian/console-setup/usr/share/console-setup/
	mv debian/console-setup/etc/default/console-setup \
		debian/console-setup/usr/share/console-setup/
	rm debian/console-setup-mini/etc/default/keyboard
	rm debian/console-setup-mini/etc/default/console-setup
	dh_movefiles -pkeyboard-configuration --sourcedir=debian/console-setup \
		usr/share/man/man5/keyboard.5
	rm debian/console-setup-mini/usr/share/man/man5/keyboard.5
# For KeyboardNames.pl and kbdnames-maker see #420914
	dh_install -pkeyboard-configuration Keyboard/KeyboardNames.pl \
		usr/share/console-setup/
	dh_install -pkeyboard-configuration Keyboard/kbdnames-maker \
		usr/share/console-setup/
	dh_link -pkeyboard-configuration usr/share/X11/xkb/rules/xorg.lst \
		usr/share/doc/keyboard-configuration/xorg.lst
	dh_installinit -pkeyboard-configuration \
		--no-start --name keyboard-setup -- start 06 S .
	dh_installinit -pkeyboard-configuration \
		--no-start --name console-setup -- start 49 S .

.PHONY : install-bdf2psf
install-bdf2psf: build
	dh_testdir
	dh_testroot
	dh_prep -pbdf2psf
	dh_install -pbdf2psf Fonts/bdf2psf usr/bin/
	dh_install -pbdf2psf Fonts/*.equivalents Fonts/*.set \
		Fonts/fontsets usr/share/bdf2psf
	dh_installman -pbdf2psf man/bdf2psf.1
	dh_installdocs -pbdf2psf debian/README.fontsets

.PHONY : install-linux-udeb
install-linux-udeb: build
	dh_testdir
	dh_testroot
# console-setup-linux-charmaps-udeb
	dh_prep -p console-setup-linux-charmaps-udeb
	$(MAKE) etcdir=debian/console-setup-linux-charmaps-udeb/etc \
		prefix=debian/console-setup-linux-charmaps-udeb/usr \
			install-common-linux
	dh_install -p console-setup-linux-charmaps-udeb \
		Keyboard/charmap_functions.sh usr/share/console-setup/
	rm -r debian/console-setup-linux-charmaps-udeb/usr/share/consolefonts/
# console-setup-linux-fonts-udeb
	dh_prep -p console-setup-linux-fonts-udeb
	dh_install -p console-setup-linux-fonts-udeb $(udeb-fonts-linux) \
		usr/share/consolefonts

.PHONY : install-freebsd-udeb
install-freebsd-udeb: build
	dh_testdir
	dh_testroot
# console-setup-freebsd-charmaps-udeb
	dh_prep -p console-setup-freebsd-charmaps-udeb
	$(MAKE) etcdir=debian/console-setup-freebsd-charmaps-udeb/etc \
		prefix=debian/console-setup-freebsd-charmaps-udeb/usr \
			install-common-freebsd
	dh_install -p console-setup-freebsd-charmaps-udeb \
		Keyboard/charmap_functions.sh usr/share/console-setup/
	rm -r debian/console-setup-freebsd-charmaps-udeb/usr/share/consoletrans/
	rm -r debian/console-setup-freebsd-charmaps-udeb/usr/share/syscons/
# console-setup-freebsd-fonts-udeb
	dh_prep -p console-setup-freebsd-fonts-udeb
	dh_install -p console-setup-freebsd-fonts-udeb $(udeb-fonts-freebsd) \
		usr/share/syscons/fonts/
	dh_install -p console-setup-freebsd-fonts-udeb $(udeb-scm) \
		usr/share/syscons/scrnmaps/

.PHONY : install-udeb
install-udeb: build
	dh_testdir
	dh_testroot
	dh_prep -p console-setup-amiga-ekmap
	dh_prep -p console-setup-ataritt-ekmap
	dh_prep -p console-setup-macintoshold-ekmap
	dh_prep -p console-setup-pc-ekmap
	dh_prep -p console-setup-sun4-ekmap
	dh_prep -p console-setup-sun5-ekmap
	dh_prep -p console-setup-udeb
	dh_prep -p console-setup-pc-ekbd
	$(MAKE) etcdir=debian/console-setup-udeb/etc \
		prefix=debian/console-setup-udeb/usr install-common
	$(MAKE) etcdir=debian/console-setup-udeb/etc \
		prefix=debian/console-setup-udeb/usr install-ckbcomp-mini
	$(pre) --mini debian/console-setup-udeb/bin/setupcon
	$(pre) --mini debian/console-setup-udeb/usr/bin/ckbcomp-mini
	rm -r debian/console-setup-udeb/usr/share/man/
	dh_installdirs -p console-setup-udeb usr/share/console-setup
	mv debian/console-setup-udeb/etc/default/keyboard \
		debian/console-setup-udeb/usr/share/console-setup/
	rm debian/console-setup-udeb/etc/default/console-setup
	rm debian/console-setup-udeb/usr/share/console-setup/charmap_functions.sh
	install -m0755 debian/keyboard-configuration.config \
		debian/console-setup-udeb/usr/share/console-setup/
	$(pre) --mini debian/console-setup-udeb/usr/share/console-setup/keyboard-configuration.config
	dh_install -p console-setup-udeb debian/kbdnames.gz usr/share/console-setup
	dh_installdirs -p console-setup-udeb lib/debian-installer.d/
	install -m0644 debian/console-setup-udeb.startup \
		debian/console-setup-udeb/lib/debian-installer.d/S55console-setup
	dh_installdirs -p console-setup-udeb usr/lib/base-installer.d
	install -m0755 debian/console-setup-udeb.base-installer \
		debian/console-setup-udeb/usr/lib/base-installer.d/20console-setup
	$(pre) --mini debian/console-setup-udeb/usr/lib/base-installer.d/20console-setup
# console-setup-*-ekmap
	dh_movefiles -p console-setup-amiga-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/amiga.ekmap.gz
	dh_movefiles -p console-setup-ataritt-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/ataritt.ekmap.gz
	dh_movefiles -p console-setup-macintoshold-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/macintosh_old.ekmap.gz
	dh_movefiles -p console-setup-pc-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/pc105.ekmap.gz
	dh_movefiles -p console-setup-sun4-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/sun4.ekmap.gz
	dh_movefiles -p console-setup-sun5-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/sun5.ekmap.gz
	dh_movefiles -p console-setup-pc-ekbd --sourcedir=debian/console-setup-udeb usr/share/console-setup/pc105.ekbd.gz

# Build architecture-independent files here.
.PHONY : binary-indep
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdocs -pkeyboard-configuration \
		FAQ copyright.fonts copyright.xkb
	dh_installdocs -p console-setup-linux --link-doc=keyboard-configuration
	dh_installdocs -pconsole-setup-freebsd --link-doc=keyboard-configuration
	dh_installdocs -p console-setup-mini --link-doc=keyboard-configuration
	dh_installdocs -p console-setup --link-doc=keyboard-configuration
	dh_lintian
	dh_bugfiles
	dh_installman
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb
	$(pre) debian/keyboard-configuration/DEBIAN/config
	$(pre) debian/keyboard-configuration/DEBIAN/postinst
	$(pre) debian/console-setup/DEBIAN/config
	$(pre) debian/console-setup/DEBIAN/postinst
	$(pre) --mini debian/console-setup-udeb/DEBIAN/postinst
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
.PHONY : binary-arch
binary-arch : build install
# We have nothing to do by default.

.PHONY : binary
binary : binary-indep binary-arch
