#!/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_MESS_OPTS = \
    OPT_FLAGS="-D'INI_PATH=\"/etc/mess\"'" \
    FULLNAME=mess \
    TARGET=mess \
    SUBTARGET=mess \
    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=2 \
    LDFLAGS="-Wl,--as-needed" \
    LDFLAGSEMULATOR="-Wl,--as-needed"

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

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

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

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

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

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

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

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

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

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

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

%:
	dh $@

override_dh_auto_build:
	$(MAKE) $(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
	rm -rf default.mess.ini
	rm -rf obj

# 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://www.progettosnaps.net/MESS/src/

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)
