#!/usr/bin/make -f

include Makefile

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  MAKEFLAGS += -j$(NUMJOBS)
endif

xkbdir := /usr/share/X11/xkb

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 supported by d-i in text console
udeb-fonts := CyrAsia-Fixed16 CyrSlav-TerminusBold16 Greek-VGA16 \
Lat15-TerminusBold16 Lat2-TerminusBold16 Lat38-VGA16 Lat7-TerminusBold16
# These are Linux-only
udeb-fonts-big :=

# These are not properly supported by d-i in text console, but
# included for completeness for speakup & brltty screen reading
udeb-fonts += Armenian-Fixed16 Georgian-Fixed16 Hebrew-VGA16 Lao-Fixed16 Thai-Fixed16
udeb-fonts-big += Arabic-VGA16 Ethiopian-Goha16 Vietnamese-Fixed16

# These are needed for proper text screen reading by speakup and brltty
udeb-fonts-big += FullCyrAsia-Fixed16 FullCyrSlav-TerminusBold16 FullGreek-VGA16

# This is not useful (KOI8-R encoding)
# CyrKoi-TerminusBold16

# These are not useful (convered by fonts above)
# Uni1-VGA16 Uni2-VGA16 Uni3-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)))

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

build: debian/kbdnames.gz

clean : maintainer-clean
	dh_testdir
	-rm -f *.ekmap *.ekbd *.ekmap.gz *.ekbd.gz
	debconf-updatepo
	$(MAKE) maintainer-clean
	-rm -f build main_build building_debian
	-rm -f debian/kbdnames.gz
	-rm -f debian/*~
	dh_clean
	ln -sf debian/copyright COPYRIGHT

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

.PHONY : deb-install-linux
deb-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
	dh_install -p console-setup-linux \
		init/90-console-setup.rules lib/udev/rules.d/
	dh_install -p console-setup-linux \
		init/keyboard-setup.sh lib/console-setup/
	dh_install -p console-setup-linux \
		init/console-setup.sh lib/console-setup/
	dh_systemd_enable -p console-setup-linux \
		--name keyboard-setup
	dh_systemd_enable -p console-setup-linux \
		--name console-setup
	dh_systemd_enable -p console-setup-linux \
		--name setvtrgb
	dh_installinit -p console-setup-linux \
		--no-start --name keyboard-setup.sh
	dh_installinit -p console-setup-linux \
		--no-start --name console-setup.sh
	dh_installinit -p console-setup-linux \
		--no-start --name setvtrgb
	dh_systemd_start -p console-setup-linux \
		--no-start --name keyboard-setup
	dh_systemd_start -p console-setup-linux \
		--no-start --name console-setup
	dh_systemd_start -p console-setup-linux \
		--no-start --name setvtrgb
	sed -i 's/RUNLEVEL/2 3 4 5/' \
		debian/console-setup-linux/etc/init.d/console-setup.sh

.PHONY : deb-install-freebsd
deb-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
	dh_install -p console-setup-freebsd \
		init/keyboard-setup.sh lib/console-setup/
	dh_install -p console-setup-freebsd \
		init/console-setup.sh lib/console-setup/
	dh_systemd_enable -p console-setup-freebsd \
		--name keyboard-setup
	dh_systemd_enable -p console-setup-freebsd \
		--name console-setup
	dh_installinit -p console-setup-freebsd \
		--no-start --name keyboard-setup.sh
	dh_installinit -p console-setup-freebsd \
		--no-start --name console-setup.sh
	dh_systemd_start -p console-setup-freebsd \
		--no-start --name keyboard-setup
	dh_systemd_start -p console-setup-freebsd \
		--no-start --name console-setup
	sed -i 's/RUNLEVEL/S/' \
		debian/console-setup-freebsd/etc/init.d/console-setup.sh

.PHONY : deb-install-main
deb-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
	dh_install -pconsole-setup Keyboard/pc105.tree \
		usr/share/console-setup
ifneq (,$(filter console-setup-linux, $(shell dh_listpackages)))
	install -m 644 debian/vtrgb debian/console-setup-linux/etc/console-setup/
	install -m 644 debian/vtrgb.vga debian/console-setup-linux/etc/console-setup/
endif
	$(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/*/* \
	   debian/console-setup-mini/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
	install -d debian/keyboard-configuration/usr/share/initramfs-tools/hooks \
		debian/keyboard-configuration/usr/share/initramfs-tools/scripts/init-top \
		debian/keyboard-configuration/usr/share/initramfs-tools/scripts/panic
	install -m755 debian/console-setup.initramfs-hook \
		debian/keyboard-configuration/usr/share/initramfs-tools/hooks/console_setup
	install -m755 debian/console-setup.initramfs-top \
		debian/keyboard-configuration/usr/share/initramfs-tools/scripts/init-top/console_setup
	sed -e "/^OPTION=/d" \
		< debian/console-setup.initramfs-top \
		> debian/keyboard-configuration/usr/share/initramfs-tools/scripts/panic/console_setup
	chmod 755 debian/keyboard-configuration/usr/share/initramfs-tools/scripts/panic/console_setup
	install -d debian/keyboard-configuration/usr/share/apport/package-hooks
	install -m644 debian/console-setup.apport \
		debian/keyboard-configuration/usr/share/apport/package-hooks/source_console-setup.py

