# $Id$
# Maintainer: Bartłomiej Piotrowski <nospam@bpiotrowski.pl>
# Maintainer (Parabola): Márcio Silva <coadde@adinet.com.uy>
# Maintainer (Parabola): André Silva <emulatorman@lavabit.com>
# Maintainer (Parabola): Jorge López <jorginho@adinet.com.uy>

pkgbase=virtualbox-modules-lts
pkgname=('virtualbox-modules-lts' 'virtualbox-parabola-modules-lts')
pkgver=4.1.18
pkgrel=1
arch=('i686' 'x86_64')
url='http://virtualbox.org'
license=('GPL')
makedepends=('libstdc++5' 'bin86' 'dev86' 'iasl' 'libxslt' 'libxml2' 'libpng' 'libidl2' 'xalan-c' 'sdl' 'linux-libre-lts' 'linux-libre-lts-headers')
[[ $CARCH == "x86_64" ]] && makedepends=("${makedepends[@]}" 'gcc-multilib' 'lib32-glibc')
source=(http://download.virtualbox.org/virtualbox/${pkgver}/VirtualBox-${pkgver}.tar.bz2
         LocalConfig.kmk 60-vboxguest.rules)
md5sums=('38db0a87cba659b484af868b0c2bd3ac'
         '4c88bd122677a35f68abd76eb01b378b'
         'ed1341881437455d9735875ddf455fbe')

_extramodules=extramodules-3.0-LIBRE-LTS
_kernver=$(pacman -Ss linux-libre-lts | awk 'NR==1{print $2}')-LIBRE-LTS

export KERN_DIR=/lib/modules/${_kernver}/build
export KERN_INCL=/usr/src/linux-${_kernver}/include/

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

    cp "$srcdir/LocalConfig.kmk" .

    ./configure \
        --with-linux=/usr/src/linux-${_kernver} \
        --disable-java \
        --disable-docs \
        --disable-xpcom \
        --disable-python \
        --disable-sdl-ttf \
        --disable-alsa \
        --disable-pulse \
        --disable-dbus  \
        --disable-opengl \
        --build-headless \
        --nofatal
    source ./env.sh
    kmk all

    make -C "$srcdir/VirtualBox-${pkgver}/out/linux.$BUILD_PLATFORM_ARCH/release/bin/src"
    make -C "$srcdir/VirtualBox-${pkgver}/out/linux.$BUILD_PLATFORM_ARCH/release/bin/additions/src"
}

package_virtualbox-parabola-modules-lts(){
    pkgdesc="Additions only for Parabola guests (long-term supported kernel modules)"
    license=('GPL')
    install=virtualbox-parabola-modules-lts.install
    depends=('linux-libre-lts>=3.0' 'linux-libre-lts<3.1')
    replaces=('virtualbox-archlinux-modules-lts')
    conflicts=('virtualbox-archlinux-modules-lts')
    provides=("virtualbox-parabola-modules=$pkgver" "virtualbox-archlinux-modules=$pkgver" "virtualbox-archlinux-modules-lts=$pkgver")

    source "$srcdir/VirtualBox-${pkgver}/env.sh"

    cd "$srcdir/VirtualBox-${pkgver}/out/linux.$BUILD_PLATFORM_ARCH/release/bin/additions/src"

    for module in vboxguest.ko vboxsf.ko vboxvideo.ko; do
        install -D -m644 ${module} \
            "$pkgdir/lib/modules/${_extramodules}/${module}"
    done

    install -D -m 0644 "$srcdir/60-vboxguest.rules" \
        "$pkgdir/usr/lib/udev/rules.d/60-vboxguest.rules"

    find "${pkgdir}" -name '*.ko' -exec gzip -9 {} \;
}

package_virtualbox-modules-lts(){
    pkgdesc="Long-term supported kernel modules for VirtualBox"
    license=('GPL')
    install=virtualbox-modules-lts.install
    depends=('linux-libre-lts>=3.0' 'linux-libre-lts<3.1')
    provides=("virtualbox-modules=$pkgver")

    source "$srcdir/VirtualBox-${pkgver}/env.sh"

    cd "$srcdir/VirtualBox-${pkgver}/out/linux.$BUILD_PLATFORM_ARCH/release/bin/src"

    install -D -m644 vboxdrv.ko \
        "$pkgdir/lib/modules/${_extramodules}/vboxdrv.ko"

    install -D -m644 vboxnetadp.ko \
        "$pkgdir/lib/modules/${_extramodules}/vboxnetadp.ko"

    install -D -m644 vboxnetflt.ko \
        "$pkgdir/lib/modules/${_extramodules}/vboxnetflt.ko"

    install -D -m644 vboxpci.ko \
        "$pkgdir/lib/modules/${_extramodules}/vboxpci.ko"

    find "${pkgdir}" -name '*.ko' -exec gzip -9 {} \;
}