# Maintainer: Oleg Rakhmanov <oleg@archlinuxarm.org>
# Contributor: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: 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>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - i.MX6 changes

buildarch=4

_prefix=/usr

pkgbase=kodi
pkgname=('kodi-imx' 'kodi-imx-eventclients' 'kodi-imx-tools-texturepacker' 'kodi-imx-dev')
pkgver=17.4
_codename=Krypton
pkgrel=4
arch=('armv7h')
url="http://kodi.tv"
license=('GPL2')
makedepends=('hicolor-icon-theme' 'fribidi' 'lzo' 'smbclient' 'libtiff' 'libva' 'libpng' 'libcdio'
             'yajl' 'libmariadbclient' 'libjpeg-turbo' 'libsamplerate' 'libssh' 'libmicrohttpd'
             'sdl_image' 'python2' 'python2-pillow' 'python2-pybluez' 'python2-simplejson' 'libass'
             'libmpeg2' 'libmad' 'libmodplug' 'jasper' 'rtmpdump' 'unzip' 'xorg-xdpyinfo' 'libbluray'
             'libnfs' 'afpfs-ng' 'avahi' 'bluez-libs' 'tinyxml'
             'libplist' 'swig' 'taglib' 'libxslt' 'shairplay' 'boost' 'cmake' 'gperf' 'nasm' 'zip'
             'upower' 'autoconf' 'java-environment' 'libcrossguid' 'dcadec' 'libpulse'
             'imx-vpuwrap' 'imx-gpu-viv-fb' 'libcec-imx6' 'firmware-imx' 'imx-gpu-viv-g2d' 'linux-imx6-headers')
source=("https://github.com/xbmc/xbmc/archive/$pkgver-$_codename.tar.gz"
        'kodi.service'
        'polkit.rules'
        'imx-hdmi-soc.conf'
        'imx-spdif.conf'
        'kodi.conf'
        'fix-python-lib-path.patch'
        'https://raw.githubusercontent.com/OpenELEC/OpenELEC.tv/master/packages/mediacenter/kodi/patches/non-rpi/kodi-PR11222.patch'
        'https://raw.githubusercontent.com/OpenELEC/OpenELEC.tv/master/packages/mediacenter/kodi/patches/imx6/kodi-0036-IMXCODEC-Honour-forced-aspect-ratio-hint.patch'
        'https://raw.githubusercontent.com/OpenELEC/OpenELEC.tv/master/packages/mediacenter/kodi/patches/imx6/kodi-0040-IMXCODEC-Prevent-lkFIFO-from-returning-uninitialized.patch'
        'https://raw.githubusercontent.com/OpenELEC/OpenELEC.tv/master/packages/mediacenter/kodi/patches/imx6/kodi-0044-IMXCODEC-Fix-hang-on-exit-and-invalid-file-descripto.patch'
        'https://raw.githubusercontent.com/OpenELEC/OpenELEC.tv/master/packages/mediacenter/kodi/patches/imx6/kodi-53-imx-fix-edid-readout.patch'
        'https://raw.githubusercontent.com/OpenELEC/OpenELEC.tv/master/packages/mediacenter/kodi/patches/imx6/kodi-58-imx-fractional-rates.patch')
