# Maintainer: Kevin Mihelich <kevin@archlinuxarm.org>
# Maintainer: Dave ... <pepdog@archlinuxarm.org>

buildarch=4

pkgbase=linux-cubox-headless
pkgname=('linux-cubox-headless')
_kernelname=${pkgname#linux}
_basekernel=3.5
pkgver=${_basekernel}.7
pkgrel=11
cryptover=1.5
arch=('arm')
url="http://www.kernel.org/"
license=('GPL2')
makedepends=('xmlto' 'docbook-xsl' 'uboot-mkimage' 'git' 'python2')
options=('!strip')
source=('config'
        'change-default-console-loglevel.patch'
        "http://download.gna.org/cryptodev-linux/cryptodev-linux-${cryptover}.tar.gz"
        'Kirkwood_SPDIF.conf')
md5sums=('593f58fda559d692c87a7fd8b9ecf8f0'
         '9d3c56a4b999c8bfbd4018089a62f662'
         '3a4b8d23c1708283e29477931d63ffb8'
         'a746383e6ce46bcecb662acb3ac21b3f')

__gitroot="git://github.com/vDorst/linux.git"
__gitname="linux"

build() {
  cd "${srcdir}"
  msg "Connecting to GIT server...."

  if [ -d $__gitname ] ; then
    cd $__gitname && git pull origin
    msg "The local files are updated."
  else
    git clone --depth 1 $__gitroot
  fi

  msg "GIT checkout done or server timeout"

  cd "${srcdir}/${__gitname}"

  # set DEFAULT_CONSOLE_LOGLEVEL to 4 (same value as the 'quiet' kernel param)
  # remove this when a Kconfig knob is made available by upstream
  # (relevant patch sent upstream: https://lkml.org/lkml/2011/7/26/227)
  patch -Np1 -i "${srcdir}/change-default-console-loglevel.patch"

  cat "${srcdir}/config" > ./.config

  # set extraversion to pkgrel
  sed -ri "s|^(EXTRAVERSION =).*|\1 -${pkgrel}|" Makefile

  # don't run depmod on 'make install'. We'll do this ourselves in packaging
  sed -i '2iexit 0' scripts/depmod.sh

  # get kernel version
  make prepare

  # load configuration
  # Configure the kernel. Replace the line below with one of your choice.
  #make menuconfig # CLI menu for configuration
  #make nconfig # new CLI menu for configuration
  #make xconfig # X-based configuration
  #make oldconfig # using old config from previous kernel version
  # ... or manually edit .config

  # Copy back our configuration (use with new kernel version)
  #cp ./.config ../${pkgver}.config

  ####################
  # stop here
  # this is useful to configure the kernel
  #msg "Stopping build"
  #return 1
  ####################

  #yes "" | make config

  # build!
  make ${MAKEFLAGS} modules uImage

  # build cryptodev module
  cd "${srcdir}/cryptodev-linux-${cryptover}"
  make KERNEL_DIR="${srcdir}/${__gitname}"
}

package_linux-cubox-headless() {
  pkgdesc="The Linux Kernel and modules for the Cubox using maximum ram but has console fb still"
  depends=('coreutils' 'linux-firmware' 'module-init-tools>=3.16' 'mkinitcpio>=0.7')
  optdepends=('crda: to set the correct wireless channels of your country')
  provides=('kernel26-cubox' 'linux=${pkgver}' 'cryptodev_friendly')
  conflicts=('kernel26' 'linux')
  replaces=('kernel26')
  backup=("etc/mkinitcpio.d/${pkgname}.preset")
  install=${pkgname}.install
  cd "${srcdir}/${__gitname}"

  KARCH=arm

  # get kernel version
  _kernver="$(make kernelrelease)"

  mkdir -p "${pkgdir}"/{lib/modules,lib/firmware,boot}
  cd "${srcdir}/linux"
  make INSTALL_MOD_PATH="${pkgdir}" modules_install
  cp arch/$KARCH/boot/uImage "${pkgdir}/boot/uImage"

  # set correct depmod command for install
  sed \
    -e  "s/KERNEL_NAME=.*/KERNEL_NAME=${_kernelname}/g" \
    -e  "s/KERNEL_VERSION=.*/KERNEL_VERSION=${_kernver}/g" \
    -i "${startdir}/${pkgname}.install"

  # remove build and source links
  rm -f "${pkgdir}"/lib/modules/${_kernver}/{source,build}
  # remove the firmware
  rm -rf "${pkgdir}/lib/firmware"
  # gzip -9 all modules to save 100MB of space
  find "${pkgdir}" -name '*.ko' |xargs -P 2 -n 1 gzip -9
  # make room for external modules
  ln -s "../extramodules-${pkgver}-${_kernelname:-ARCH}" "${pkgdir}/lib/modules/${_kernver}/extramodules"
  # add real version for building modules and running depmod from post_install/upgrade
  mkdir -p "${pkgdir}/lib/modules/extramodules-${pkgver}-${_kernelname:-ARCH}"
  echo "${_kernver}" > "${pkgdir}/lib/modules/extramodules-${pkgver}-${_kernelname:-ARCH}/version"

  # install cryptodev module
  cd "${srcdir}/cryptodev-linux-${cryptover}"
  make -C "${srcdir}/${__gitname}" INSTALL_MOD_PATH="${pkgdir}" SUBDIRS=`pwd` modules_install

  cd "${srcdir}/${__gitname}"

  # Now we call depmod...
  depmod -b "$pkgdir" -F System.map "$_kernver"

  # move module tree /lib -> /usr/lib
  mkdir -p "${pkgdir}/usr"
  mv "$pkgdir/lib" "$pkgdir/usr"

  install -Dm644 "${srcdir}/Kirkwood_SPDIF.conf" \
    "${pkgdir}/usr/share/alsa/cards/Kirkwood_SPDIF.conf"
}