#!/bin/sh
#----------------------------------------------------------------------------
# /tmp/install.sh - install eiskernel-dev
#
# Copyright (c) 2001-2025 the eisfair team, team(at)eisfar(dot)org
#
# Creation:     2004-01-09 jv
# Last Update:  2025-12-19 02:29:26 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.
#----------------------------------------------------------------------------
. /var/install/include/eislib

system="$(lsb_release -d | awk '{ print $2 }')"
kernel_version="6.12.62"
source_file="linux-6.12.62.tar.xz"
source_folder="linux-6.12.62-eisfair"
running_kernel="$(uname -r)"

#-----------------------------------------------------------------------------
# prepare kernel source configuration
#-----------------------------------------------------------------------------
setup_kernel_config()
{
    if echo "${running_kernel}" | grep -E -q ".*-VIRT$"
    then
        mecho "Configuring kernel source for ${system}-VIRT ..."
        ./switch-config "virt" > /dev/null
        return 0
    fi

    if echo "${running_kernel}" | grep -E -q ".*-PAE$"
    then
        mecho "Configuring kernel source for ${system}-PAE ..."
        ./switch-config "pae" > /dev/null
        return 0
    fi

    if echo "${running_kernel}" | grep -E -q ".*-SMP$"
    then
        mecho "Configuring kernel source for ${system}-SMP ..."
        ./switch-config "smp" > /dev/null
        return 0
    fi

    mecho --warn "unknown kernel variant found!"
    mecho --warn "kernel configuration was not initialized!"
}

#-----------------------------------------------------------------------------
# patch uninstall kernel source routines
#-----------------------------------------------------------------------------
patch_source_uninstall_scripts()
{
    local files=$(ls /var/install/deinstall/linux-source-*)
    local file
    for file in $files
    do
        sed -i 's#ls /usr/src/linux-\*-eisfair\*#ls -d /usr/src/linux-*-eisfair*#g' ${file}
        sed -i 's#ln -s "\${next_source_package}"#ln -sr "${next_source_package}"#g' ${file}
    done
}

#-----------------------------------------------------------------------------
# main program
#-----------------------------------------------------------------------------
main()
{
    mecho "Extracting kernel source ..."

    if ! tar -C "/usr/src/${source_folder}" \
         -xf "/usr/src/${source_file}" \
         --strip-components 1
    then
        mecho --error "Unable to extract kernel source!"
        mecho --error "Leaving source archive in /usr/src."
        exit 0
    fi

    mecho "Patching kernel source ..."
    cd "/usr/src/${source_folder}"
    patch -p1 -s < "/usr/src/${source_folder}/6.12.62.patch"

    setup_kernel_config
    cd - > /dev/null

    rm -f "/usr/src/linux"
    ln -sr "/usr/src/${source_folder}" "/usr/src/linux"

    rm -f "/usr/src/${source_file}"

    patch_source_uninstall_scripts
}

main "${@}"
