#!/bin/bash

# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2021-present Team LibreELEC (https://libreelec.tv)

if [ -z "${1}" -o "${1}" == "help" -o "${1}" == "--help" -o "${1}" == "h" -o "${1}" == "-h" ]; then
  echo -e "A kernel config command must be specified:\n"
  echo -e "${0} menuconfig"
  echo -e ""
  echo -e "To see all available config commands use:\n"
  echo -e "${0} commands"
  exit
fi

KERNEL_CONFIG_COMMAND="$1"

. config/options linux

${SCRIPTS}/unpack linux

AVAILABLE_KERNEL_CONFIG_COMMANDS="$(kernel_make -C ${PKG_BUILD} help | sed -n '/Configuration targets:/,/Other generic targets:/p' | sed -n -E 's/^  ([a-z0-9]+)[ -]*.*/\1/p')"

print_config_commands() {
  echo -e "Available kernel config commands are:\n\n${AVAILABLE_KERNEL_CONFIG_COMMANDS}"
}

if [ "${1}" == "commands" ]; then
  print_config_commands
  exit
fi

if [ -z "$(echo ${AVAILABLE_KERNEL_CONFIG_COMMANDS} | grep ${KERNEL_CONFIG_COMMAND})" ]; then
  echo -e "\"${KERNEL_CONFIG_COMMAND}\" is not an available kernel config command\n"
  print_config_commands
  exit
fi

tools/check_kernel_config

kernel_make KCONFIG_CONFIG=${PKG_KERNEL_CFG_FILE} -C ${PKG_BUILD} ${KERNEL_CONFIG_COMMAND}
