#!/usr/bin/make -f
#
# Copyright © 1997-1999 Joey Hess <joeyh@debian.org>
# Copyright © 2002-2004 Robert Millan <rmh@debian.org>
# Copyright © 2004-2012 Guillem Jover <guillem@debian.org>
# Copyright © 2018-2021 Stephen Kitt <skitt@debian.org>
#

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

tmpdir := $(CURDIR)/debian/tmp
pkg_bochs := $(CURDIR)/debian/bochs

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk

cdrom := cdrom

ifeq ($(DEB_HOST_ARCH_OS),linux)
  kernel := linux
  conf_arch_args += --enable-pcidev
  eth := eth0
  com := ttyS0
endif
ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
  kernel := fbsd
  eth := xl0
  com := cuua0
  cdrom := acd0
endif
ifeq ($(DEB_HOST_ARCH_OS),hurd)
  eth := eth0
  com := com0
endif

%:
	dh $@ --no-parallel

override_dh_auto_clean:
	[ ! -f bios/Makefile ] || $(MAKE) -C bios bios-clean
	[ ! -f Makefile ] || $(MAKE) dist-clean

	dh_auto_clean

override_dh_autoreconf:
# config.h.in is extensively modified, we can’t run autoheader
	AUTOHEADER=true dh_autoreconf

override_dh_auto_configure:
# Note: We disable docbook support here as a cheap way to avoid
# building it in build-arch, and only in build-indep. This works
# because configure only disables entering the directory.
	dh_auto_configure -- \
	  --with-rfb \
	  --with-sdl2 \
	  --with-term \
	  --with-x11 \
	  --with-wx \
	  --disable-docbook \
	  --enable-3dnow \
	  --enable-a20-pin \
	  --enable-all-optimizations \
	  --enable-amx \
	  --enable-avx \
	  --enable-busmouse \
	  --enable-cdrom \
	  --enable-cet \
	  --enable-clgd54xx \
	  --enable-compressed-hd \
	  --enable-cpu-level=6 \
	  --enable-debugger \
	  --enable-debugger-gui \
	  --enable-disasm \
	  --enable-e1000 \
	  --enable-es1370 \
	  --enable-evex \
	  --enable-fpu \
	  --enable-idle-hack \
	  --enable-instrumentation \
	  --enable-pci \
	  --enable-plugins \
	  --enable-repeat-speedups \
	  --enable-memtype \
	  --enable-ne2000 \
	  --enable-pnic \
	  --enable-protection-keys \
	  --enable-raw-serial \
	  --enable-sb16 \
	  --enable-smp \
	  --enable-svm \
	  --enable-uintr \
	  --enable-usb \
	  --enable-usb-ehci \
	  --enable-usb-ohci \
	  --enable-usb-xhci \
	  --enable-vmx=2 \
	  --enable-voodoo \
	  --enable-x86-64 \
	  --enable-x86-debugger \
	  $(conf_arch_args)

override_dh_auto_build-arch:
	dh_auto_build

ifeq ($(DEB_HOST_ARCH_CPU),i386)
	dh_auto_build -- misc/sb16/sb16ctrl
endif

override_dh_auto_build-indep:
# bochsbios
	$(MAKE) -C bios GCC32="i686-linux-gnu-gcc -march=i386 -fno-stack-protector" LD32="i686-linux-gnu-ld" OBJCOPY32="i686-linux-gnu-objcopy"

# bochs-doc
	$(MAKE) -C doc/docbook USE_JADE=1

override_dh_auto_install-arch:
	dh_auto_install

# misc cleanup
	rm -f \
	  $(tmpdir)/usr/share/bochs/install-x11-fonts \
	  $(tmpdir)/usr/share/bochs/test-x11-fonts \
	  $(tmpdir)/usr/share/man/man1/bochs-dlx.1.gz \
	  $(tmpdir)/usr/share/doc/bochs/COPYING.gz \
	  $(tmpdir)/usr/bin/bochs-docs

# bochs
	cat $(tmpdir)/usr/share/doc/bochs/bochsrc-sample.txt \
	| sed \
	  -e "s/#kernel#/$(kernel)/g" \
	  -e "s/#eth#/$(eth)/g" \
	  -e "s/#com#/$(com)/g" \
	  -e "s/#cdrom#/$(cdrom)/g" \
	| gzip -c9n \
	> $(pkg_bochs)/usr/share/doc/bochs/examples/bochsrc.gz
	rm -f $(tmpdir)/usr/share/doc/bochs/bochsrc-sample.txt

	mv $(tmpdir)/usr/bin/bochs \
	   $(tmpdir)/usr/bin/bochs-bin
	install -m755 debian/launcher \
		      $(tmpdir)/usr/bin/bochs

	cp -a debian/etc debian/tmp/
	chmod 755 $(tmpdir)/etc/bochs-init/init.sh

ifeq ($(DEB_HOST_ARCH_CPU),i386)
	cp misc/sb16/sb16ctrl \
	   $(tmpdir)/usr/bin/
endif

override_dh_auto_install-indep:
# bochsbios
	mkdir -p \
	   $(tmpdir)/usr/share/bochs/
	cp bios/BIOS* \
	   $(tmpdir)/usr/share/bochs/

# bochs-doc
	$(MAKE) -C doc/docbook install DESTDIR=$(tmpdir)

	cp /usr/share/sgml/docbook/stylesheet/dsssl/modular/images/note.gif \
	   $(tmpdir)/usr/share/doc/bochs/images/

override_dh_installchangelogs:
	dh_installchangelogs CHANGES

override_dh_makeshlibs: