#!/bin/bash

kernel_screen()  {

  need_dependency disk &&
  need_dependency install_start &&

  debug_log "kernel" 3 "Entering kernel selection screen"

  # Sanity check the system date,
  # gnupg fails when the kernel signing key will be created in the future
  touch -d "now + 1 week" $TMP_DIR/timetest
  if [[ $TMP_DIR/timetest -ot /bin/bash ]] ;then
    confirm "Your system date appears to be set wrong.
This might cause the following signature check
on the linux kernel sources to fail.

You can fix this by switching to another terminal
and entering something like
date -d \"2005-12-24 20:30:00\".
If you do this, answer \"no\" to this query.

Alternatively, I can just set the system time to
the time this ISO was created. Should I do this now?" --defaultno &&
    date -s "$(date -r /bin/bash)"
  fi

  if run_dialog --yes-label "copy" --no-label "compile" \
                --yesno                                 \
"Would you like to configure and compile a custom kernel,
or install/copy the precompiled kernel from the ISO?
The precompiled kernel is highly modular to support almost
any hardware we might encounter, the custom kernel needs
some work to do all the config, but will be smaller.
Special hardware may also require a custom kernel." 0 0 ;then
    kn_copy_kernel
  else
    kn_compile_kernel
  fi || {
    debug_log "ERROR" -1 "Failed to install the kernel"
    return 1
  }

}

kn_copy_kernel() {

  echo "Waiting for the tools needed for kernel installation to install..."
  debug_log "kernel" 2 "Starting kernel copy"
  si_wait Iso.Compile_env gnupg

  # install the config used for this kernel
  cp -f /boot/config-$KERNEL_VERSION ${TARGET}/etc/sorcery/local/kernel.config

  cp --parents /etc/sorcery/local/depends/linux.p $TARGET

  touch $TARGET/etc/sorcery/local/makefile.patches

  echo "Setting up kernel source tree... (needed by some compiles)"

  debug_log "kernel" 3 "Starting cast of kernel"
  export PROMPT_DELAY=0
  export INTERNAL_ISO_VAR="yes"
  chroot_cmd cast -c linux
  local rc=$? # We want the following statements to execute on fail and success

  unset PROMPT_DELAY
  unset INTERNAL_ISO_VAR
  rm -f $TARGET/etc/sorcery/local/depends/linux.p \
    ${TARGET}/etc/sorcery/local/kernel.config \
    ${TARGET}/usr/src/linux*/.spell-config.p
  # We don't want the linux spell to pick up our borked config

  (( rc )) && return $rc # If linux cast failed, fail out now

  sed -i '/^linux:/d' $TARGET/var/state/sorcery/packages &&
  # we don't want the spellinstaller to think it is already installed

  debug_log "kernel" 3 "Starting si install of kernel" &&
  si_queue_now linux       &&
  # install the meat after the linux spell has done its thing

  fill_dependency linux copy &&
  debug_log "kernel" 1 "copied pre-made kernel"
}

kn_compile_kernel() {

  run_dialog --cr-wrap --textbox  ${DATA_DIR}/kernel.help 0 0

  echo "Waiting for the tools needed for kernel installation to install..."
  debug_log "kernel" 2 "Starting kernel compile"
  si_wait Iso.Compile_env gnupg

  cp --parents /etc/sorcery/local/depends/linux.p $TARGET

  if run_dialog --yes-label "ISO" --no-label "default" --yesno \
    "Do you want to start with the ISO's kernel config?
It does support a lot of hardware with modules, on the other
hand it will be large and take a long time to build.
Alternatively, I can just use the linux defaults." 0 0 \
     ;then
    # install the config used for this kernel
    debug_log "kernel" 1 "using ISO config"
    cp -f /boot/config-$KERNEL_VERSION ${TARGET}/etc/sorcery/local/kernel.config
  else
    # make linux spell use the linux default config
    debug_log "kernel" 1 "using default config"
    ln -s /usr/src/linux/arch/i386/defconfig \
      ${TARGET}/etc/sorcery/local/kernel.config
    # The target of this link does not exist yet, but will at the time the
    # link is resolved by the linux spell
  fi

  try_run sed -i -e 's/^MAKEMODE=.*/MAKEMODE="menuconfig"/'  \
                 -e 's/^CONFIG_KERNEL=.*/CONFIG_KERNEL="y"/' \
    $TARGET/etc/sorcery/local/depends/linux.p

  touch $TARGET/etc/sorcery/local/makefile.patches

  debug_log "kernel" 3 "casting kernel..."
  chroot_cmd cast -c linux
  local rc=$?

  rm -f $TARGET/etc/sorcery/local/depends/linux.p \
    ${TARGET}/etc/sorcery/local/kernel.config \
    ${TARGET}/usr/src/linux*/.spell-config.p
  # We don't want the linux spell to pick up our borked config

  (( rc )) && return $rc

  fill_dependency linux compile &&
  debug_log "kernel" 1 "compiled custom kernel"
}
