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

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

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

%:
	dh $@

override_dh_installchangelogs:
	dh_installchangelogs -Xhistory

# Convert upstream sources from zip to tar.xz
VERSION := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p')
MAME_PACKAGE = mame-extra
MAME_VERSION := $(shell echo $(VERSION) | sed -e 's/\.//' )
MAME_EXTRA_TARBALL = $(MAME_PACKAGE)_$(VERSION).orig.tar.xz

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


HISTORY_PACKAGE = history
# 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

NPLAYERS_PACKAGE = nplayers
NPLAYERS_URL = http://nplayers.arcadebelgium.be/files/
NPLAYERS_SRCFILE = $(NPLAYERS_PACKAGE)$(MAME_VERSION).zip

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

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

$(MAME_EXTRA_TARBALL): $(HISTORY_SRCFILE) $(NPLAYERS_SRCFILE)
	mkdir -p $(TMPDIR)/$(MAME_PACKAGE)-$(VERSION)
	7z x -o$(TMPDIR)/$(MAME_PACKAGE)-$(VERSION)/$(HISTORY_PACKAGE) \
		$(HISTORY_SRCFILE)
	unzip $(NPLAYERS_SRCFILE) \
		-d $(TMPDIR)/$(MAME_PACKAGE)-$(VERSION)/$(NPLAYERS_PACKAGE)
	tar -C $(TMPDIR) -acvf $(MAME_EXTRA_TARBALL) \
		$(MAME_PACKAGE)-$(VERSION)
	rm -rf $(TMPDIR)
	rm -f $(HISTORY_SRCFILE)
	rm -f $(NPLAYERS_SRCFILE)

get-orig-source: $(MAME_EXTRA_TARBALL)

.PHONY: get-orig-source