sha256sums=('6b0886e7449fc201e0ec0584b37f9f654c429797a41e6d0b6a4b5a7fd5ec34dc'
            '75eae110b6ca1c844505a231b453377afb092ef3969a31c8f4c72a3c3247381a'
            '9ea592205023ba861603d74b63cdb73126c56372a366dc4cb7beb379073cbb96'
            '6d9822d97b9e1268c04078f6c9b6316eb013f95f96f90034b51fa7473b9752ff'
            '0e16e4d2ec0c0e1810fe1ad6a7c4dee99d6fc0f49502f0e836210f5dd537d92d'
            'db6372248015fa4e663bf789e4f7121b5a9ade323f361994bbd6f135ce169123'
            '1c07c9fdd8e2958262cf917e4266c4933fcd06529c111e3cb0cbaaa05c934033'
            '20ec2004427a557ed8224dafd158f76bd0c658f871c7eb6504323d68084db662'
            '01f84d2fd65b0d0a884fafe8947cbd1bd123b9be8a0dc629e75951cb361753e0'
            '7814f79c5d2c7248a463ff50498db0c0b0645dabc25b14f563c96c4fb25dd9a5'
            'e29279ffa431078559535fc3cc70ad93344ce027f3bcb823e7c8d06c5fb9e44b'
            'b39f6c0f9ec9103c1aff87d8ef95cd8185800cb94fe81ab1460fe003700c02cc'
            '517f9a515c5a9b5130abd7a9d70bd08de798b987c0fcd27860dd8e0658e9ed3b')

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

  # OpenELEC patches
  patch -Np1 -i "$srcdir/kodi-PR11222.patch"
  patch -Np1 -i "$srcdir/kodi-0036-IMXCODEC-Honour-forced-aspect-ratio-hint.patch"
  patch -Np1 -i "$srcdir/kodi-0040-IMXCODEC-Prevent-lkFIFO-from-returning-uninitialized.patch"
  patch -Np1 -i "$srcdir/kodi-0044-IMXCODEC-Fix-hang-on-exit-and-invalid-file-descripto.patch"
  patch -Np1 -i "$srcdir/kodi-53-imx-fix-edid-readout.patch"
  patch -Np1 -i "$srcdir/kodi-58-imx-fractional-rates.patch"

  #
  patch -p1 -i "$srcdir/fix-python-lib-path.patch"

  [[ -d kodi-build ]] && rm -rf kodi-build
  mkdir $srcdir/kodi-build
}

build() {
  cd kodi-build

  cmake -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=/usr/lib \
    -DCMAKE_PREFIX_PATH=/opt/fsl \
    -DCORE_SYSTEM_NAME=linux \
    -DWITH_CPU=${CARCH} \
    -DENABLE_EVENTCLIENTS=ON \
    -DENABLE_OPENGL=OFF \
    -DENABLE_OPENGLES=ON \
    -DENABLE_X11=OFF \
    -DENABLE_IMX=ON \
    -DENABLE_VAAPI=OFF \
    -DENABLE_VDPAU=OFF \
    -DENABLE_NONFREE=ON \
    -DENABLE_UPNP=ON \
    -DENABLE_CEC=ON \
    -DENABLE_OPENSSL=ON \
    -DENABLE_LIRC=ON \
    -DLIRC_DEVICE=/run/lirc/lircd \
    -DENABLE_INTERNAL_FFMPEG=1 \
    -DIMX_INCLUDE_DIR=/opt/fsl/include/ \
    -DFSLVPUWRAP_LIBRARY=/opt/fsl/lib/libfslvpuwrap.so \
    -DVPU_LIBRARY=/opt/fsl/lib/libvpu.so \
    -DG2D_LIBRARY=/opt/fsl/lib/libg2d.so \
    ../"xbmc-$pkgver-$_codename"/project/cmake

  make
  make preinstall
}

