# Maintainer (Arch): Sven-Hendrik Haase <sh@lutzhaase.com>
# Contributor (Arch): M0Rf30
# Contributor (Arch): Samsagax <samsagax@gmail.com>
# Maintainer: André Silva <emulatorman@parabola.nu>
# Contributor: Luke Shumaker <lukeshu@sbcglobal.net>
# Contributor: Márcio Silva <coadde@parabola.nu>

_kernelname=-lts

if [[ ${_kernelname} == "" ]]; then
  _basekernel=4.8
  _archrel=54
  _parabolarel=1
elif [[ ${_kernelname} == -lts ]]; then
  _basekernel=4.4
  _archrel=24
  _parabolarel=1
elif [[ ${_kernelname} == -grsec ]]; then
  _basekernel=4.8
  _archrel=1
  _parabolarel=1
elif [[ ${_kernelname} == -pck ]]; then
  _basekernel=4.8
  _archrel=1
  _parabolarel=1
elif [[ ${_kernelname} == -knock ]]; then
  _basekernel=4.8
  _archrel=1
  _parabolarel=1
elif [[ ${_kernelname} == -lts-knock ]]; then
  _basekernel=4.4
  _archrel=1
  _parabolarel=1
elif [[ ${_kernelname} == -grsec-knock ]]; then
  _basekernel=4.8
  _archrel=1
  _parabolarel=1
elif [[ ${_kernelname} == -grsec-xen ]]; then
  _basekernel=4.8
  _archrel=1
  _parabolarel=1
elif [[ ${_kernelname} == -rt ]]; then
  _basekernel=4.6
  _archrel=1
  _parabolarel=1
elif [[ ${_kernelname} == -pae ]]; then
  _basekernel=4.8
  _archrel=1
  _parabolarel=1
elif [[ ${_kernelname} == -xen ]]; then
  _basekernel=4.8
  _archrel=1
  _parabolarel=1
fi

pkgbase=bbswitch
pkgver=0.8
pkgrel=${_archrel}.parabola${_parabolarel}.basekernel${_basekernel}
_extramodules=extramodules-${_basekernel}${_kernelname}
pkgdesc="Kernel module allowing to switch dedicated graphics card on Optimus laptops (built for the linux-libre${_kernelname} kernel package)"
arch=('i686' 'x86_64')
url="http://github.com/Bumblebee-Project/${pkgbase}"
license=('GPL')

# Generic (you shouldn't have to modify any of these variables)
_toohigh=$(IFS=. read a b <<<$_basekernel; echo $a.$((b+1)))
makedepends=("linux-libre${_kernelname}-headers>=${_basekernel}" "linux-libre${_kernelname}-headers<${_toohigh}" "linux-libre${_kernelname}>=${_basekernel}" "linux-libre${_kernelname}<${_toohigh}")
makedepends+=('libelf')

if [[ ${_kernelname} != "" ]]; then
  provides=("${pkgbase}")
fi

source=("${pkgbase}-${pkgver}.tar.gz::https://github.com/Bumblebee-Project/bbswitch/archive/v${pkgver}.tar.gz")
md5sums=('5b116b31ace3604ddf9d1fc1f4bc5807')

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

  _kernver="$(cat /usr/lib/modules/${_extramodules}/version)"

  make KDIR=/lib/modules/${_kernver}/build
}

_package() {
  depends=("linux-libre${_kernelname}>=${_basekernel}" "linux-libre${_kernelname}<${_toohigh}")
  replaces=("${pkgbase}-libre${_kernelname}" "${pkgbase}-parabola${_kernelname}")
  conflicts=("${pkgbase}-libre${_kernelname}" "${pkgbase}-parabola${_kernelname}")
  install='kmod.install'

  cd "${srcdir}/${pkgbase}-${pkgver}"

  # Set the correct extramodules directory for install
  cp -f "${startdir}/${install}" "${startdir}/${install}.pkg"
  true && install=${install}.pkg
  sed -i "s/^_EXTRAMODULES=.*/_EXTRAMODULES="${_extramodules}"/" "${startdir}/${install}"

  # Actually install
  install -Dm644 ${pkgbase}.ko "${pkgdir}"/usr/lib/modules/${_extramodules}/${pkgbase}.ko
  gzip "${pkgdir}/usr/lib/modules/${_extramodules}/${pkgbase}.ko"
}

_package-dkms() {
  depends=('dkms')

  cd "${srcdir}/${pkgbase}-${pkgver}"

  install -dm755 "${pkgdir}/usr/src/${pkgbase}-${pkgver}/"

  install -Dm644 Makefile bbswitch.c dkms/dkms.conf "${pkgdir}/usr/src/${pkgbase}-${pkgver}/"
}

pkgname=("${pkgbase}${_kernelname}" "${pkgbase}${_kernelname}-dkms")
for _p in ${pkgname[@]}; do
  eval "package_${_p}() {
    $(declare -f "_package${_p#${pkgbase}${_kernelname}}")
    _package${_p#${pkgbase}${_kernelname}}
  }"
done