# $Id: PKGBUILD 73064 2012-06-30 06:07:26Z jconder $
# Maintainer: Jonathan Conder <jonno.conder@gmail.com>
# Contributor: Giovanni Scafora <giovanni@archlinux.org>
# Contributor: Juergen Hoetzel <juergen@archlinux.org>
# Contributor: <kleptophobiac@gmail.com>
# Contributor: dorphell <dorphell@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - correctly define ARM cpus for configure
#  - disabled vdpau, mmx, incompatible ARM FPUs
#  - patch to fix v5 build

pkgname=mythtv
pkgver=0.28
pkgrel=9
epoch=1
pkgdesc="A Homebrew PVR project"
arch=('i686' 'x86_64')
url="http://www.mythtv.org/"
license=('GPL')
depends=('avahi' 'fftw' 'lame' 'libass' 'libavc1394' 'libcdio' 'libiec61883'
         'libgl' 'libpulse' 'libva' 'libvpx' 'libxinerama' 'lirc' 'mariadb-clients'
         'mysql-python' 'perl-dbd-mysql' 'perl-io-socket-inet6' 'perl-libwww'
         'perl-net-upnp' 'python2-lxml' 'qt5-webkit' 'qt5-script' 'taglib' 'urlgrabber'
         'libx264' 'exiv2' 'libxrandr' 'jack')
makedepends=('glew' 'libcec' 'libxml2' 'mesa' 'mesa-libgl' 'openssl' 'yasm' 'x264' 'gdb')
optdepends=('glew: for GPU commercial flagging'
            'libcec: for consumer electronics control capabilities'
            'libxml2: to read blu-ray metadata'
            'openssl: for AirTunes (RAOP) support')
conflicts=('myththemes' 'mythplugins-mythvideo')
replaces=('myththemes' 'mythplugins-mythvideo')
install='mythtv.install'
source=("$pkgname-$pkgver.tar.gz::https://github.com/MythTV/$pkgname/archive/v$pkgver.tar.gz"
        mythtv-fix-segfault.patch::"https://github.com/MythTV/mythtv/commit/c3a79298.patch"
        mythtv-gcc6.patch::"https://github.com/MythTV/mythtv/commit/e4f6e011.patch"
        'libcec4.patch'
        'mythbackend.service' '99-mythbackend.rules'
        '0001-libudfread-set-HAVE_PTHREAD_H.patch')
sha256sums=('7b3476c0ec0fc17d6b734f0440383815e81850a70b78c12ee40d61a408eba340'
            '60696991fcaa24fb61392401bd27cfa35a15b54f4bcc01aee54dc84440c6df99'
            'd35c4738f331ffff7d26d4cb98965689bdc72bd5b9776c1b31e5d97d06be724e'
            'd54b455e3216f9975a648eb0692b4020a87e46441e9e0af6a082b5de93ec3cc1'
            'ed5ca54de26b7cd8a64e09626eed6e09f35d677daf88c530bb24cc4252bcce6d'
            'ecfd02bbbef5de9773f4de2c52e9b2b382ce8137735f249d7900270d304fd333'
            'c15efbcc7d8db4a185ef908861f48633353173012b18dada44df4c200fea8072')

prepare() {
  cd $pkgname-$pkgver/$pkgname

  find 'bindings/python' 'contrib' 'programs/scripts' -type f | xargs sed -i 's@^#!.*python$@#!/usr/bin/python2@'

# Fix segfault with GCC 6 https://github.com/MythTV/mythtv/pull/122
  cd ..
  patch -p1 -i "$srcdir"/mythtv-fix-segfault.patch
# Fix build with GCC 6
  patch -p1 -i "$srcdir"/mythtv-gcc6.patch
# fix to compile with libcec4
  patch -p1 -i "$srcdir/libcec4.patch"

  patch -p1 -i "${srcdir}/0001-libudfread-set-HAVE_PTHREAD_H.patch"
}

build() {
  cd $pkgname-$pkgver/$pkgname

  [[ $CARCH == "arm" ]] && ARCH="armv5te" && CONFIG="--disable-armv6 --disable-armv6t2 --disable-vfp"
  [[ $CARCH == "armv6h" ]] && ARCH="armv6l" && CONFIG="--disable-vfp"
  [[ $CARCH == "armv7h" ]] && ARCH="armv7-a"
  [[ $CARCH == "aarch64" ]] && CONFIG+=" --enable-vaapi" || CONFIG+=" --disable-vaapi"
  ./configure --prefix=/usr \
              --cpu="$ARCH" \
              --disable-mmx \
              --disable-vdpau \
              --disable-altivec \
              --disable-audio-jack \
              --disable-ccache \
              --enable-libfftw3 \
              --enable-libmp3lame \
              --enable-libvpx \
              --enable-libx264 \
              --python=python2 \
              --perl-config-opts=INSTALLDIRS=vendor \
              --disable-neon $CONFIG
  make
}

package() {
  cd $pkgname-$pkgver/$pkgname
  make INSTALL_ROOT="$pkgdir" install

  install -D -m644 "$srcdir/mythbackend.service" "$pkgdir/usr/lib/systemd/system/mythbackend.service"
  install -D -m644 'database/mc.sql' "$pkgdir/usr/share/mythtv/mc.sql"

  mkdir -p "$pkgdir/usr/share/mythtv"
  cp -R 'contrib' "$pkgdir/usr/share/mythtv"
  mkdir -p "$pkgdir/var/log/mythtv"

# Install udev rules https://www.mythtv.org/wiki/Systemd_mythbackend_Configuration#Delay_starting_the_backend_until_tuners_have_initialized
  install -Dm644 "$srcdir"/99-mythbackend.rules "$pkgdir"/usr/lib/udev/rules.d/99-mythbackend.rules
}