# vim:set ts=2 sw=2 et:
# $Id$
# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Maintainer: BlackIkeEagle < ike DOT devolder AT gmail DOT 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>

pkgbase=kodi
pkgname=('kodi' 'kodi-eventclients')
pkgver=16.1
_codename=Jarvis
pkgrel=2
arch=('i686' 'x86_64')
url="http://kodi.tv"
license=('GPL2')
makedepends=(
  'afpfs-ng' 'bluez-libs' 'boost' 'cmake' 'curl' 'cwiid' 'doxygen' 'git' 'glew'
  'gperf' 'hicolor-icon-theme' 'jasper' 'java-runtime' 'libaacs' 'libass'
  'libbluray' 'libcdio' 'libcec' 'libgl' 'libmysqlclient' 'libmicrohttpd'
  'libmodplug' 'libmpeg2' 'libnfs' 'libplist' 'libssh' 'libva'
  'libvdpau' 'libxrandr' 'libxslt' 'lzo' 'mesa' 'nasm' 'nss-mdns'
  'python2-pillow' 'python2-pybluez' 'python2-simplejson' 'rtmpdump' 'sdl2'
  'sdl_image' 'shairplay' 'smbclient' 'swig' 'taglib' 'tinyxml' 'unzip' 'upower'
  'yajl' 'zip' 'mesa' 'dcadec' 'libcrossguid'
)
source=(
  "$pkgname-$pkgver-$_codename.tar.gz::https://github.com/xbmc/xbmc/archive/$pkgver-$_codename.tar.gz"
  'kodi.sh'
  'kodi_lib.conf'
)

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

  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/kodi.sh.in
  sed 's/shell python/shell python2/' -i tools/EventClients/Makefile.in

  # patches
}

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

	# Bootstrapping
  MAKEFLAGS=-j1 ./bootstrap

  #./configure --help
  #return 1

  # Configuring XBMC
  export PYTHON_VERSION=2  # external python v2
  ./configure --prefix=/usr --exec-prefix=/usr \
    --disable-debug \
    --enable-optimizations \
    --enable-libbluray \
    --enable-shared-libraries \
    --with-lirc-device=/run/lirc/lircd \
    ac_cv_type__Bool=yes

  # Now (finally) build
  make
}

package_kodi() {
  pkgdesc="A software media player and entertainment hub for digital media"

  # depends expected for kodi plugins:
  # 'python2-pillow' 'python2-pybluez' 'python2-simplejson'
  # depends expeced in FEH.py
  # 'mesa-demos' 'xorg-xdpyinfo'
  depends=(
    'python2-pillow' 'python2-pybluez' 'python2-simplejson'
    'mesa-demos' 'xorg-xdpyinfo'
    'bluez-libs' 'fribidi' 'glew' 'hicolor-icon-theme' 'libass' 'libcdio'
    'libjpeg-turbo' 'libmysqlclient' 'libmicrohttpd' 'libssh'
    'libva' 'libxrandr' 'libxslt' 'lzo' 'sdl2' 'smbclient' 'taglib' 'tinyxml'
    'yajl' 'mesa' 'dcadec' 'desktop-file-utils'
  )
  optdepends=(
    'afpfs-ng: Apple shares support'
    'bluez: Blutooth support'
    'libnfs: NFS shares support'
    'libplist: AirPlay support'
    'libcec: Pulse-Eight USB-CEC adapter support'
    'lirc: Remote controller support'
    'shairplay: AirPlay support'
    'udisks: Automount external drives'
    'unrar: Archives support'
    'unzip: Archives support'
    'upower: Display battery level'
    'lsb-release: log distro information in crashlog'
  )
  install="kodi.install"
  provides=('xbmc')
  conflicts=('xbmc')
  replaces=('xbmc')

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

  # We will no longer support the xbmc name
  rm "$pkgdir/usr/share/xsessions/xbmc.desktop"
  rm "$pkgdir/usr/bin/"xbmc{,-standalone}
  # we will leave /usr/{include,lib,share}/xbmc for now

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

  # LinHES stuff
  mkdir -p ${pkgdir}/usr/LH/bin
  install -D -m 0755 ${srcdir}/kodi.sh ${pkgdir}/usr/LH/bin
  #add in kodi_lib.conf
  install -D -m0744 ${srcdir}/kodi_lib.conf ${pkgdir}/etc/gen_lib_xml.d/kodi_lib.conf
}

package_kodi-eventclients() {
  pkgdesc="Kodi Event Clients"

  depends=('cwiid')

  cd "$srcdir/xbmc-$pkgver-$_codename"

  make DESTDIR="$pkgdir" eventclients WII_EXTRA_OPTS=-DCWIID_OLD
}

md5sums=('79358ad5f77f42c5498a9412d5a78dd5'
         'f6adc8e06d7076143ca2ad118126b158'
         'f04d1c89f5108c6aba36cf708a74cc95')