unset LC_ALL

linux_new_uncompress()
{
  if [[ $(file $1 | grep gzip) != "" ]]
  then
    gunzip $1
  elif [[ $(file $1 | grep bzip) != "" ]]
  then
    bunzip2 $1
  elif [[ $(file $1 | grep diff) != "" ]]  || 
       [[ $(file $1 | grep ASCII) != "" ]] ||
       [[ $(file $1 | grep text) != "" ]]
  then
    message "${MESSAGE_COLOR}$1 doesn't need uncompressing${DEFAULT_COLOR}"
  elif
       [[ $(file $1 | grep data) != "" ]] && [[ "$(echo $1|grep -o "\.[^.]*$")" == '.lzma' ]]
  then
       unlzma $1
  elif
       [[ $(file $1 | grep data) != "" ]] && [[ "$(echo $1|grep -o "\.[^.]*$")" == '.xz' ]]
  then
       unxz $1
  elif [[ $(file $1 | grep Zstandard) != "" ]]
  then
    unzstd $1
  else
    message "${PROBLEM_COLOR}$1 has some unknown/unimplemented compression type${DEFAULT_COLOR}"
    return 1
  fi
}

# message for little output to the user
case "$KMODE" in
  oldktree)
    if [[ -d ${LINUX_SOURCE_DIRECTORY} ]]
    then
      message "${MESSAGE_COLOR}Okay ${LINUX_SOURCE_DIRECTORY} does exist${DEFAULT_COLOR}" 
    else
      message "${PROBLEM_COLOR}Ah, ${LINUX_SOURCE_DIRECTORY} doesn't exist\nGoing to newktree mode${DEFAULT_COLOR}" &&
      KMODE="newktree"
    fi
  ;;
  newktree)
    message "${MESSAGE_COLOR}Using ${LINUX_SOURCE_DIRECTORY} for new build${DEFAULT_COLOR}"
  ;;
  expert)
    LINUX_SOURCE_DIRECTORY=$old_src_tree
    if [[ -d ${LINUX_SOURCE_DIRECTORY} ]]
    then
      message "${MESSAGE_COLOR}Using ${LINUX_SOURCE_DIRECTORY} for expert build${DEFAULT_COLOR}"
    else
      message "${PROBLEM_COLOR}Ah ${LINUX_SOURCE_DIRECTORY} doesn't exist exiting${DEFAULT_COLOR}"
      return 1
    fi
  ;;
  *)
    message "${PROBLEM_COLOR}Ah (KMODE) ${KMODE} isn't set properly${DEFAULT_COLOR}"
    message "${PROBLEM_COLOR}Fix this by editing /usr/src/linux-$(installed_version linux)/.spell-config.p${DEFAULT_COLOR}"
    return 1
  ;;
esac

