#!/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.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

CFLAGS = -Wall -g

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

# Override relevant make variables in original makefile
DEB_MAME_OPTS = \
    OPT_FLAGS="-D'INI_PATH=\"/etc/mame\"'" \
    FULLNAME=mame \
    TARGET=mame \
    SUBTARGET=mame \
    OSD=sdl \
    CROSS_BUILD_OSD= \
    TARGETOS=unix \
    NOASM=1 \
    PREFIX= \
    SUFFIX= \
    DEBUG= \
    PROFILER= \
    NOWERROR= \
    MACOSX_USE_LIBSDL= \
    BUILD_EXPAT= \
    BUILD_ZLIB= \
    SYMBOLS= \
    SYMLEVEL= \
    DUMPSYM= \
    PROFILE= \
    MAP= \
    VERBOSE= \
    OPTIMIZE=3 \
    LDFLAGS="-Wl,--as-needed" \
    LDFLAGSEMULATOR="-Wl,--as-needed" \
    NOWERROR=1

DEB_MESS_OPTS = \
    $(DEB_MAME_OPTS) \
    OPT_FLAGS="-D'INI_PATH=\"/etc/mess\"'" \
    FULLNAME=mess \
    TARGET=mess \
    SUBTARGET=mess

# Override make variables for specific archs
# Linux architectures
ifeq ($(DEB_HOST_ARCH),amd64)
DEB_MAME_OPTS += \
    ARCHOPTS=-march=athlon64 \
    FORCE_DRC_C_BACKEND= \
    PTR64=1 \
    NOASM= \
    BIGENDIAN=
endif

ifeq ($(DEB_HOST_ARCH),i386)
DEB_MAME_OPTS += \
    ARCHOPTS=-march=pentium2 \
    FORCE_DRC_C_BACKEND= \
    PTR64= \
    NOASM= \
    BIGENDIAN=
endif

ifeq ($(DEB_HOST_ARCH),ia64)
DEB_MAME_OPTS += \
    FORCE_DRC_C_BACKEND=1 \
    PTR64=1 \
    NOASM=1 \
    BIGENDIAN=
endif

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

ifeq ($(DEB_HOST_ARCH),mipsel)
DEB_MAME_OPTS += \
    ARCHOPTS=-Umips \
    FORCE_DRC_C_BACKEND=1
endif

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

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

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

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

# kFreeBSD architectures
ifeq ($(DEB_HOST_ARCH),kfreebsd-amd64)
DEB_MAME_OPTS += \
    TARGETOS=freebsd \
    PTR64=1 \
    FORCE_DRC_C_BACKEND= \
    NOASM= \
    BIGENDIAN=
endif

ifeq ($(DEB_HOST_ARCH),kfreebsd-i386)
DEB_MAME_OPTS += \
    TARGETOS=freebsd \
    FORCE_DRC_C_BACKEND= \
    PTR64= \
    NOASM= \
    BIGENDIAN=
endif

%:
	dh $@

override_dh_auto_build:
	$(MAKE) -j3 $(DEB_MESS_OPTS) all
	$(CURDIR)/mess -createconfig
	mv mess.ini default.mess.ini

override_dh_auto_clean:
	echo "Starting clean process..."
	$(MAKE) $(DEB_MESS_OPTS) clean
	[ ! -f default.mess.ini ] || rm default.mess.ini

# dh_installchangelogs doesn't know about whatsnew.txt,
# so we need to make an explicit call
override_dh_installchangelogs:
	dh_installchangelogs whatsnew.txt
	dh_installchangelogs -pmess docs/messnew.txt

# Remove Windows specific documentation
override_dh_installdocs:
	dh_installdocs --exclude=license.txt \
                   --exclude=windows.txt \
                   --exclude=newvideo.txt

# Remove OSX specific keymaps
override_dh_install:
	dh_install --exclude=OSX.txt


# Convert upstream sources from zip to tar.xz
VERSION := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p')

MESS_PACKAGE = mess
MESS_FILEBASE = $(MESS_PACKAGE)$(VERSION)
MESS_URL = http://mess.redump.net/_media/downloads:

AGENT = Debian MAME/MESS Packagers <mame@packages.debian.org>
TMPDIR := $(shell mktemp --dry-run --directory -p .)

# Upstream zip file does not have a dot in its name, so we have to cut it out
UPSTREAM_VERSION := $(shell echo $(VERSION) | sed -e 's/\.//' )

$(MESS_FILEBASE)s.zip:
	wget -U '$(AGENT)' \
        "$(MESS_URL)$(MESS_PACKAGE)$(UPSTREAM_VERSION)s.zip" \
        -O $(MESS_FILEBASE)s.zip

get-orig-source: $(MESS_FILEBASE)s.zip
	mkdir $(TMPDIR)

	# Create an orig.tar.xz tarball for MESS
	mkdir -p $(TMPDIR)/$(MESS_PACKAGE)-$(VERSION)
	unzip $(MESS_FILEBASE)s.zip -d $(TMPDIR)/$(MESS_PACKAGE)-$(VERSION)
	# Strip trailing CR
	find $(TMPDIR)/$(MESS_PACKAGE)-$(VERSION) \
	    -type f -regextype posix-egrep \
	    ! -regex ".*(zip|gif|png|bmp|ico)" \
	    | sed 's/\ /\\\ /g' \
	    | xargs sed -i "s/\r$$//"
	tar -C $(TMPDIR) -cvf - $(MESS_PACKAGE)-$(VERSION) | \
	    xz > ../$(MESS_PACKAGE)_$(VERSION).orig.tar.xz
	rm -f $(MESS_FILEBASE)s.zip

	rm -rf $(TMPDIR)
