#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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


# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Rational http://www.winehq.org/pipermail/wine-bugs/2007-July/062505.html
LDFLAGS = 
CFLAGS = -Wall -g -mtune=corei7-avx -mfpmath=sse -msse3


ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Support passing of parallel=<n> in build options
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif


ifeq ($(DEB_BUILD_ARCH), amd64)
	# Support for 64-bit windows binaries is disabled for now until
	# Wine can be built with both 64 and 32 bit support
	# CONFFLAGS += --enable-win64

	# Wine installs 32 bit libraries, place those into /usr/lib32
	CONFFLAGS += --libdir=\$${prefix}/lib32
endif

config.status: configure
	dh_testdir
	dh_quilt_patch
	# Add here commands to configure the package.

	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info $(CONFFLAGS)

build: build-stamp
build-stamp:  config.status
	dh_testdir

	#### PGO-build was disabled due to apparent sound regression
	## Use pgo-data (uuencoded to allow binary existence in debian/ folder)
	#mkdir -p $(CURDIR)/debian/tmp
	#cp -f $(CURDIR)/debian/pgo.tar.coded $(CURDIR)/debian/tmp
	#uudecode -o $(CURDIR)/debian/tmp/pgo.tar $(CURDIR)/debian/tmp/pgo.tar.coded
	#tar -C $(CURDIR)/debian/tmp -xvvf $(CURDIR)/debian/tmp/pgo.tar

	# Add here commands to compile the package.
	# $(MAKE) depend
	#$(MAKE) EXTRACFLAGS="-Wcoverage-mismatch -fprofile-use -fprofile-dir=`pwd`/debian/tmp/pgo" LDFLAGS="-fprofile-use -fprofile-dir=`pwd`/debian/tmp/pgo" 
	$(MAKE)

	#### PGO-build was disabled due to apparent sound regression
	# Clean up pgo data (so dh_install --fail-missing doesn't complain)
	#rm $(CURDIR)/debian/tmp/pgo.tar
	#rm $(CURDIR)/debian/tmp/pgo.tar.coded
	#rm -rf $(CURDIR)/debian/tmp/pgo
	####

	#### Make the documentation.  The dh_installdocs command should install them
	# Remember to have the files wine.docs and wine-dev.docs point to any new
	# documentation files made
	# As of 20050524 as the documentation was split into another package
	# and all that's here are the foreign language readmes and the manpages.
	# It should be compiled automatically as the package compiles
	# $(MAKE) -C documentation txt
	####

	touch build-stamp

clean: 
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	#-$(MAKE) -C documentation clean

#ifneq "$(wildcard /usr/share/misc/config.sub)" ""
#	cp -f /usr/share/misc/config.sub config.sub
#endif
#ifneq "$(wildcard /usr/share/misc/config.guess)" ""
#	cp -f /usr/share/misc/config.guess config.guess
#endif

	dh_quilt_unpatch
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/tmp
	$(MAKE) install prefix=$(CURDIR)/debian/tmp/usr

	# Create a system-wide Wine Applications folder
	mkdir -p $(CURDIR)/debian/tmp/etc/xdg/menus/applications-merged
	cp -f $(CURDIR)/debian/wine.menu $(CURDIR)/debian/tmp/etc/xdg/menus/applications-merged

	# Install application-specific desktop files
	mkdir -p $(CURDIR)/debian/tmp/usr/share/applications
	cp -f $(CURDIR)/debian/wine-uninstaller.desktop $(CURDIR)/debian/tmp/usr/share/applications/
	cp -f $(CURDIR)/debian/wine-notepad.desktop $(CURDIR)/debian/tmp/usr/share/applications/
	cp -f $(CURDIR)/debian/wine-browsedrive.desktop $(CURDIR)/debian/tmp/usr/share/applications/
	cp -f $(CURDIR)/debian/wine-winecfg.desktop $(CURDIR)/debian/tmp/usr/share/applications/

	mkdir -p $(CURDIR)/debian/tmp/usr/share/desktop-directories/
	cp -f $(CURDIR)/debian/wine-wine.directory $(CURDIR)/debian/tmp/usr/share/desktop-directories/
	cp -f $(CURDIR)/debian/wine-Programs.directory $(CURDIR)/debian/tmp/usr/share/desktop-directories/
	cp -f $(CURDIR)/debian/wine-Programs-Accessories.directory $(CURDIR)/debian/tmp/usr/share/desktop-directories/
	# Drop MIME-sensing .desktop file: execution must use binfmt-support
	# Re-enabled for usability purposes, ultimately to be replaced by exe-handler
	# rm -f $(CURDIR)/debian/tmp/usr/share/applications/wine.desktop

	# Install icons (uuencoded to allow binary existence in debian/ folder)
	# These should be moved to be part of the icon themes upstream, but it hasn't happened yet
	cp -f $(CURDIR)/debian/icons.tar.coded $(CURDIR)/debian/tmp
	uudecode -o $(CURDIR)/debian/tmp/icons.tar $(CURDIR)/debian/tmp/icons.tar.coded
	tar -C $(CURDIR)/debian/tmp -xf $(CURDIR)/debian/tmp/icons.tar
	mkdir -p $(CURDIR)/debian/wine1.3/usr/share/icons
	mv $(CURDIR)/debian/tmp/icons $(CURDIR)/debian/wine1.3/usr/share/icons/hicolor
	# Clean up (so dh_install --fail-missing doesn't complain)
	rm $(CURDIR)/debian/tmp/icons.tar
	rm $(CURDIR)/debian/tmp/icons.tar.coded

	# Set up binfmt-support file
	mkdir -p $(CURDIR)/debian/tmp/usr/share/binfmts
	cp $(CURDIR)/debian/wine.binfmt $(CURDIR)/debian/tmp/usr/share/binfmts/wine

	# Set up wineserver restart notificition
	mkdir -p $(CURDIR)/debian/tmp/usr/share/wine/
	cp -f $(CURDIR)/debian/wineserver-restart-required.update-notifier $(CURDIR)/debian/tmp/usr/share/wine/


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	# The changelog was removed upstream in 1.0-rc4 as it was very large
	dh_installchangelogs
	dh_installdocs -a
	dh_lintian
	dh_installexamples
	dh_install --fail-missing --sourcedir=debian/tmp
	dh_installmime
	dh_installman
	dh_installdebconf
	dh_link
	dh_strip -Xwine-pthread -Xwine-kthread --dbg-package=wine1.3-dbg
	dh_compress
	dh_fixperms
	dh_icons
	dh_makeshlibs
	dh_installdeb
ifeq ($(DEB_BUILD_ARCH), amd64)
	dh_gencontrol -- -V"shlibs:Depends= ia32-libs (>= 1.6), lib32asound2 (>> 1.0.14), libc6-i386 (>= 2.6-1), lib32nss-mdns (>= 0.10-3)"
else
	dh_shlibdeps -L wine -l debian/wine/usr/lib
	dh_gencontrol
endif
	dh_md5sums
	dh_builddeb -- -Z lzma

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
