#!/usr/bin/make -f

export DH_VERBOSE=1

dversion=$(shell dpkg-parsechangelog | grep ^Version | cut -d' ' -f2 | cut -d':' -f 2)
uversion=$(shell uscan --report --dehs |grep upstream-version | cut -d\< -f2 | cut -d\> -f2)
keepers=icons lib changelog steam.6 steam steam.desktop steam_subscriber_agreement.txt
orig=../steam_$(uversion).orig.tar.xz
dest=../steam-$(uversion).orig

build-arch:
ifneq ($(DEB_HOST_ARCH), i386)
	@echo "error: $(DEB_HOST_ARCH) is not a supported architecture"
	@exit 1
endif

%:
	dh $@

override_dh_auto_configure:
	./debian/scripts/templates-helper

override_dh_strip:
	# Valve's binaries have no symbols, so a dbgsym package is not useful
	dh_strip --no-automatic-dbgsym

override_dh_clean:
	dh_clean -- debian/templates
	# Disabled
	#./debian/scripts/copyright-helper debian/copyright.in debian/copyright

# Don't use an epoch for steam-installer
override_dh_gencontrol:
	dh_gencontrol -psteam-installer -- -v$(dversion)
	dh_gencontrol --remaining-packages

get-orig-source:
	@test "$(uversion)" = "" && \
	    echo "There is no new upstream source file." && exit 1 || true
	mkdir get-orig
	mkdir get-orig/chdist
	chdist -d $(CURDIR)/get-orig/chdist create steam http://repo.steampowered.com/steam precise steam
	cp debian/valve-steam-keyring.gpg get-orig/chdist/steam/etc/apt/trusted.gpg.d/
	chdist -d $(CURDIR)/get-orig/chdist apt-get steam update
	cd get-orig && chdist -d $(CURDIR)/get-orig/chdist apt-get steam source -d steam=$(uversion)
	rm -f $(orig)
	mkdir -p $(dest)
	tar xf get-orig/steam_$(uversion).tar.gz --strip-components=1 -C $(dest)
	rm -rf get-orig
	mv $(dest)/debian/changelog $(dest)
	rm -f $(dest)/steam
	cd $(dest) && tar xf bootstrap*.tar.* --strip-components=1
	cd $(dest) && for file in *; do \
	    echo $(keepers) | grep -q $$file || rm -rf $$file; done
	tar cJf $(orig) $(dest)
	rm -rf $(dest)
	@echo "Successfully created new upstream source file: $(orig)"
