#!/usr/bin/make -f

# 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)
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Set this variable to yes for release builds to disable debugging and enable
# optimizations.
# NOTE: DEB_BUILD_OPTIONS must have 'nostrip' otherwise debugging symbols will
# be stripped from binaries.
XBMC_RELEASE ?= yes
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
XBMC_RELEASE ?= no
endif

# Whether we're building for release or not
ifeq (yes,$(XBMC_RELEASE))
  DEBUG_OPTIONS = --disable-debug --enable-optimizations
else
  DEBUG_OPTIONS = --enable-debug --disable-optimizations
endif

ifeq (,$(filter i386 amd64,$(DEB_HOST_ARCH)))
  ARCH_SPECIFIC_OPTIONS = --disable-crystalhd
else
  ARCH_SPECIFIC_OPTIONS = --enable-crystalhd
  ARCH_SPECIFIC_LIB_RECOMMENDS = -lcrystalhd
endif

# Enable gles for armel and armhf
# Also disable VDPAU and VAAPI there since they break the build with gles
ifeq (,$(filter armel armhf,$(DEB_HOST_ARCH)))
GL_CONFIG_OPTIONS = --enable-gl --disable-gles --enable-vdpau
else
GL_CONFIG_OPTIONS = --disable-gl --enable-gles --disable-vdpau \
                    --disable-vaapi
endif

# Use package's version in XBMC
VERSION := $(shell dpkg-parsechangelog | sed -n 's/^Version: \(.*\)/\1/p')

# Various environment variables to set
DEB_CFLAGS ?=  $(shell dpkg-buildflags --get CPPFLAGS) \
  $(filter-out -g -O2, $(shell dpkg-buildflags --get CFLAGS)) \
  -DDEB_VERSION=\"\\\"$(VERSION)\\\"\"
DEB_CXXFLAGS ?= $(shell dpkg-buildflags --get CPPFLAGS) \
  $(filter-out -g -O2, $(shell dpkg-buildflags --get CXXFLAGS)) \
  -DDEB_VERSION=\"\\\"$(VERSION)\\\"\"
DEB_LDFLAGS ?= $(shell dpkg-buildflags --get LDFLAGS)
ENV_OPTIONS = CFLAGS="$(DEB_CFLAGS)" CXXFLAGS="$(DEB_CXXFLAGS)" \
  LDFLAGS="$(DEB_LDFLAGS)"

# List of options to pass to configure. Can be overridden.
# Extra options can simply be passed using XBMC_CONFIG_EXTRA_OPTIONS env
# variable.
XBMC_CONFIG_OPTIONS ?= --host=$(DEB_HOST_GNU_TYPE) \
  --build=$(DEB_BUILD_GNU_TYPE) \
  --prefix=/usr --docdir=/usr/share/doc/xbmc \
  $(DEBUG_OPTIONS) \
  $(GL_CONFIG_OPTIONS) \
  --disable-vdadecoder \
  --disable-vtbdecoder \
  --disable-openmax \
  --disable-tegra \
  --disable-profiling \
  --enable-joystick \
  --enable-xrandr \
  --disable-pulse \
  --enable-rtmp \
  --enable-ffmpeg-libvorbis \
  --disable-mid \
  --disable-hal \
  --enable-avahi \
  --disable-asap-codec \
  --enable-webserver \
  --enable-optical-drive \
  --enable-texturepacker \
  --enable-nfs \
  --disable-libcec \
  --disable-afpclient \
  --enable-airtunes \
  --enable-external-libraries \
  --enable-external-ffmpeg \
  --disable-dependency-tracking \
  --disable-dvdcss \
  --disable-non-free \
  $(ARCH_SPECIFIC_OPTIONS) \
  $(XBMC_CONFIG_EXTRA_OPTIONS) \
  $(ENV_OPTIONS)

# Use --parallel option only if debhelper supports it
DH_PARALLEL_OPT=$(shell dh_testdir --parallel 2>/dev/null && echo "--parallel")

# Used to show what config options are enabled
show-xbmc-config-options:
	$(info $(XBMC_CONFIG_OPTIONS))

%:
	dh $@ --with autoreconf $(DH_PARALLEL_OPT)

override_dh_clean:
	dh_clean libtool xbmc/config.h xbmc/stamp-h1
	find . -name config.status -o -name config.cache -o -name config.log \
		-exec rm -f "{}" \;

override_dh_auto_configure: configure
	./bootstrap
	./configure $(XBMC_CONFIG_OPTIONS)
	# Add Debian related notes to XBMC's original logo
	mkdir -p $(CURDIR)/debian/tmp/home/user
	HOME=$(CURDIR)/debian/tmp/home/user gimp -i -b '(load "debian/xcf2png.scm") (xcf2png "debian/xbmc-logo.xcf" "addons/skin.confluence/media/xbmc-logo.png") (gimp-quit 0)'

override_dh_auto_install:
	$(MAKE) -C lib/addons/script.module.pil
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) eventclients DESTDIR=$(CURDIR)/debian/tmp \
		WII_EXTRA_OPTS=-DCWIID_OLD

override_dh_install:
	dh_install --sourcedir=$(CURDIR)/debian/tmp -XLICENCE \
		-XLicence.txt -XLicense.txt -XLiberationSans-Regular.ttf \
		-XDejaVuSans.ttf -XDejaVuSans-Bold.ttf -XLICENSE.txt

override_dh_makeshlibs:
	# We don't install shared libraries in standard locations so don't run
	# dh_makeshlibs

