#!/usr/bin/make -f

# Install the win32-loader-standalone.exe as debian/tools/win32-loader/$(SUITE)/win32-loader.exe on the mirrors
BYHAND ?= yes

W32_VERSION := $(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')
W32_BYHAND_NAME := win32-loader_$(W32_VERSION)_all

PACKAGES_LIST = $(shell dpkg-query -f='$${Package;-25} $${Version}\\n' -W grub-pc cpio-win32 gzip-win32 gpgv-win32 debian-archive-keyring loadlin )
NSIS_VERSION  = $(shell dpkg-query -f='$${Version}' -W nsis )

%:
	dh $@

override_dh_auto_build:
ifeq ($(BYHAND),yes)
	# Build the standalone version
	STANDALONE=yes \
		OUTFILE_NAME=$(W32_BYHAND_NAME).exe \
		dh_auto_build
	
	# Prepare the README file
	awk '{sub(/@PACKAGES_LIST@/,"$(PACKAGES_LIST)")}1 \
             {sub(/@NSIS_VERSION@/,"$(NSIS_VERSION)")}1 \
             {sub(/@W32_VERSION@/,"$(W32_VERSION)")}1' \
		debian/win32-loader_doc.txt > $(W32_BYHAND_NAME).txt
	cat debian/copyright >> $(W32_BYHAND_NAME).txt
endif
	# Build the cdrom version
	OUTFILE_NAME=win32-loader.exe \
		dh_auto_build

override_dh_auto_clean:
	dh_auto_clean
	rm -f $(W32_BYHAND_NAME).exe
	rm -f $(W32_BYHAND_NAME).txt

override_dh_builddeb:
	dh_builddeb
ifeq ($(BYHAND),yes)
	cp $(W32_BYHAND_NAME).exe ../
	dpkg-distaddfile $(W32_BYHAND_NAME).exe byhand -

	cp $(W32_BYHAND_NAME).txt ../
	dpkg-distaddfile $(W32_BYHAND_NAME).txt byhand -
endif