.PHONY : deb-install-bdf2psf
deb-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 : udeb-install-linux
udeb-install-linux: 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
	dh_install -p console-setup-linux-fonts-udeb debian/font-switch \
		/usr/share/console-setup

.PHONY : udeb-install-freebsd
udeb-install-freebsd: 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 : udeb-install
udeb-install: 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) --udeb debian/console-setup-udeb/bin/setupcon
	$(pre) --udeb 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) --udeb debian/console-setup-udeb/usr/share/console-setup/keyboard-configuration.config
	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) --udeb debian/console-setup-udeb/usr/lib/base-installer.d/20console-setup
	install -m 644 debian/vtrgb debian/console-setup-udeb/etc/
	install -m 644 debian/vtrgb.vga debian/console-setup-udeb/etc/
# console-setup-*-ekmap
	dh_movefiles -p console-setup-amiga-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/amiga.ekmap.gz
	#cat amiga.ekmap.gz >debian/console-setup-amiga-ekmap/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
	#cat ataritt.ekmap.gz >debian/console-setup-ataritt-ekmap/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
	#cat macintosh_old.ekmap.gz >debian/console-setup-macintoshold-ekmap/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
	#cat pc105.ekmap.gz >debian/console-setup-pc-ekmap/usr/share/console-setup/pc105.ekmap.gz
	dh_install -pconsole-setup-pc-ekmap Keyboard/pc105.tree \
		usr/share/console-setup-mini
	dh_movefiles -p console-setup-sun4-ekmap --sourcedir=debian/console-setup-udeb usr/share/console-setup/sun4.ekmap.gz
	#cat sun4.ekmap.gz >debian/console-setup-sun4-ekmap/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
	#cat sun5.ekmap.gz >debian/console-setup-sun5-ekmap/usr/share/console-setup/sun5.ekmap.gz
# console-setup-pc-ekbd
	dh_movefiles -p console-setup-pc-ekbd --sourcedir=debian/console-setup-udeb usr/share/console-setup/pc105.ekbd.gz
	#cat pc105.ekbd.gz >debian/console-setup-pc-ekbd/usr/share/console-setup/pc105.ekbd.gz

# Build architecture-independent files here.
.PHONY : binary-indep
binary-indep: build deb-udeb-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 -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
# To deal somehow with bug #657904.  See also #255819 and #17624.
#	rm debian/console-setup/usr/share/doc/console-setup
	$(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) --udeb debian/console-setup-udeb/DEBIAN/postinst
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

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

.PHONY : build-arch build-indep build-stamp
build-arch build-indep build-stamp : build