# do the creation of tree if needed
case "${KMODE}" in
  newktree)
    mk_source_dir $LINUX_SOURCE_DIRECTORY &&
    cd $LINUX_SOURCE_DIRECTORY &&
    case $VERSION in
      4*|3.[0-9]*)
              case $SOURCE in
                *.bz2)
                message "${MESSAGE_COLOR}bunzip2 $SOURCE_CACHE/$SOURCE${DEFAULT_COLOR}"
                bunzip2 -k $SOURCE_CACHE/$SOURCE
                SOURCE_ORI=$SOURCE
                SOURCE=${SOURCE/.bz2}
                ;;
                *.xz)
                message "${MESSAGE_COLOR}unxz $SOURCE_CACHE/$SOURCE${DEFAULT_COLOR}"
                unxz -k $SOURCE_CACHE/$SOURCE
                SOURCE_ORI=$SOURCE
                SOURCE=${SOURCE/.xz}
                ;;
                *)
                message "${MESSAGE_COLOR}unknown compression: $SOURCE${DEFAULT_COLOR}"
                return 1
                ;;
              esac
              unpack_file '' || return 1
              message "${MESSAGE_COLOR}rm $SOURCE_CACHE/$SOURCE${DEFAULT_COLOR}"
              rm $SOURCE_CACHE/$SOURCE
              SOURCE=$SOURCE_ORI
              ;;
             *)
              unpack_file '' || return 1
              ;;
    esac
    mv ./*/* ./*/.[a-z]* ./ &&
    . ${SPELL_DIRECTORY}/latest.defaults &&
    local count=3
    local source="SOURCE${count}"
    local source_check="PATCH[${count}]"
    while [[ ! -z "${!source_check}" ]]
    do
      pval=1 &&
      patch="PATCH[${count}]"
      tmp=$(find ${SPELL_DIRECTORY} -name ${!patch} | head -n 1) &&
      . ${tmp} &&
      cd ${LINUX_SOURCE_DIRECTORY} &&
      if [[ ! -z "$source" ]]
      then
        verify_file "$count" &&
        cp -v ${SOURCE_CACHE}/${source} ./ &&
        linux_new_uncompress ./${source} &&
        message "${MESSAGE_COLOR}Patching ${!patch}${DEFAULT_COLOR}" &&
        patch -p${pval} <${!patch}
        rc=$? 
        if [[ $rc != 0 ]] 
        then
          message "${PROBLEM_COLOR}Bah, patch ${!patch} failed${DEFAULT_COLOR}" &&
          return 1
        fi
      fi
      (( count++ ))
      source="SOURCE${count}"
      source_check="PATCH[${count}]"
    done &&	
    patch -p1 < "$SPELL_DIRECTORY/0001-use-ld.bfd-to-prevent-linking-error.patch" &&

    chmod og-w -R $LINUX_SOURCE_DIRECTORY &&
    make mrproper
  ;;
  oldktree)
    message "${MESSAGE_COLOR}mode is oldktree${DEFAULT_COLOR}" &&
    cd $LINUX_SOURCE_DIRECTORY
  ;;
  expert)
    message "${MESSAGE_COLOR}mode is expert${DEFAULT_COLOR}" &&
    cd $LINUX_SOURCE_DIRECTORY
  ;;
  *)
    message "${PROBLEM_COLOR}I don't know what $mode is${DEFAULT_COLOR}" &&
    return 1
  ;;
esac &&

if [[ "$TXTMESSAGE" != "" ]]
then
  message $TXTMESSAGE &&
  sleep 5
fi &&
if [[ "$CONFIG_RESTORE" != "" ]]
then
  case "${CONFIG_RESTORE}" in
    /proc/config.gz)
      if [[ -f ${CONFIG_RESTORE} ]]
      then
        zcat /proc/config.gz > .config
      fi
    ;;
    *)
      if [[ -f ${CONFIG_RESTORE} ]]
      then
        cp -v ${CONFIG_RESTORE} .config
      fi
    ;;
  esac
fi &&
if [[ "$KLINUX_RECONFIGURE" == "true" ]] || [[ "$CONFIG_KERNEL" == "y" ]]
then
  case "$MAKEMODE" in
    menuconfig)
      MENUCONFIG_EXIT="n" &&
      while [[ "$MENUCONFIG_EXIT" == "n" ]]
      do
        make $MAKEMODE &&
        if query "Do you really want to exit menuconfig?" y 
        then
          MENUCONFIG_EXIT="y" 
        else
          MENUCONFIG_EXIT="n"
        fi
      done
    ;;
    smgl-update-defaults)
      yes '' | make oldconfig
    ;;
    *)
      make $MAKEMODE
    ;;
  esac
else
  case "$MAKEMODE" in
    defconfig|allyesconfig|allnoconfig|allmodconfig|randconfig)
      make $MAKEMODE
    ;;
    *)
      # run oldconfig just to make sure it's got a correct config
      yes '' | make oldconfig
    ;;
  esac
fi &&

# fix build via gcc 4.3
if [[ "$(installed_version gcc | sed 's:\.::g')" -ge "430" ]]
then
  sed -i "s:-declaration:-declaration -fno-tree-scev-cprop:" \
    $LINUX_SOURCE_DIRECTORY/Makefile
fi &&

# check to make sure .config exists 
if [[ ! -f ./.config ]]
then
  message "${PROBLEM_COLOR}Ah!!! no config file this should happen!!!${DEFAULT_COLOR}"
  message "${PROBLEM_COLOR}Try running cast -r $SPELL${DEFAULT_COLOR}"
  return 1
fi &&

# workaround for 4.14.10:
chmod -f +x "${LINUX_SOURCE_DIRECTORY}/tools/objtool/sync-check.sh" &&

# fix path to awk
find "${LINUX_SOURCE_DIRECTORY}" -name *.awk -o -name ver_linux \
  -exec sed '/usr\/bin\/awk/s;usr\/;;' -i {} \;