package_kodi-imx() {
  pkgdesc="A software media player and entertainment hub for digital media (Freescale i.MX6)"
  depends=('hicolor-icon-theme' 'fribidi' 'lzo' 'smbclient' 'libtiff' 'libva' 'libpng' 'libcdio'
           'yajl' 'libmariadbclient' 'libjpeg-turbo' 'libsamplerate' 'libssh' 'libmicrohttpd'
           'sdl_image' 'python2' 'python2-pillow' 'python2-pybluez' 'python2-simplejson' 'libass'
           'libmpeg2' 'libmad' 'libmodplug' 'jasper' 'rtmpdump' 'xorg-xdpyinfo' 'libbluray'
           'avahi' 'bluez-libs' 'tinyxml' 'libpulse' 'libplist' 'swig' 'taglib' 'libxslt' 'libcrossguid'
           'imx-vpuwrap' 'imx-gpu-viv-fb' 'libcec-imx6' 'firmware-imx' 'imx-gpu-viv-g2d')
  optdepends=(
    'afpfs-ng: Apple airplay and AFP share support'
    'libnfs: NFS shares support'
    'lirc: remote controller support'
    'lsb-release: log distro information in crashlog'
    'polkit: permissions for automounting external drives and power management functionality'
    'shairplay: limited Apple airplay support'
    'unrar: access compressed files without unpacking them'
    'unzip: access compressed files without unpacking them'
    'upower: used to trigger power management functionality')
  install='kodi.install'
  provides=('xbmc' 'kodi')
  conflicts=('xbmc' 'kodi' 'arm-mem-git' 'shairplay-git')
  replaces=('xbmc' 'xbmc-imx-git')
  _components=('kodi' 'kodi-bin')

  cd kodi-build
  # install eventclients
  for _cmp in ${_components[@]}; do
  DESTDIR="$pkgdir" /usr/bin/cmake \
    -DCMAKE_INSTALL_COMPONENT="$_cmp" \
     -P cmake_install.cmake
  done

  # 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

  # imx6-specific alsa conf
  install -Dm0644 $srcdir/imx-spdif.conf $pkgdir/usr/share/alsa/cards/imx-spdif.conf
  install -Dm0644 $srcdir/imx-hdmi-soc.conf $pkgdir/usr/share/alsa/cards/imx-hdmi-soc.conf

  # python2 is being used
  cd "$pkgdir"
  grep -lR '#!.*python' * | while read file; do sed -s 's/\(#!.*python\)/\12/g' -i "$file"; done

  install -Dm0644 "$srcdir/kodi.service" "$pkgdir/usr/lib/systemd/system/kodi.service"
  install -Dm0644 "$srcdir/polkit.rules" "$pkgdir/usr/share/polkit-1/rules.d/10-kodi.rules"
  install -Dm0644 "$srcdir/kodi.conf" "$pkgdir/etc/tmpfiles.d/kodi.conf"
  chmod 0750 "$pkgdir/usr/share/polkit-1/rules.d/"
}

package_kodi-imx-eventclients() {
  pkgdesc="Kodi event clients (Freescale i.MX6)"
  provides=('kodi-eventclients')
  conflicts=('kodi-eventclients')
  _components=('kodi-eventclients-common'
    'kodi-eventclients-ps3'
    'kodi-eventclients-xbmc-send')

  cd kodi-build
  # install eventclients
  for _cmp in ${_components[@]}; do
    DESTDIR="$pkgdir" /usr/bin/cmake \
      -DCMAKE_INSTALL_COMPONENT="$_cmp" \
      -P cmake_install.cmake
  done

  # python2 is being used
  cd "$pkgdir"
  grep -lR '#!.*python' * | while read file; do sed -s 's/\(#!.*python\)/\12/g' -i "$file"; done
}

package_kodi-imx-tools-texturepacker() {
  pkgdesc="Kodi texturepacker tool (Freescale i.MX6)"
  depends=('libpng' 'giflib' 'libjpeg-turbo' 'lzo')

  _components=('kodi-tools-texturepacker')

  cd kodi-build
  # install eventclients
  for _cmp in ${_components[@]}; do
    DESTDIR="$pkgdir" /usr/bin/cmake \
      -DCMAKE_INSTALL_COMPONENT="$_cmp" \
      -P cmake_install.cmake
  done
}

package_kodi-imx-dev() {
  pkgdesc="Kodi dev files (Freescale i.MX6)"
  depends=('kodi')
  provides=('kodi-dev')

  _components=('kodi-addon-dev'
    'kodi-audio-dev'
    'kodi-eventclients-dev'
    'kodi-game-dev'
    'kodi-inputstream-dev'
    'kodi-peripheral-dev'
    'kodi-pvr-dev'
    'kodi-screensaver-dev'
    'kodi-visualization-dev')

  cd kodi-build
  # install eventclients
  for _cmp in ${_components[@]}; do
    DESTDIR="$pkgdir" /usr/bin/cmake \
      -DCMAKE_INSTALL_COMPONENT="$_cmp" \
      -P cmake_install.cmake
  done

  # python2 is being used
  cd "$pkgdir"
  grep -lR '#!.*python' * | while read file; do sed -s 's/\(#!.*python\)/\12/g' -i "$file"; done
}