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

# 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 = \
    OSD=sdl \
    CROSS_BUILD_OSD= \
    NOASM=1 \
    PREFIX= \
    SUFFIX= \
    DEBUG= \
    PROFILER= \
    NOWERROR=1 \
    MACOSX_USE_LIBSDL= \
    BUILD_EXPAT= \
    BUILD_ZLIB= \
    BUILD_JPEGLIB= \
    BUILD_FLAC= \
    SYMBOLS= \
    SYMLEVEL= \
    DUMPSYM= \
    PROFILE= \
    MAP= \
    VERBOSE= \
    OPTIMIZE= \
    LDFLAGS="-Wl,--as-needed" \
    LDFLAGSEMULATOR="-Wl,--as-needed"

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

ifeq ($(DEB_HOST_ARCH),i386)
DEB_OPTS += \
    ARCHOPTS=-mtune=generic \
    FORCE_DRC_C_BACKEND= \
    PTR64= \
    NOASM= \
    BIGENDIAN=
endif

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

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

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

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

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

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

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

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

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

DEB_MAME_OPTS = \
    OPT_FLAGS="-D'INI_PATH=\"/etc/mame\"'" \
    FULLNAME=mame \
    TARGET=mame \
    SUBTARGET=mame

DEB_MESS_OPTS = \
    OPT_FLAGS="-D'INI_PATH=\"/etc/mess\"'" \
    FULLNAME=mess \
    TARGET=mess \
    SUBTARGET=mess

%:
	dh $@

override_dh_auto_build:
	$(MAKE) $(DEB_MAME_OPTS) $(DEB_OPTS)
	$(CURDIR)/mame -createconfig
	mv mame.ini default.mame.ini
# Force rebuild to allow Mess specific value for INI_PATH
	find obj -type f \
             -not -name \*.lh -and -not -name drivlist.c \
             -exec rm {} \;
	$(MAKE) $(DEB_MESS_OPTS) $(DEB_OPTS) all
	$(CURDIR)/mess -createconfig
	mv mess.ini default.mess.ini

override_dh_auto_clean:
	$(MAKE) $(DEB_MAME_OPTS) $(DEB_OPTS) clean
	$(MAKE) $(DEB_MESS_OPTS) $(DEB_OPTS) clean
	rm -rf obj
	rm -f default.mame.ini default.mess.ini

# 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')

AGENT = Debian MAME Packagers <mame@packages.debian.org>
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_URL = http://mamedev.org/downloader.php?file=releases/
MAME_SRCFILE = $(MAME_PACKAGE)$(MAME_VERSION)s.exe
MAME_TARBALL = $(MAME_PACKAGE)_$(VERSION).orig.tar.xz

$(MAME_SRCFILE):
	wget -U '$(AGENT)' $(MAME_URL)$(MAME_SRCFILE) -O $(MAME_SRCFILE)

$(MAME_TARBALL): $(MAME_SRCFILE)
	mkdir $(TMPDIR)
	7z x -o$(TMPDIR)/$(MAME_PACKAGE)-$(VERSION) $(MAME_SRCFILE)
	# Strip trailing CR
	find $(TMPDIR)/$(MAME_PACKAGE)-$(VERSION) \
	    -type f -regextype posix-egrep \
	    ! -regex ".*(zip|gif|png|bmp|ico)" \
	    | sed 's/\ /\\\ /g' \
	    | xargs sed -i "s/\r$$//"
	tar -C $(TMPDIR) -acvf $(MAME_TARBALL) $(MAME_PACKAGE)-$(VERSION)
	rm -rf $(TMPDIR)
	rm -f $(MAME_SRCFILE)

HISTORY_PACKAGE = mamehistory
# Upstream source file has no leading zero in version number, so cut it out
HISTORY_VERSION := $(shell echo $(VERSION) | sed -e 's/^0\.//' )
HISTORY_URL = http://www.arcade-history.com/dats/
HISTORY_SRCFILE = $(HISTORY_PACKAGE)$(HISTORY_VERSION).7z
HISTORY_TARBALL = $(MAME_PACKAGE)_$(VERSION).orig-$(HISTORY_PACKAGE).tar.xz

$(HISTORY_SRCFILE):
	wget -U '$(AGENT)' $(HISTORY_URL)$(HISTORY_SRCFILE)

$(HISTORY_TARBALL): $(HISTORY_SRCFILE)
	mkdir $(TMPDIR)
	7z x -o$(TMPDIR)/$(HISTORY_PACKAGE) $(HISTORY_SRCFILE)
	# Strip trailing CR
	find $(TMPDIR)/$(HISTORY_PACKAGE) -type f | xargs sed -i "s/\r$$//"
	tar -C $(TMPDIR) -acvf $(HISTORY_TARBALL) $(HISTORY_PACKAGE)
	rm -rf $(TMPDIR)
	rm -f $(HISTORY_SRCFILE)

NPLAYERS_PACKAGE = nplayers
NPLAYERS_URL = http://nplayers.arcadebelgium.be/files/
NPLAYERS_SRCFILE = $(NPLAYERS_PACKAGE)$(MAME_VERSION).zip
NPLAYERS_TARBALL = $(MAME_PACKAGE)_$(VERSION).orig-$(NPLAYERS_PACKAGE).tar.xz

$(NPLAYERS_SRCFILE):
	wget -U '$(AGENT)' $(NPLAYERS_URL)$(NPLAYERS_SRCFILE)

$(NPLAYERS_TARBALL): $(NPLAYERS_SRCFILE)
	mkdir $(TMPDIR)
	unzip $(NPLAYERS_SRCFILE) -d $(TMPDIR)/$(NPLAYERS_PACKAGE)
	# Strip trailing CR
	find $(TMPDIR)/$(NPLAYERS_PACKAGE) -type f | xargs sed -i "s/\r$$//"
	tar -C $(TMPDIR) -acvf $(NPLAYERS_TARBALL) $(NPLAYERS_PACKAGE)
	rm -rf $(TMPDIR)
	rm -f $(NPLAYERS_SRCFILE)

get-orig-source: $(MAME_TARBALL) $(HISTORY_TARBALL) $(NPLAYERS_TARBALL)

erase-upstream: clean
	rm -rf artwork
	rm -rf docs
	rm -rf hash
	rm -rf hlsl
	rm -rf keymaps
	rm -f makefile
	rm -rf obj
	rm -rf src
	rm -f whatsnew*.txt
	rm -rf mamehistory
	rm -rf nplayers
