# Tanım: Kararli kernel sürümü
# URL: http://www.kernel.org
# Paketçi: milisarge
# Gerekler: linux-firmware intel-ucode elfutils
# Grup: sistem

isim=kernel
_basekernel=4.17
surum=4.17.14
devir=1

kaynak=(http://www.kernel.org/pub/linux/kernel/v4.x/linux-$_basekernel.tar.xz
        http://www.kernel.org/pub/linux/kernel/v4.x/patch-${surum}.xz
        config
        aufs4-loopback.patch
        aufs4-base.patch
        aufs4-mmap.patch
        aufs4-standalone.patch
        aufs4-kbuild.patch
        aufs4.patch
        config.aufs
        tmpfs-idr.patch
        vfs-ino.patch)


derle() {
  
  cd "${SRC}/linux-$_basekernel"
  cp "${SRC}/patch-$surum.xz" .
  unxz patch-$surum.xz
  # güncel kernel yama
  patch -p1 -i "patch-${surum}"
  
  # güvenli kernel yaması
  #patch -p1 -i "${SRC}/linux-hardened-${surum}.a.patch"
  
  #çalışan sistem aufs yamaları
  patch -p1 -i "${SRC}/aufs4.patch"
  patch -p1 -i "${SRC}/aufs4-base.patch"
  patch -p1 -i "${SRC}/aufs4-kbuild.patch"
  patch -p1 -i "${SRC}/aufs4-loopback.patch"
  patch -p1 -i "${SRC}/aufs4-mmap.patch"
  patch -p1 -i "${SRC}/aufs4-standalone.patch"
  patch -p1 -i "${SRC}/tmpfs-idr.patch"
  patch -p1 -i "${SRC}/vfs-ino.patch"
  
  cat "${SRC}/config" > ./.config

  cat "${SRC}/config.aufs" >> ./.config

  # kernel sürümüne devir sayısı eklemek için
  # set extraversion to release
  #sed -ri "s|^(EXTRAVERSION =).*|\1 -${devir}|" Makefile

  cd "${SRC}/linux-${_basekernel}"
  
  export KBUILD_BUILD_USER="milisarge"
  export KBUILD_BUILD_HOST="`uname -m`.milislinux.org"
  
  # ayarları onaylama
  yes "" | make oldconfig >/dev/null

  # derleme
  make ${MAKEFLAGS} bzImage modules || make ${MAKEFLAGS} bzImage modules

  KARCH=x86

  # get kernel version
  #_kernver="$(make kernelrelease)"
  #KERNEL_VERSION="${surum}-milis"
  _kernver="${surum}-milis"
  mkdir -p "${PKG}"/{lib/modules,lib/firmware,boot}
  make INSTALL_MOD_PATH="${PKG}" modules_install
  cp arch/$KARCH/boot/bzImage "${PKG}/boot/kernel-${surum}"

  # vmlinux un source a eklenmesi,modül derlerken bu geçici silinecek
  #install -D -m644 vmlinux "${PKG}/usr/src/linux-${_kernver}/vmlinux"

  # derleme kalıntılarının temizlenmesi
  rm -f "${PKG}"/lib/modules/${_kernver}/{source,build}
  # firmwarelerin temizlenmesi
  rm -rf "${PKG}/lib/firmware"
  # modüllerin sıkıştırılarak ek yer kazanılması
  find "${PKG}" -name '*.ko' -exec gzip -9 {} \;
  
  #linux-headers / linux modülleri

  install -dm755 "${PKG}/lib/modules/${_kernver}"

  cd "${PKG}/lib/modules/${_kernver}"
  ln -sf ../../../usr/src/linux-${_kernver} build

  cd "${SRC}/linux-${_basekernel}"
  install -D -m644 Makefile \
    "${PKG}/usr/src/linux-${_kernver}/Makefile"
  install -D -m644 kernel/Makefile \
    "${PKG}/usr/src/linux-${_kernver}/kernel/Makefile"
  install -D -m644 .config \
    "${PKG}/usr/src/linux-${_kernver}/.config"

  ######################
  mkdir -p "${PKG}/usr/src/linux-${_kernver}/include"

  for i in acpi asm-generic config crypto drm generated linux math-emu \
    media net pcmcia scsi rdma soc sound trace uapi video xen; do
    cp -a include/${i} "${PKG}/usr/src/linux-${_kernver}/include/"
  done

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

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

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

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

  cp arch/${KARCH}/Makefile "${PKG}/usr/src/linux-${_kernver}/arch/${KARCH}/"

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

  # add headers for lirc package
  # pci
  for i in bt8xx cx88 saa7134; do
    mkdir -p "${PKG}/usr/src/linux-${_kernver}/drivers/media/pci/${i}"
    cp -a drivers/media/pci/${i}/*.h "${PKG}/usr/src/linux-${_kernver}/drivers/media/pci/${i}"
  done
  # usb
  for i in cpia2 em28xx pwc; do
    mkdir -p "${PKG}/usr/src/linux-${_kernver}/drivers/media/usb/${i}"
    cp -a drivers/media/usb/${i}/*.h "${PKG}/usr/src/linux-${_kernver}/drivers/media/usb/${i}"
  done
  # i2c
  mkdir -p "${PKG}/usr/src/linux-${_kernver}/drivers/media/i2c"
  cp drivers/media/i2c/*.h  "${PKG}/usr/src/linux-${_kernver}/drivers/media/i2c/"
  for i in cx25840; do
    mkdir -p "${PKG}/usr/src/linux-${_kernver}/drivers/media/i2c/${i}"
    cp -a drivers/media/i2c/${i}/*.h "${PKG}/usr/src/linux-${_kernver}/drivers/media/i2c/${i}"
  done

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

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

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

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

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

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

  # add dvb headers
  mkdir -p "${PKG}/usr/src/linux-${_kernver}/drivers/media/usb/dvb-usb"
  cp drivers/media/usb/dvb-usb/*.h "${PKG}/usr/src/linux-${_kernver}/drivers/media/usb/dvb-usb/"
  mkdir -p "${PKG}/usr/src/linux-${_kernver}/drivers/media/dvb-frontends"
  cp drivers/media/dvb-frontends/*.h "${PKG}/usr/src/linux-${_kernver}/drivers/media/dvb-frontends/"
  mkdir -p "${PKG}/usr/src/linux-${_kernver}/drivers/media/tuners"
  cp drivers/media/tuners/*.h "${PKG}/usr/src/linux-${_kernver}/drivers/media/tuners/"

  # add xfs and shmem for aufs building
  mkdir -p "${PKG}/usr/src/linux-${_kernver}/fs/xfs/libxfs"
  mkdir -p "${PKG}/usr/src/linux-${_kernver}/mm"
  cp fs/xfs/libxfs/xfs_sb.h "${PKG}/usr/src/linux-${_kernver}/fs/xfs/libxfs/xfs_sb.h"

  #aufs4-util need
  sed -i "s:__user::g" "${PKG}/usr/src/linux-${_kernver}/include/uapi/linux/aufs_type.h"

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

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

  # strip scripts directory
  find "${PKG}/usr/src/linux-${_kernver}/scripts" -type f -perm -u+w 2>/dev/null | while read binary ; do
    case "$(file -bi "${binary}")" in
      *application/x-sharedlib*) # Libraries (.so)
        /usr/bin/strip ${STRIP_SHARED} "${binary}";;
      *application/x-archive*) # Libraries (.a)
        /usr/bin/strip ${STRIP_STATIC} "${binary}";;
      *application/x-executable*) # Binaries
        /usr/bin/strip ${STRIP_BINARIES} "${binary}";;
    esac
  done

  # add objtool, nvidia needed
  install -Dt "${PKG}/usr/src/linux-${_kernver}/tools/objtool" tools/objtool/objtool

  # remove unneeded architectures
  rm -rf "${PKG}"/usr/src/linux-${_kernver}/arch/{alpha,arm,arm26,arm64,avr32,blackfin,c6x,cris,frv,h8300,hexagon,ia64,m32r,m68k,m68knommu,mips,microblaze,mn10300,openrisc,parisc,powerpc,ppc,s390,score,sh,sh64,sparc,sparc64,tile,unicore32,um,v850,xtensa}
}