override_dh_shlibdeps: debian/tmp/xbmc-bin-depends-dummy.so debian/tmp/xbmc-bin-recommends-dummy.so
	dh_shlibdeps -a -O--parallel \
		-l$(CURDIR)/debian/xbmc-bin/usr/lib/xbmc/system/players/dvdplayer
	# Need to manually add dependencies for dlopened libs.
	dpkg-shlibdeps -pdlopenlibs -edebian/tmp/xbmc-bin-depends-dummy.so -xlibc6 -O >>debian/xbmc-bin.substvars
	dpkg-shlibdeps -dRecommends -edebian/tmp/xbmc-bin-recommends-dummy.so -xlibc6 -O >>debian/xbmc-bin.substvars

debian/tmp/xbmc-bin-depends-dummy.so:
	mkdir -p debian/tmp
	cc -xc -shared -Wl,--no-as-needed -o $@ /dev/null \
		-lcurl-gnutls

debian/tmp/xbmc-bin-recommends-dummy.so:
	mkdir -p debian/tmp
	cc -xc -shared -Wl,--no-as-needed -o $@ /dev/null \
		-lass \
		-lbluray \
		-lFLAC \
		-lmad \
		-lmodplug \
		-lmp3lame \
		-lmpeg2 \
		-lnfs \
		-logg \
		-lplist \
		-lrtmp \
		-lshairport \
		-lvdpau \
		-lvorbis \
		-lvorbisenc \
		-lvorbisfile \
		$(ARCH_SPECIFIC_LIB_RECOMMENDS)

# Get original XBMC source and cut not needed bits.
# Based on https://github.com/xbmc/xbmc-packaging/blob/master/xbmc-get-orig-source
ORIG_SRC_DIR := debian/tmp-src-dir
UPSTREAM_VER ?= $(shell dpkg-parsechangelog | sed -rne 's/^Version: ([0-9]:)?([0-9.]+)(\+dfsg\d+)?.*$$/\2/p')
DFSG_VER = $(UPSTREAM_VER)+dfsg1

get-orig-source:
	mkdir -p $(ORIG_SRC_DIR)
	wget -O $(ORIG_SRC_DIR)/xbmc-orig.tar.gz http://mirrors.xbmc.org/releases/source/xbmc-$(UPSTREAM_VER).tar.gz
	tar -C $(ORIG_SRC_DIR) -xf $(ORIG_SRC_DIR)/xbmc-orig.tar.gz
	rm $(ORIG_SRC_DIR)/xbmc-orig.tar.gz
	# Remove non-free components
	find $(ORIG_SRC_DIR)/* -depth \( -iname '*dvdcss*' -o -iname '*unrar*' \) -exec rm -rf "{}" \;
	# Remove components not needed for Linux
	rm -rf $(ORIG_SRC_DIR)/*/lib/afpfs-ng \
		$(ORIG_SRC_DIR)/*/lib/enca \
		$(ORIG_SRC_DIR)/*/lib/freetype \
		$(ORIG_SRC_DIR)/*/lib/gtest \
		$(ORIG_SRC_DIR)/*/lib/libass \
		$(ORIG_SRC_DIR)/*/lib/libbluray \
		$(ORIG_SRC_DIR)/*/lib/libcec \
		$(ORIG_SRC_DIR)/*/lib/liblame \
		$(ORIG_SRC_DIR)/*/lib/libmad \
		$(ORIG_SRC_DIR)/*/lib/libmicrohttpd \
		$(ORIG_SRC_DIR)/*/lib/libmodplug \
		$(ORIG_SRC_DIR)/*/lib/libmpeg2 \
		$(ORIG_SRC_DIR)/*/lib/libnfs \
		$(ORIG_SRC_DIR)/*/lib/librtmp \
		$(ORIG_SRC_DIR)/*/lib/libshairport \
		$(ORIG_SRC_DIR)/*/lib/libsidplay2 \
		$(ORIG_SRC_DIR)/*/lib/groovy \
		$(ORIG_SRC_DIR)/*/lib/taglib \
		$(ORIG_SRC_DIR)/*/lib/win32
	find $(ORIG_SRC_DIR)/* -depth \( \( -iname '*.so' -o -iname '*.exe' -o -iname '*.lib' \
		-o -iname '*.dll' -o -iname '*.dylib' -o -iname '*.a' -o -iname '*.obj' \
		\) -exec rm -rf "{}" \; \)
	# Remove components not needed for Linux when using external libs
	rm -rf $(ORIG_SRC_DIR)/*/lib/ffmpeg
	# Remove temp files and other cruft from source tarball
	# The find command snippet here was taken from debhelper's dh_clean command
	# with some modification to delete more unneeded files.
	find $(ORIG_SRC_DIR)/* -depth \( \( -type f -a \
		\( -name '#*#' -o -name '.*~' -o -name '*~' -o -name DEADJOE \
		-o -name '*.orig' -o -name '*.rej' -o -name '*.bak' \
		-o -name '.*.orig' -o -name .*.rej -o -name '.SUMS' \
		-o -name TAGS -o \( -path '*/.deps/*' -a -name '*.P' \) \
		-o -name config.status -o -name config.cache -o -name config.log \
		\) -exec rm -f "{}" \; \) -o \
		\( -type d -a -name autom4te.cache -prune -exec rm -rf "{}" \; \) \)
		rm $(ORIG_SRC_DIR)/*/.gitignore
	#Remove empty directories
	find $(ORIG_SRC_DIR)/* -type d -empty -delete
	(cd $(ORIG_SRC_DIR) && tar -cJf ../../xbmc_$(DFSG_VER).orig.tar.xz *)
	rm -rf $(ORIG_SRC_DIR)

.PHONY: get-orig-source
