#!/bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/eiskernel-dev-switch-kernel-config - switch to kernel configuration
#
# Copyright (c) 2001-2024 the eisfair team, team(at)eisfar(dot)org
#
# Creation   : 2013-01-18 tb
# Last Update: 2024-04-30 22:21:23 dv
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

system="$(lsb_release -d | awk '{ print $2 }')"
local_kernel_version="5.15.157-${system}"

. /var/install/include/eislib

#----------------------------------------------------------------------------
# exit with error message and exit code 1
#----------------------------------------------------------------------------
exit_failure()
{
    mecho
    mecho --error "${1}"
    mecho

    cd - > /dev/null
    exit 1
}

#----------------------------------------------------------------------------
# normal exit
#----------------------------------------------------------------------------
exit_success()
{
    cd - > /dev/null
}

#----------------------------------------------------------------------------
# main routine
#----------------------------------------------------------------------------
main()
{
    flavour="${1}"

    # set defconfig for specified kernel
    if [ "$(arch)" = "x86_64" ]
    then
        case "${flavour}" in
        smp|pae)
            mecho --warn "'smp' and 'pae' is not available for this platform" >&2
            exit 1
            ;;
        virt)
            defconfig="${system}_virt_defconfig"
            kernel_name="5.15.157-${system}-VIRT"
            ;;
        *)
            mecho --warn "usage: ${0} virt" >&2
            exit 1
            ;;
        esac
    else
        case "${flavour}" in
        smp)
            defconfig="${system}_smp_defconfig"
            kernel_name="5.15.157-${system}-SMP"
            ;;
        pae)
            defconfig="${system}_pae_defconfig"
            kernel_name="5.15.157-${system}-PAE"
            ;;
        virt)
            defconfig="${system}_virt_defconfig"
            kernel_name="5.15.157-${system}-VIRT"
            ;;
        *)
            mecho --warn "usage: ${0} smp|pae|virt" >&2
            exit 1
            ;;
        esac
    fi

    # change into the directory where the script is located
    cd $(dirname ${0})

    rm -f .config
    rm -f Module.symvers

    mecho --info "Making mrproper ..."
    if ! make mrproper
    then
        exit_failure "Failed: make mrproper!"
    fi

    mecho --info "Making defconfig ..."
    if ! make defconfig "${defconfig}"
    then
        mecho --warn "Maybe the defconfig ${defconfig} is not available for this"
        mecho --warn "architecture."
        exit_failure "Failed: make defconfig ${defconfig}"
    fi

    if [ -f /var/lib/alt-kernel/symvers-${kernel_name}.gz ]
    then
        zcat /var/lib/alt-kernel/symvers-${kernel_name}.gz > Module.symvers
    else
        mecho --warn "'symvers-${kernel_name}.gz' not found!"
        rm -f Module.symvers
    fi

    mecho --info "Making modules_prepare ..."
    if ! make modules_prepare
    then
        exit_failure "Failed: make modules_prepare!"
    fi

    mecho --info "Source is now configured for ${kernel_name}!"
    exit_success
}

main "$@"