#!/usr/bin/make -f
# -*- makefile -*-
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
# Default/i386/amd64 options from sdlmame rules file by Cesare Falco.

export DH_VERBOSE = 1

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_ARCH_BITS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)

# Define DEB_HOST_MULTIARCH for QT prefix handling
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# add c++ hardening options (upstream makefile doesn't use CPPFLAGS)
export DEB_CFLAGS_MAINT_APPEND = $(shell dpkg-buildflags --get CPPFLAGS)

# Sacrifice linking speed to avoid using so much memory
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-keep-memory

# taken verbatim from debian policy
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += -j$(NUMJOBS)
endif

# Override relevant make variables in original makefile
DEB_OPTS = \
    NOWERROR=1 \
    OSD=sdl \
    DEBUG= \
    TARGETOS=linux \
    DISTRO=debian-stable \
    FORCE_DRC_C_BACKEND=1 \
    PTR64= \
    BIGENDIAN= \
    NOASM=1 \
    QT_HOME=/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/ \
    USE_SYSTEM_LIB_EXPAT=1 \
    USE_SYSTEM_LIB_ZLIB=1 \
    USE_SYSTEM_LIB_JPEG=1 \
    USE_SYSTEM_LIB_FLAC=1 \
    USE_SYSTEM_LIB_LUA=lua5.3:/usr/include/lua5.3 \
    USE_SYSTEM_LIB_SQLITE3=1 \
    USE_SYSTEM_LIB_PORTMIDI=1 \
    USE_SYSTEM_LIB_PORTAUDIO=1

# Override make variables for specific archs
# Linux architectures
ifeq ($(DEB_HOST_ARCH),amd64)
DEB_OPTS += \
    FORCE_DRC_C_BACKEND= \
    PTR64=1 \
    NOASM=
endif

ifeq ($(DEB_HOST_ARCH),arm64)
DEB_OPTS += \
    ARCHOPTS=-mabi=lp64
endif

ifeq ($(DEB_HOST_ARCH),i386)
DEB_OPTS += \
    FORCE_DRC_C_BACKEND= \
    NOASM=
endif

ifeq ($(DEB_HOST_ARCH),ia64)
DEB_OPTS += \
    PTR64=1
endif

ifeq ($(DEB_HOST_ARCH),mips)
DEB_OPTS += \
    ARCHOPTS=-Umips \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),mipsel)
DEB_OPTS += \
    ARCHOPTS=-Umips
endif

ifeq ($(DEB_HOST_ARCH),powerpc)
DEB_OPTS += \
    ARCHOPTS=-Upowerpc \
    NOASM= \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),ppc64)
DEB_OPTS += \
    PTR64=1 \
    NOASM= \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),ppc64el)
DEB_OPTS += \
    PTR64=1 \
    NOASM=
endif

ifeq ($(DEB_HOST_ARCH),s390)
DEB_OPTS += \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),s390x)
DEB_OPTS += \
    PTR64=1 \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),sparc)
DEB_OPTS += \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),sparc64)
DEB_OPTS += \
    PTR64=1 \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),x32)
DEB_OPTS += \
    FORCE_DRC_C_BACKEND= \
    NOASM=
endif

# kFreeBSD architectures
ifeq ($(DEB_HOST_ARCH),kfreebsd-amd64)
DEB_OPTS += \
    TARGETOS=freebsd \
    FORCE_DRC_C_BACKEND= \
    PTR64=1 \
    NOASM= \
    USE_SYSTEM_LIB_PORTMIDI=
export DEB_LDFLAGS_MAINT_APPEND = -ldl
endif

ifeq ($(DEB_HOST_ARCH),kfreebsd-i386)
DEB_OPTS += \
    TARGETOS=freebsd \
    FORCE_DRC_C_BACKEND= \
    NOASM= \
    USE_SYSTEM_LIB_PORTMIDI=
export DEB_LDFLAGS_MAINT_APPEND = -ldl
endif

