#!/bin/bash

# Slackware bash script named SBKS (Slack-Build-Kernel-Script)
# For Slackware64 and Slackware based distros.
# Anagnostakis Ioannis Greece 2022-2025 rizitis@gmail.com
# REQUIRES="kdialog"
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if [ "$EUID" -ne 0 ];then
    kdialog --error \
       --title "Error Message" \
       --width 500 \
       --height 100 \
       --text "Permission denied. Please run SBKS as root user (no sudo)"
    exit 1
fi

if [ -x /usr/bin/kdialog ]; then
    echo "kdialog found"
else
    echo "************************************"
    echo "kdialog not found. Please install kdialog"
    echo "************************************"
    exit 1
fi

# ADD Linux Kernel version you want to build here
KERNEL_VERSION=$(kdialog --inputbox "Enter the kernel version you want to build. Example: 6.2.8")
echo "Kernel version selected: $KERNEL_VERSION"  # Echo to terminal

# If your oldconfig was built by u and your old kernel’s name is tagged by you assume: "6.0.13-tag"
# then add your TAG in VERSION2. Example VERSION2=6.0.14-TAG else script will fail using oldconfig.
VERSION2=$(kdialog --inputbox "Enter kernel version again. If your current kernel version custom with TAG, then add it here also. Example: 6.2.8-rtz")
echo "Kernel version again if tag also: $VERSION2"  # Echo to terminal

# script asks for current bootloader
BOOTLOADER=$(kdialog --inputbox "Enter the number of your bootloader. Example: Elilo=5 Lilo=0 Grub=7 myown=100")
echo "Bootloader number: $BOOTLOADER"  # Echo to terminal

set -e
kdialog --yesno "Check your inputs in terminal. Should we proceed?" --title "SBKS Question?"
if [ $? -ne 0 ]; then
    echo "User canceled. Exiting..."
    exit 1
fi

set +e

# Normal wont need to edit the next lines except if you want to use "make menuconfig or other make_config"
KERNEL_URL=https://cdn.kernel.org/pub/linux/kernel/v6.x  # Don't change if we are still in 6.x.x kernel
PRGNAM=linux
GPG=gpg2
WGET=wget
JOBS=-j$(getconf _NPROCESSORS_ONLN)

cd /usr/src/
# Check if BOTH kernel version AND signature file exist
$WGET -c --spider $KERNEL_URL/linux-"$KERNEL_VERSION".tar.{sign,xz}
if [ $? ]; then
    ($WGET -c $KERNEL_URL/linux-"$KERNEL_VERSION".tar.{xz,sign}) | sed -u 's/.* \([0-9]\+%\)\ \+\([0-9.]\+.\) \(.*\)/\1\n# Downloading at \2\/s, ETA \3/' | kdialog --progressbar "Downloading Files..." --percentage=1 --pulsate --time-remaining --auto-kill --auto-close \
    --width 800 \
    --height 50
    echo "Kernel and signature files downloaded."  # Echo to terminal
fi

# Using GnuPG to verify kernel signatures
# Not working with all kernels every time, sorry, that's why set -e starts after this step.
unxz linux-"$KERNEL_VERSION".tar.xz
$GPG --verify linux-"$KERNEL_VERSION".tar.sign
xz -cd "$KERNEL_VERSION".tar.xz | gpg2 --verify linux-"$KERNEL_VERSION".tar.sign -
echo "Kernel signature verified."  # Echo to terminal

set -e
kdialog --yesno "Please check your terminal if RSA keys are the same. Should we proceed?" --title "SBKS Proceed?"
if [ $? -ne 0 ]; then
    echo "User canceled. Exiting..."
    exit 1
fi

# Just in case...
if [ "$EUID" -ne 0 ]; then
    echo "Please run this script as root"
    exit 1
fi

# create a directory, put the kernel tarball there for the rest of the work
mkdir -p kernel-"$KERNEL_VERSION"
cp $PRGNAM-"$KERNEL_VERSION".tar kernel-"$KERNEL_VERSION"/
cd kernel-"$KERNEL_VERSION"/
echo "Untarring Linux kernel"
sleep 2
tar -xf $PRGNAM-"$KERNEL_VERSION".tar
echo "cd to Linux-kernel package"
# My old .config I use has commented
# CONFIG_SYSTEM_TRUSTED_KEYS
# CONFIG_SYSTEM_TRUSTED_KEYRING
sleep 2
cd $PRGNAM-"$KERNEL_VERSION"/
zcat /proc/config.gz > .config
make oldconfig
# uncomment next line if you want to use make menuconfig
# make menuconfig
make "$JOBS"
make modules_install

# fix vmlinuz and System.map. Some things are optional but just in case... not hurt.
cp arch/x86_64/boot/bzImage /boot/vmlinuz-"$VERSION2"
cp System.map /boot/System.map-"$VERSION2"

cd /boot
rm System.map
ln -s System.map-"$VERSION2" System.map

dracut  --force --kver "$VERSION2" /boot/initramfs-"$VERSION2".img
sleep 2

# update-bootloader
if [[ "$BOOTLOADER" == 7 ]]; then
    grub-mkconfig -o /boot/grub/grub.cfg
    echo "Grub configuration updated."  # Echo to terminal
elif [[ "$BOOTLOADER" == 5 ]]; then
    eliloconfig
    echo "Elilo configuration updated."  # Echo to terminal
elif [[ "$BOOTLOADER" == 0 ]]; then
    lilo
    echo "Lilo configuration updated."  # Echo to terminal
else
    kdialog --warning \
    --text="Failed to update bootloader. Please do it manually."
    echo "Bootloader update failed. Please update manually."  # Echo to terminal
fi

if [[ "$BOOTLOADER" == 100 ]]; then
    kdialog --warning \
    --text="Please update the bootloader manually."
    echo "Bootloader update manually required."  # Echo to terminal
fi

sleep 2
set +e

kdialog --warning \
       --title "Warning Message" \
       --width 500 \
       --height 100 \
       --text "Finally, you have successfully broken your system. Congrats!"
echo "Script has finished execution."  # Echo to terminal

if kdialog --yesno "Do you want to reboot the PC?" --title "Reboot?"; then
    kdialog --msgbox "System rebooting..." --title "Success"
    echo "System is rebooting..."  # Echo to terminal
    /sbin/reboot
else
    kdialog --msgbox "OK! SBKS finished its job. Have a nice Slacky Day." --title "Goodbye"
    echo "Script finished. No reboot."  # Echo to terminal
fi

