#!/bin/sh
#----------------------------------------------------------------------------
# /tmp/preinstall.sh - preinstall kernel update
#
# Copyright (c) 2001-2025 the eisfair team, team(at)eisfar(dot)org
#
# Creation   : 2008-08-14 tb
# Last Update: 2025-07-06 15:26:15 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


#-----------------------------------------------------------------------------
# check if glibc update requires reboot
#-----------------------------------------------------------------------------
check_for_glibc_reboot_sentinel()
{
    if [ -f /etc/rc2.d/S99resetlibc ]
    then
        echo
        echo
        mecho --error "Sorry, you have to reboot the system before installing this"
        mecho --error "package. Start the kernel installation after the reboot again."
        echo
        echo
        return 1
    fi
    return 0
}

#-----------------------------------------------------------------------------
# check if cpu is able to run this kernel
#-----------------------------------------------------------------------------
check_for_missing_cpu_features()
{
    local missing_cpu_feature=''
    if ! grep -q ' cmov ' /proc/cpuinfo
    then
        missing_cpu_feature='cmov'
    fi

    if ! grep -q ' pae ' /proc/cpuinfo
    then
        if [ -z "${missing_cpu_feature}" ]
        then
            missing_cpu_feature='pae'
        else
            missing_cpu_feature="${missing_cpu_feature pae}"
        fi
    fi

    if [ -n "${missing_cpu_feature}" ]
    then
        echo
        echo
        mecho --error "Sorry, your cpu misses the following features:"
        mecho --warn "${missing_cpu_feature}"
        mecho --error "Please use the SMP kernel!"
        echo
        echo
        return 1
    fi
    return 0
}

#-----------------------------------------------------------------------------
# check if hda entries are present in fstab
#-----------------------------------------------------------------------------
check_for_invalid_entries_in_fstab()
{
    local fstab_hd_devices=$(grep /dev/hd /etc/fstab | grep -v '^#' | grep -Ev 'noauto')
    if [ -n "${fstab_hd_devices}" ]
    then
        echo
        echo
        mecho --error "You mount /dev/hdX devices in /etc/fstab:"
        mecho --warn "$fstab_hd_devices"
        mecho --error "There is no driver for /dev/hdX devices in this kernel anymore."
        echo
        echo
        return 1
    fi
    return 0
}

#-----------------------------------------------------------------------------
# check if lilo config is valid or broken
#-----------------------------------------------------------------------------
check_for_valid_lilo_config()
{
    if [ -f /usr/libexec/verify-bootloader ]
    then
        echo "Verifying existing bootloader configuration ..."
        /usr/libexec/verify-bootloader
        return "$?"
    fi
    return 0
}

#-----------------------------------------------------------------------------
# main program
#-----------------------------------------------------------------------------
main()
{
    if ! check_for_glibc_reboot_sentinel
    then
        anykey
        exit 1
    fi

    if ! check_for_missing_cpu_features
    then
        anykey
        exit 1
    fi

    if ! check_for_valid_lilo_config
    then
        anykey
        exit 1
    fi

    # fix deinstall script file name
    if [ -f "/var/install/deinstall/linux-kernel-5.10.26-smp" ]
    then
        mv "/var/install/deinstall/linux-kernel-5.10.26-smp" \
           "/var/install/deinstall/linux-kernel-5.10.31-smp"
    fi

    # fix file list
    if [ -f "/etc/filelist.d/linux-kernel-5.10.31-smp-files.txt" ]
    then
        sed -i 's/5\.10\.26/5.10.31/g' "/etc/filelist.d/linux-kernel-5.10.31-smp-files.txt"
    fi

    # check for remainders of kernel 5.15.157 (user-merge)
    if [ -d /usr/lib/modules/5.15.157-eisfair-1-PAE ]
    then
        if [ "$(eisman check linux-kernel-5.15.157-pae)" = "not-installed" ]
        then
            rm -rf /usr/lib/modules/5.15.157-eisfair-1-PAE
        fi
    fi

    # check for remainders of kernel 5.15.159 (user-merge)
    if [ -d /usr/lib/modules/5.15.159-eisfair-1-PAE ]
    then
        if [ "$(eisman check linux-kernel-5.15.159-pae)" = "not-installed" ]
        then
            rm -rf /usr/lib/modules/5.15.159-eisfair-1-PAE
        fi
    fi

    # since kernel archive is quite big, print some text so that
    # users (used to very verbose output) stay calm
    echo "Extracting archive ..."
    exit 0
}

main "${@}"
