# Template file for 'xbmc-rpi'
pkgname=xbmc-rpi
version=12.3
revision=2
patch_args="-Np1"
wrksrc="xbmc-${version}-Frodo"
short_desc="XBMC Media Center for the Raspberry Pi"
maintainer="Juan RP <xtraeme@gmail.com>"
homepage="http://www.xbmc.org"
license="GPL-2"
distfiles="http://mirrors.xbmc.org/releases/source/xbmc-$version.tar.gz"
checksum=3e15c960d034efdea5f92a7b74716cb48094842d077b076025fd8640754ede73

only_for_archs="armv6l"
# Due to bootstrap requiring java, it's only possible currently to cross
# compile it from x86.
if [ "$XBPS_MACHINE" != "i686" -a "$XBPS_MACHINE" != "x86_64" ]; then
	msg_error "${pkgname}-${version}: can only be cross built on x86."
fi

hostmakedepends="
 automake libtool pkg-config gperf cmake zip unzip nasm yasm
 swig jre which gettext-devel libltdl-devel python-devel libmysqlclient-devel
 SDL_image-devel lzo-devel optipng"
makedepends="
 libudev-devel>=183 pcre-devel>=8.30 expat-devel libpng-devel>=1.6 libjpeg-turbo-devel
 avahi-libs-devel alsa-lib-devel samba-devel tiff-devel libmysqlclient-devel>=5.5.27
 libmpeg2-devel wavpack-devel zlib-devel lzo-devel fribidi-devel sqlite-devel freetype-devel
 jasper-devel faac-devel faad2-devel libmodplug-devel openssl-devel libass-devel
 libmad-devel fontconfig-devel libsamplerate-devel libmms-devel libcurl-devel
 enca-devel boost-devel libbluetooth-devel yajl-devel libplist-devel librtmp-devel
 tinyxml-devel taglib-devel libcap-devel lame-devel libnfs-devel systemd-devel
 libcec-devel libbluray-devel libmicrohttpd-devel libcdio-devel python-devel
 libdvdread-devel libssh-devel rpi-firmware"
makedepends+=" hicolor-icon-theme desktop-file-utils"
# The following dependencies are dlopen(3)ed.
depends="libbluray libmad libogg libcurl libflac libmodplug libass libmpeg2 lame librtmp libnfs libplist"
depends+=" hicolor-icon-theme desktop-file-utils"

provides="xbmc-${version}_${revision}"
# Create xbmc system user to launch xbmc-standalone.
system_accounts="xbmc"
xbmc_homedir="/var/lib/xbmc"
xbmc_groups="audio,video"

pre_configure() {
	. /etc/profile.d/java.sh
	./bootstrap

	# fix lsb_release dependency
	sed -i -e 's:/usr/bin/lsb_release -d:/bin/true:' xbmc/utils/SystemInfo.cpp

	# Use cross prefix for python include/libdir.
	sed -e "s,-L\$ac_python_libdir,-L${XBPS_CROSS_BASE}/usr/lib,g" -i configure
	sed -e "s,PYTHON_CPPFLAGS=\$python_path,PYTHON_CPPFLAGS=-I${XBPS_CROSS_BASE}/usr/include/python2.7,g" -i configure

	# Optimize png files.
	OLDIFS=$IFS
	IFS=$'\n'
	PNGFILES=($(find ${wrksrc} -type f -name *.png))
	IFS=$OLDIFS
	len=${#PNGFILES[@]}
	for ((i=0; i<${len}; i++)); do
		fname="$(basename ${PNGFILES[$i]})"
		[ "$fname" = "AppIcon.png" ] && continue
		optipng "${PNGFILES[$i]}"
	done
}

do_configure() {
	. /etc/profile.d/java.sh
	CFLAGS="-O3 -mcpu=arm1176jzf-s -mtune=arm1176jzf-s -mfloat-abi=hard -mfpu=vfp -mabi=aapcs-linux"
	CFLAGS+=" -I${XBPS_CROSS_BASE}/opt/vc/include/ -I${XBPS_CROSS_BASE}/opt/vc/include/IL"
	CFLAGS+=" -I${XBPS_CROSS_BASE}/opt/vc/include/interface/vcos/pthreads"
	CFLAGS+=" -I${XBPS_CROSS_BASE}/opt/vc/include/interface/vmcs_host/linux"
	CXXFLAGS="$CFLAGS"
	LDFLAGS+=" -L${XBPS_CROSS_BASE}/opt/vc/lib"

	export CFLAGS CXXFLAGS LDFLAGS

	./configure ${configure_args} \
		--disable-gl \
		--disable-sdl \
		--disable-x11 \
		--disable-xrandr \
		--disable-openmax \
		--disable-joystick \
		--disable-debug \
		--disable-crystalhd \
		--disable-vtbdecoder \
		--disable-vaapi \
		--disable-vdpau \
		--disable-pulse \
 		--disable-projectm \
		--with-platform=raspberry-pi \
		--enable-gles \
		--enable-dvdcss \
		--enable-optical-drive \
		--enable-libbluray \
		--enable-optimizations \
		--enable-libcec \
		--enable-player=omxplayer

	sed -e "s,\$(DVDREAD_CFLAGS),,g;s,AR=ar,AR=$AR,g;s,LD=ld,LD=$LD,g;s,RANLIB=ranlib,RANLIB=$RANLIB,g" -i lib/libdvd/libdvd{nav,read}/Makefile
	sed -e 's,-L/usr/lib,,g' -i lib/cmyth/Makefile
}

do_build() {
	make ${makejobs}
}

do_install() {
	make DESTDIR=${DESTDIR} install

	# Remove checks that don't apply to the raspberry pi
	head -n 171 ${DESTDIR}/usr/share/xbmc/FEH.py > ${DESTDIR}/usr/share/xbmc/FEH.py.new
	mv ${DESTDIR}/usr/share/xbmc/FEH.py.new ${DESTDIR}/usr/share/xbmc/FEH.py

	vinstall tools/Linux/xbmc.desktop 644 usr/share/applications
	vinstall tools/Linux/xbmc-48x48.png 644 usr/share/pixmaps xbmc.png

	rm -f ${DESTDIR}/usr/share/icons/hicolor/icon-theme.cache

	vinstall ${FILESDIR}/xbmc.service 0644 usr/lib/systemd/system
	vinstall ${FILESDIR}/polkit.rules 0644 etc/polkit-1/rules.d 10-xbmc.rules
}
