#!/usr/bin/make -f

export DH_VERBOSE=1

# unfortunately wine fails to build with pie enabled
export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie

VERSUFFIX=$(shell dpkg-parsechangelog | grep ^Source | cut -d\  -f2 | sed s/wine//g)

DATADIR=/usr/share/wine$(VERSUFFIX)
LIBDIR=usr/lib/$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)/wine$(VERSUFFIX)

# linker options
export DEB_LDFLAGS_MAINT_APPEND+=-Wl,-rpath,/$(LIBDIR)

# use freebsd-glue on kfreebsd architectures
ifeq ($(DEB_BUILD_ARCH_OS), kfreebsd)
export DEB_LDFLAGS_MAINT_APPEND+=-lfreebsd-glue
endif

# configure options
CONFLAGS=--without-hal \
	 --without-v4l \
	 --without-sane \
	 --without-capi \
	 --without-gphoto \
	 --disable-tests \
	 --prefix=$(DATADIR) \
	 --libdir=/$(LIBDIR) \
	 --mandir=/usr/share/man \
	 --datarootdir=$(DATADIR) \
	 --exec-prefix=/$(LIBDIR) \
	 --includedir=/usr/include \
         $(shell dpkg-buildflags --export=configure) \

# enable wine64 on amd64
ifeq ($(DEB_BUILD_ARCH_CPU), amd64)
CONFLAGS+=--enable-win64
endif

# additional files to generate
INSTALLS=$(shell ls debian/*-common | sed s/-common// | sed s/\\./$(VERSUFFIX)\\./)

debian/control: debian/control.in
	sed -e 's/\(wine[a-z0-9_-]*\)/\1$(VERSUFFIX)/g' \
	    -e 's/\(gecko[a-z0-9_-]*\)$(VERSUFFIX)/\1/' \
	    -e 's/pkg-wine$(VERSUFFIX)\/wine$(VERSUFFIX)\.git/pkg-wine\/wine.git/g' \
	    -e 's/pkg-wine-party$(VERSUFFIX)/pkg-wine-party/g' \
	    -e 's/winehq$(VERSUFFIX)/winehq/g' $< > $@

debian/%$(VERSUFFIX).docs: debian/%.docs-common
	cp $< $@

debian/%$(VERSUFFIX).links: debian/%.links-common
	cp $< $@

debian/%$(VERSUFFIX).install: debian/%.install-common
	cp $< $@

debian/%$(VERSUFFIX).manpages: debian/%.manpages-common
	cp $< $@

debian/%$(VERSUFFIX).lintian-overrides: debian/%.lintian-overrides-common
	cp $< $@

%:
	dh $@ --parallel --with autoreconf

override_dh_auto_configure:
	ln -s /usr/share/misc/config.sub tools/config.sub
	ln -s /usr/share/misc/config.guess tools/config.guess
ifneq ($(DEB_BUILD_ARCH), kfreebsd-amd64)
	dh_auto_configure -- $(CONFLAGS)
endif

override_dh_install: $(INSTALLS)
ifneq ($(DEB_BUILD_ARCH), kfreebsd-amd64)
	mkdir -p debian/tmp/usr/bin
	ln -s ../../$(LIBDIR)/bin/wine32 debian/tmp/usr/bin/wine32 || true
	ln -s ../../$(LIBDIR)/bin/wine64 debian/tmp/usr/bin/wine64 || true
	cd debian/tmp/$(LIBDIR)/bin && mv wine64 wine || true
	cd debian/tmp/$(LIBDIR)/bin && mv wine64-preloader wine-preloader || true
	cd debian/tmp/$(LIBDIR)/bin && ln -s wine wine32 || true
	cd debian/tmp/$(LIBDIR)/bin && ln -s wine wine64 || true
	cd debian/tmp/$(LIBDIR)/bin && ln -s wine-preloader wine32-preloader || true
	cd debian/tmp/$(LIBDIR)/bin && ln -s wine-preloader wine64-preloader || true
	cp debian/tmp/usr/share/man/man1/wine.1 debian/tmp/usr/share/man/man1/wine32.1
	cp debian/tmp/usr/share/man/man1/wine.1 debian/tmp/usr/share/man/man1/wine64.1
	test -f debian/tmp/$(LIBDIR)/bin/wine-preloader || \
	    cp debian/scripts/wine-preloader debian/tmp/$(LIBDIR)/bin
	sed 's|LIBDIR|$(LIBDIR)|g' < debian/scripts/winegcc > debian/tmp/winegcc$(DEB_BUILD_ARCH_BITS)
	dh_install
endif

override_dh_strip:
	dh_strip -Xwine-pthread -Xwine-kthread --dbg-package=libwine-dbg$(VERSUFFIX)
	rm -rf debian/libwine-dbg$(VERSUFFIX)/usr/lib/debug/usr/bin

override_dh_clean:
	dh_clean
	rm -rf debian/libwine-dbg$(VERSUFFIX)
	rm -f config.log configure tools/config.sub tools/config.guess
	rm -f dlls/*.def tools/makedep loader/wine_info.plist # random uncleaned files
	rm -f $(INSTALLS) debian/control
	make -f debian/rules debian/control
