### vim:ft=zsh:foldmethod=marker
###
### this is just a simple wrapper around 'make'
###
### uses special compiler (and destination) to build the kernel.
### vars used: ${KERNEL_COMPILER}, ${KERNEL_BUILD_DIR}

if [[ ${OSTYPE} != linux* ]] ; then
    printf 'kmake is only available on linux.\n'
    return 1
fi
check_com_print make || return 1

setopt localoptions
setopt rmstarsilent
setopt normstarwait
local opwd

case $1 in
    (-buildclean)
        if [[ -z ${KERNEL_BUILD_DIR} ]] ; then
            printf '\${KERNEL_BUILD_DIR} not set. ignoring.\n'
        else
            builtin cd ${KERNEL_BUILD_DIR}
            command rm -Rf *
            command rm -f .*(.)
            builtin cd ${OLDPWD}
        fi
        return
        ;;
    (-catconfig)
        if [[ -n ${KERNEL_BUILD_DIR} ]] && [[ -f /proc/config.gz ]] ; then
            command zcat /proc/config.gz > "${KERNEL_BUILD_DIR}/.config"
        else
            printf '\${KERNEL_BUILD_DIR} not set or /proc/config.gz not found. ignoring.\n'
        fi
        return
        ;;
esac

if [[ -z ${KERNEL_BUILD_DIR} ]] ; then
    command make "$@" V=1 CC=${KERNEL_COMPILER:-gcc} HOSTCC=${KERNEL_COMPILER:-gcc}
else
    command make "$@" V=1 CC=${KERNEL_COMPILER:-gcc} HOSTCC=${KERNEL_COMPILER:-gcc} O=${KERNEL_BUILD_DIR}
fi