# vim:set ts=2 sw=2 et:
# $Id$
# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: Brad Fanella <bradfanella@archlinux.us>
# Contributor: [vEX] <niechift.dot.vex.at.gmail.dot.com>
# Contributor: Zeqadious <zeqadious.at.gmail.dot.com>
# Contributor: BlackIkeEagle < ike DOT devolder AT gmail DOT com >
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Maxime Gauduin <alucryd@gmail.com>

_prefix=/usr

pkgname=xbmc
pkgver=13.2
_codename=Gotham
pkgrel=8
pkgdesc="A software media player and entertainment hub for digital media"
arch=('i686' 'x86_64')
url="http://xbmc.org"
license=('GPL2')
depends=(
'avahi' 'hicolor-icon-theme' 'glu' 'lame' 'libass' 'libbluray' 'libcdio' 'libmad'
'libmicrohttpd' 'libmodplug' 'libmpeg2' 'libmysqlclient' 
'libsamplerate' 'libssh' 'libva' 'libvdpau' 'libvorbis' 'libxrandr' 'libxslt'
'lzo' 'mesa' 'mesa-demos' 'python2' 'python2-pillow' 'rtmpdump' 'sdl_image'
'smbclient' 'taglib' 'tinyxml' 'unzip' 'xorg-xdpyinfo' 'yajl'
)
makedepends=(
'afpfs-ng' 'libnfs' 'libplist' 'shairplay' 'libcec' 'boost' 'cmake' 'doxygen'
'gperf' 'jasper' 'java-runtime-headless' 'nasm' 'swig' 'taglib' 'udisks'
'zip'
)
optdepends=(
'afpfs-ng: Apple File Protocol suport'
'libnfs: NFS Share support'
'libplist: AirPlay support'
'shairplay: AirPlay support'
'libcec: Pulse-Eight USB-CEC adapter support'
'polkit: power management'
'lirc: remote controller support'
'unrar: access compressed files without unpacking them'
'udisks: automount external drives'
'upower: display battery level'
)
install="${pkgname}.install"
source=(
  "xbmc-$pkgver-$_codename.tar.gz::https://github.com/xbmc/xbmc/archive/$pkgver-$_codename.tar.gz"
  '0001-CEC-fixed-don-t-try-to-stick-a-value-255-in-a-byte.patch'
  '0002-CEC-bump-libCEC-to-2.2.0.patch'
  '0003-CEC-prevent-bus-rescan-on-a-Pi-since-the-adapter-can.patch'
  '0004-CEC-fixed-don-t-return-garbage-from-CEC-related-app-.patch'
  '0005-CEC-renamed-the-iDoubleTapTimeoutMs-in-the-new-libCE.patch'
  '0006-CEC-fixed-don-t-use-CEC_CLIENT_VERSION_CURRENT-for-t.patch'
  'xbmc.sh'
  'xbmc_lib.conf'
)

prepare() {
  cd "$srcdir/xbmc-$pkgver-$_codename"

  patch -Np1 -i "$srcdir/0001-CEC-fixed-don-t-try-to-stick-a-value-255-in-a-byte.patch"
  patch -Np1 -i "$srcdir/0002-CEC-bump-libCEC-to-2.2.0.patch"
  patch -Np1 -i "$srcdir/0003-CEC-prevent-bus-rescan-on-a-Pi-since-the-adapter-can.patch"
  patch -Np1 -i "$srcdir/0004-CEC-fixed-don-t-return-garbage-from-CEC-related-app-.patch"
  patch -Np1 -i "$srcdir/0005-CEC-renamed-the-iDoubleTapTimeoutMs-in-the-new-libCE.patch"
  patch -Np1 -i "$srcdir/0006-CEC-fixed-don-t-use-CEC_CLIENT_VERSION_CURRENT-for-t.patch"

  find -type f -name *.py -exec sed 's|^#!.*python$|#!/usr/bin/python2|' -i "{}" +
  sed 's|^#!.*python$|#!/usr/bin/python2|' -i tools/depends/native/rpl-native/rpl
  sed 's/python/python2/' -i tools/Linux/xbmc.sh.in
}

build() {
  cd "$srcdir/xbmc-$pkgver-$_codename"

  # Bootstrapping XBMC
  ./bootstrap

  # Configuring XBMC
  export PYTHON_VERSION=2  # external python v2
  ./configure --prefix=$_prefix --exec-prefix=$_prefix \
    --disable-debug \
    --enable-optimizations \
    --enable-gl \
    --enable-sdl \
    --enable-vaapi \
    --enable-vdpau \
    --enable-xvba \
    --enable-joystick \
    --enable-xrandr \
    --enable-rsxs \
    --enable-projectm \
    --enable-x11 \
    --enable-rtmp \
    --enable-samba \
    --enable-nfs \
    --enable-afpclient \
    --enable-airplay \
    --enable-airtunes \
    --enable-ffmpeg-libvorbis \
    --enable-dvdcss \
    --disable-hal \
    --enable-avahi \
    --enable-webserver \
    --enable-optical-drive \
    --enable-libbluray \
    --enable-texturepacker \
    --enable-udev \
    --enable-libusb \
    --enable-libcec \
    --with-lirc-device=/run/lirc/lircd

  # Now (finally) build
  make
}

package() {
  cd "$srcdir/xbmc-$pkgver-$_codename"
  # Running make install
  make DESTDIR="$pkgdir" install

  # Tools
  install -D -m 0755 "$srcdir/xbmc-$pkgver-$_codename/tools/TexturePacker/TexturePacker" \
    "${pkgdir}$_prefix/lib/xbmc/"

  # Licenses
  install -dm755 "${pkgdir}$_prefix/share/licenses/$pkgname"
  for licensef in LICENSE.GPL copying.txt; do
    mv "${pkgdir}$_prefix/share/doc/xbmc/$licensef" \
      "${pkgdir}$_prefix/share/licenses/$pkgname"
  done

  # LinHES stuff
  mkdir -p ${pkgdir}/usr/LH/bin
  install -D -m 0755 ${srcdir}/xbmc.sh ${pkgdir}/usr/LH/bin
  #add in xbmc_lib.conf
  install -D -m0744 ${srcdir}/xbmc_lib.conf ${pkgdir}/etc/gen_lib_xml.d/xbmc_lib.conf
}
md5sums=('d80153b2cc78b88966458ab136e9494f'
         'd00af955c000f285a230568004209117'
         '6ee855d2cd6ada08b2c72b5c15d5ae76'
         '3e810f451e58d5704cdd15b83552aa53'
         '693869eb72d696c3457e795722e707c6'
         'b6346f66286195189e41839e9a23ef87'
         '47b765e1d4299fac32bf92f108548d06'
         'e48a3d87984669ef206e902bc8290ff9'
         '71176cb0e5aaa230d7c9634c37245bfb')