#!/usr/bin/make -f

tmp  = debian/tmp
ctmp = debian/spectemu-common
xtmp = debian/spectemu-x11
stmp = debian/spectemu-svga

# Put any extra svgalib architectures here.
svgalib_archs = amd64 i386

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

CFLAGS = -O3 -Wall -fomit-frame-pointer -funroll-loops
ifeq (,$(filter debug,$(DEB_BUILD_OPTIONS)))
CFLAGS += -g
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- --datadir=/etc --with-readline

override_dh_auto_build:
	$(MAKE) CFLAGS="$(CFLAGS)" all tapeout
	$(MAKE) FLAGS="$(CFLAGS)" -C utils

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) realclean
	$(MAKE) -C utils clean

override_dh_auto_install:
	# datadir is deliberately different here because we're splitting it,
	# and this makes the split more convenient.
	dh_auto_install -- prefix=`pwd`/$(tmp)/usr \
			   datadir=`pwd`/$(tmp)/usr/share/spectemu

override_dh_install:
	mv -f $(tmp)/usr/share/spectemu/spectemu.cfg $(ctmp)/etc/spectemu/
	dh_install
	mv -f $(tmp)/usr/share/man/man1/xspect.1 \
	      $(ctmp)/usr/share/man/man1/spectemu.1

	# dh_compress won't do these for us, as the package split causes
	# them to be broken symlinks.
	ln -sf spectemu.1.gz $(xtmp)/usr/share/man/man1/xspect.1.gz
ifneq (,$(findstring $(DEB_HOST_ARCH),$(svgalib_archs)))
	ln -sf spectemu.1.gz $(stmp)/usr/share/man/man1/vgaspect.1.gz
endif

override_dh_installdocs:
	dh_installdocs --link-doc=spectemu-common

ifneq (,$(findstring $(DEB_HOST_ARCH),$(svgalib_archs)))
override_dh_fixperms:
	dh_fixperms
	chmod 4755 $(stmp)/usr/bin/vgaspect
endif