# Enable full building log when verbose output required
ifdef DH_VERBOSE
DEB_OPTS += VERBOSE=1
endif

DEB_MAME_OPTS = \
    SDL_INI_PATH=/etc/mame \
    FULLNAME=mame \
    TARGET=mame \
    SUBTARGET=mame \
    TOOLS=1

ifndef DEBUG
# Disable -g entirely
CFLAGS := $(CFLAGS:-g=-g0)
endif

%:
	dh $@

override_dh_auto_build:
	$(MAKE) $(DEB_OPTS) $(DEB_MAME_OPTS)
	-mv mame64 mame #FIXME
	$(CURDIR)/mame -createconfig
	mv mame.ini default.mame.ini

override_dh_install:
	dh_install --fail-missing \
               --exclude=dir.txt \
               --exclude=LICENSE

override_dh_fixperms-indep:
	dh_fixperms
	find debian/mame-data/usr/share/games/mame/artwork -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/ctrlr -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/hash -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/keymaps -type f -execdir chmod 644 {} \;


# Retrieve source from svn repo (recommended by upstream developers)
VERSION := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p')
TMPDIR := $(shell mktemp --dry-run --directory -p .)

MAME_PACKAGE = mame
# Upstream source has no dot in its name, so we have to cut it out
MAME_VERSION := $(shell echo $(VERSION) | sed -e 's/\.//' )
MAME_SRCDIR = $(MAME_PACKAGE)-$(VERSION)
MAME_TARBALL = $(MAME_PACKAGE)_$(VERSION).orig.tar.xz

get-orig-source:
	mkdir -p $(TMPDIR)/$(MAME_SRCDIR)
	wget -O source.tar.gz \
        https://github.com/mamedev/mame/archive/$(MAME_PACKAGE)$(MAME_VERSION).tar.gz
	tar -xzf source.tar.gz --strip-component=1 -C $(TMPDIR)/$(MAME_SRCDIR)/ \
        --exclude=\.*
	# Remove binaries and other cruft
	cd $(TMPDIR)/$(MAME_SRCDIR) && make clean && make cleansrc
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/.mame_version
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/zlib/contrib/dotzlib/DotZLib.chm
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/bx/tools/bin/linux/genie
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/bx/tools/bin/linux/ninja
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/bx/tools/bin/windows/genie.exe
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/bx/tools/bin/windows/ninja.exe
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/bgfx/3rdparty/remotery/vis/extern/BrowserLib/Core/Code/Convert.js
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/bgfx/3rdparty/scintilla/src/Editor.cxx.orig
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/bgfx/3rdparty/scintilla/gtk/makefile.orig
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/lzma/7zr.exe
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/lzma/lzma.exe
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/portmidi/pm_java/JavaExe.exe
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/portmidi/pm_java/UpdateRsrcJavaExe.exe
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/portmidi/pm_csharp/pm_managed/app.rc
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/portmidi/pm_csharp/pm_managed/resource.h
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/portmidi/pm_java/pmjni/pmjni.rc
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/portmidi/pm_java/setup/pmdefaults-setup.exe
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/android-project/gradle/wrapper/gradle-wrapper.jar
	rm -rf $(TMPDIR)/$(MAME_SRCDIR)/3rdparty/zlib/contrib/dotzlib/DotZLib.chm
	wget https://github.com/mamedev/build/archive/master.zip
	unzip -oqjd $(TMPDIR) master.zip \
        *mamedirs.zip *whatsnew_$(MAME_VERSION).txt
	mv $(TMPDIR)/whatsnew_$(MAME_VERSION).txt \
        $(TMPDIR)/$(MAME_SRCDIR)/whatsnew.txt
	unzip -oqd $(TMPDIR)/$(MAME_SRCDIR)/ $(TMPDIR)/mamedirs.zip
	tar -C $(TMPDIR) -cf - $(MAME_SRCDIR) | xz -9c > $(MAME_TARBALL)
	rm -rf $(TMPDIR) source.tar.gz master.zip

.PHONY: get-orig-source
