# $Id: PKGBUILD 35833 2010-12-23 10:25:34Z spupykin $
# 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: BlackEagle < ike DOT devolder AT herecura DOT be >

# python dependency or not
_ext_python="disable"
_prefix=/usr

pkgname=xbmc
pkgver=10.0
pkgrel=5
pkgdesc="XBMC Media Center"
arch=('i686' 'x86_64')
url="http://xbmc.org"
license=('GPL' 'custom')
depends=( 'bzip2' 'curl' 'enca' 'faac' 'faad2' 'fontconfig' 'fribidi'
          'glew' 'jasper' 'libcdio' 'libgl' 'libmad' 'libmms'
	  'libmpeg2' 'libmysqlclient' 'libsamplerate' 'libxinerama'
	  'libxrandr' 'libxtst' 'lzo2' 'sdl_image' 'sdl_mixer'
	  'smbclient' 'wavpack' 'mesa-demos' 'xorg-utils' 'libmicrohttpd'
	  'libssh' 'libva' 'libmodplug' 'rtmpdump')
if [ "$_ext_python" != "disable" ]; then
  depends=( '${depends[@]}' 'python2' )
fi
makedepends=('boost' 'cmake' 'gperf' 'nasm' 'libvdpau' 'unzip' 'zip')
optdepends=('lirc: remote controller support'
	    'unrar: access compressed files without unpacking them'
	    'devicekit-power: used to trigger suspend functionality'
	    'udisks: automount external drives'
	    'upower: used to trigger suspend functionality'
	    'libvdpau: for nvidia hardware acceleration')
install=("${pkgname}.install")
options=(makeflags)
source=("http://mirrors.xbmc.org/releases/source/xbmc-$pkgver.tar.gz"
	"FEH.sh")
md5sums=('728fb514e5f43f27bb880305061b4e72'
         'c3e2ab79b9965f1a4a048275d5f222c4')

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

    pacman -Q libass && { msg "Remove libass"; return 1; }

    # remove "--as-needed" from LDFLAGS
    unset LDFLAGS; LDFLAGS="-Wl,--hash-style=gnu"

    msg "Bootstrapping XBMC"
    ./bootstrap

    msg "Configuring XBMC"
    ./configure --prefix=/usr \
	--enable-vdpau \
	--disable-pulse \
	--disable-hal \
	--disable-avahi \
	--disable-external-liba52 \
	--disable-external-libdts \
	--enable-external-libmpeg2 \
	--enable-external-libogg \
	--enable-external-libwavpack \
	--disable-external-libass \
	--disable-external-ffmpeg \
	--enable-rtmp \
	--${_ext_python}-external-python \
	--disable-debug

    # Now (finally) build
    msg "Running make"
    make ${MAKEFLAGS}
    make -C lib/addons/script.module.pil
    make -C lib/addons/script.module.pysqlite
}

package() {
    cd "${srcdir}/xbmc-$pkgver"
    msg "Running make install"
    make prefix="${pkgdir}${_prefix}" install

    # Replace FEH.py with FEH.sh (and thus remove external python dependency)
    install -D -m 0755 "${srcdir}/FEH.sh" "${pkgdir}${_prefix}/share/xbmc/FEH.sh"
    sed -i -e 's/^python \(.*\)FEH.py \(.*\)$/\1FEH.sh \2/' "${pkgdir}${_prefix}/bin/xbmc"

    # lsb_release fix
    sed -i -e 's/which lsb_release &> \/dev\/null/\[ -f \/etc\/arch-release ]/g' "${pkgdir}${_prefix}/bin/xbmc"
    sed -i -e "s/lsb_release -a 2> \/dev\/null | sed -e 's\/\^\/    \/'/cat \/etc\/arch-release/g" "${pkgdir}${_prefix}/bin/xbmc"

    # .desktop files
    install -D -m 0644 "${srcdir}/xbmc-$pkgver/tools/Linux/xbmc.desktop" "${pkgdir}${_prefix}/share/applications/xbmc.desktop"
    install -D -m 0644 "${srcdir}/xbmc-$pkgver/tools/Linux/xbmc-48x48.png" "${pkgdir}${_prefix}/share/pixmaps/xbmc.png"

    # Tools
    install -D -m 0755 "${srcdir}/xbmc-$pkgver/xbmc-xrandr" "${pkgdir}${_prefix}/share/xbmc/xbmc-xrandr"
    install -D -m 0755 "${srcdir}/xbmc-$pkgver/tools/TexturePacker/TexturePacker" "${pkgdir}${_prefix}/share/xbmc/"

    # Licenses
    install -d -m 0755 "${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

    # Docs
    install -d -m 0755 "${pkgdir}${_prefix}/share/doc/${pkgname}"

    # cleanup some stuff
    msg "Cleanup unneeded files"
    rm -rf "${pkgdir}/usr/share/xsessions"
    rm -f "${pkgdir}/usr/share/xbmc/FEH.py"
    rm -f "${pkgdir}/usr/share/icons/hicolor/icon-theme.cache"

    # strip
    msg "Stripping binaries"
    find "$pkgdir" -type f -exec strip {} \; >/dev/null 2>/dev/null
}