# Contributor: Kiril Zyapkov <kiril.zyapkov@gmail.com>

# iMX233-OLinuXino-Maxi kernel for archlinuxarm
# basically replicates the oe-recipe
# see https://github.com/OSSystems/meta-fsl-arm-extra.git
# vim:set ts=2 sw=2 et:

buildarch=2

pkgbase="kernel26-olinuxino"
pkgname=('kernel26-olinuxino' 'kernel26-headers-olinuxino')
_kernelname=${pkgname#kernel26}
_basekernel=2.6
pkgver=${_basekernel}.35.3
pkgrel=8
makedepends=('xmlto' 'docbook-xsl' 'make' 'git' 'elftosb' 'imx-bootlets-olinuxino' 'imx-bootlets-olinuxino-ntsc')
optdepends=()
options=('!strip' '!buildflags' '!makeflags')
arch=('arm')
CARCH=arm
KARCH=arm
LDFLAGS=""
license=('GPL2')
url="https://github.com/Freescale/meta-fsl-arm/"
source=(config
        NFS-Fix-nfsroot-support.patch
        NFS-allow-nfs-root-mount-to-use-alternate-rpc-ports.patch
        mx28-removecpufreq.patch
        mxs-duart-use-ttyAMA-for-device-name.patch
        imx233-olinuxino.patch)
md5sums=('953ddea92634bb713853f2c1b14ed130'
         'e086ab0b162d292549f9bf453e07ad2e'
         'ff1281a87da6f8093992ad70d23e71d2'
         'c2d9dba73587fd00d990a2521428d67b'
         '302ad0fe4e990c789793e25ced58f712'
         'ba72c5e3ea407e6214e237323d6b467b')

__gitname="linux-2.6-imx"
__gitroot="git://git.freescale.com/imx/${__gitname}.git"
__gitbranch="imx_2.6.35_10.12.01"
__gitcommit="38bf41cc376e24c384934fbeb10770d6712e9143"


build() {
  cd "$srcdir"
  if [ -d $__gitname ] ; then
    msg "Using existing GIT repo clone."
    cd ${__gitname} && git checkout ${__gitcommit} -- .
    msg "The local files are updated."
  else
    msg "GIT Checkout of ${__gitroot}"
    git clone --depth=1 --branch=${__gitbranch} ${__gitroot} ${__gitname}
    cd ${__gitname}
    git checkout ${__gitcommit} -- .
  fi

  msg "Cleaning up the working copy..."
  git clean -df
  make mrproper

  msg "Patching ..."

  patch -Np1 -i ../NFS-Fix-nfsroot-support.patch
  patch -Np1 -i ../NFS-allow-nfs-root-mount-to-use-alternate-rpc-ports.patch
  patch -Np1 -i ../mx28-removecpufreq.patch
  patch -Np1 -i ../mxs-duart-use-ttyAMA-for-device-name.patch
  patch -Np1 -i ../imx233-olinuxino.patch
  
  sed -i 's|/sbin/depmod|/bin/true|' Makefile
  
  msg "Patching done, building the kernel."

  cp ${srcdir}/config .config
  make prepare

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

  # 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 oldconfig # using old config from previous kernel version

  # Build!

  make ${MAKEFLAGS} zImage modules
}

package_kernel26-olinuxino() {
  pkgdesc="The Linux Kernel and modules for iMX233-OLinuXino-Maxi with PAL video output"
  groups=('base')
  depends=('coreutils' 'module-init-tools')
  replaces=('kernel24' 'kernel24-scsi' 'kernel26-scsi'
            'alsa-driver' 'ieee80211' 'hostap-driver26'
            'pwc' 'nforce' 'squashfs' 'unionfs' 'ivtv'
            'zd1211' 'kvm-modules' 'iwlwifi' 'rt2x00-cvs'
            'gspcav1' 'atl2' 'wlan-ng26' 'rt2500' 'nouveau-drm')
  provides=("kernel26=${pkgver}" "linux=${pkgver}")
  install=kernel26.install
  KARCH=arm

  cd ${srcdir}/${__gitname}
  
  # get kernel version
  _kernver="$(make kernelrelease)"

  # Install zImage, System.map and the config for reference
  mkdir -p ${pkgdir}/{lib/modules,lib/firmware,boot}
  make INSTALL_MOD_PATH="${pkgdir}" modules_install
  #cp arch/$KARCH/boot/uImage ${pkgdir}/boot/uImage
  cp arch/$KARCH/boot/zImage ${pkgdir}/boot/zImage
  ln -sf "${pkgdir}/boot/zImage" .
  cp .config ${pkgdir}/boot/config-${_kernver}
  cp System.map System.map-${_kernver}

  # 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/kernel26.install

  # Generate an imx233 boot image
  msg "Generating PAL sdcard image ..."
  ln -sf /opt/imx-bootlets-olinuxino
  elftosb -z -c ./imx-bootlets-olinuxino/linux.bd -o ${pkgdir}/boot/oli-${_kernver}-pal.sb

  msg "Generating NTSC sdcard image ..."
  ln -sf /opt/imx-bootlets-olinuxino-ntsc
  elftosb -z -c ./imx-bootlets-olinuxino-ntsc/linux.bd -o ${pkgdir}/boot/oli-${_kernver}-ntsc.sb

  # remove build and source links
  rm -f ${pkgdir}/lib/modules/${_kernver}/{source,build}

  # 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"
}

package_kernel26-headers-olinuxino() {
  pkgdesc="Header files and scripts for building modules for kernel26 for iMX233-OLinuXino-Maxi"
  provides=("kernel26-headers=${pkgver}" "linux-headers=${pkgver}")
  KARCH=arm

  cd ${srcdir}/${__gitname}

  # get kernel version
  _kernver="$(make kernelrelease)"
  
  install -dm755 "${pkgdir}/usr/lib/modules/${_kernver}"

  cd "${pkgdir}/usr/lib/modules/${_kernver}"
  ln -sf ../../../src/linux-${_kernver} build
  
  cd ${srcdir}/${__gitname}
  install -D -m644 Makefile \
    ${pkgdir}/usr/src/linux-${_kernver}/Makefile
  install -D -m644 kernel/Makefile \
    ${pkgdir}/usr/src/linux-${_kernver}/kernel/Makefile
  install -D -m644 .config \
    ${pkgdir}/usr/src/linux-${_kernver}/.config
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include

  for i in acpi asm-generic config generated linux math-emu media net pcmcia scsi sound trace video; do
    cp -a include/$i ${pkgdir}/usr/src/linux-${_kernver}/include/
  done

  # copy arch includes for external modules
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH
  cp -a arch/$KARCH/include ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/

  # install platform- and machine-specific headers
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/mach-mx23
  cp -a arch/$KARCH/mach-mx23/include ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/mach-mx23/
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/plat-mxs
  cp -a arch/$KARCH/plat-mxs/include ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/plat-mxs/

  # copy files necessary for later builds, like nvidia and vmware
  cp Module.symvers ${pkgdir}/usr/src/linux-${_kernver}
  cp -a scripts ${pkgdir}/usr/src/linux-${_kernver}

  # fix permissions on scripts dir
  chmod og-w -R ${pkgdir}/usr/src/linux-${_kernver}/scripts
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/.tmp_versions

  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel

  cp arch/$KARCH/Makefile ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/
  cp arch/$KARCH/kernel/asm-offsets.s ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel/

  # add headers for lirc package
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video
  cp drivers/media/video/*.h  ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/
  for i in bt8xx cpia2 cx25840 cx88 em28xx et61x251 pwc saa7134 sn9c102 usbvideo; do
   mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/$i
   cp -a drivers/media/video/$i/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/$i
  done

  # add docbook makefile
  install -D -m644 Documentation/DocBook/Makefile \
    ${pkgdir}/usr/src/linux-${_kernver}/Documentation/DocBook/Makefile

  # add dm headers
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/md
  cp drivers/md/*.h  ${pkgdir}/usr/src/linux-${_kernver}/drivers/md

  # add inotify.h
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/linux
  cp include/linux/inotify.h ${pkgdir}/usr/src/linux-${_kernver}/include/linux/

  # add wireless headers
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/
  cp net/mac80211/*.h ${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/

  # add dvb headers for external modules
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core
  cp drivers/media/dvb/dvb-core/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core/

  # add dvb headers for external modules
  #mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/
  #cp include/config/dvb/*.h ${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/

  # add dvb headers for http://mcentral.de/hg/~mrec/em28xx-new
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
  cp drivers/media/dvb/frontends/lgdt330x.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
  cp drivers/media/video/msp3400-driver.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/

  # add dvb headers  
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-usb
  cp drivers/media/dvb/dvb-usb/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-usb/
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends
  cp drivers/media/dvb/frontends/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/common/tuners
  cp drivers/media/common/tuners/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/common/tuners/
  
  # add xfs and shmem for aufs building
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs
  mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/mm
  cp fs/xfs/xfs_sb.h ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs/xfs_sb.h

  # add headers vor virtualbox
  cp -a include/drm $pkgdir/usr/src/linux-${_kernver}/include/

  # add headers for broadcom wl
  cp -a include/trace $pkgdir/usr/src/linux-${_kernver}/include/

  # copy in Kconfig files
  for i in `find . -name "Kconfig*"`; do 
    mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/`echo $i | sed 's|/Kconfig.*||'`
    cp $i ${pkgdir}/usr/src/linux-${_kernver}/$i
  done

  chown -R root.root ${pkgdir}/usr/src/linux-${_kernver}
  find ${pkgdir}/usr/src/linux-${_kernver} -type d -exec chmod 755 {} \;

  # remove unneeded architectures
  rm -rf ${pkgdir}/usr/src/linux-${_kernver}/arch/{alpha,avr32,blackfin,cris,frv,h8300,ia64,m32r,m68k,m68knommu,mips,microblaze,mn10300,parisc,powerpc,ppc,s390,sh,sh64,sparc,sparc64,um,v850,xtensa,x86}
}
md5sums=('477fe18ac96828b5b7238cc1ee1547e8'
         'e086ab0b162d292549f9bf453e07ad2e'
         'ff1281a87da6f8093992ad70d23e71d2'
         'c2d9dba73587fd00d990a2521428d67b'
         '302ad0fe4e990c789793e25ced58f712'
         'ba72c5e3ea407e6214e237323d6b467b')