#!/bin/bash
# Gettext internationalization
#
# Copyright 2022-2025 Dimitris Tzemos, GR
#!/bin/bash
# All rights reserved.
#
# 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.
#
########################################### #######################
####                      install-upgrade-kernel               ####
###################################################################
# script to install/upgrade new kernel images                     #
# when kernel upgrade on repos                                    #
# when installing/upgrading generic kernel an initrd is created   #
# This script created for slackel/salix                           #
#                                                                 #
###################################################################

AUTHOR='Dimitris Tzemos - dijemos@gmail.com'
LICENCE='GPL v3+'
SCRIPT=$(basename "$0")
SCRIPT=$(readlink -f "$SCRIPT")
VER=5.0

init(){

		if [ "$UID" != "0" ]; then
			echo "You need to be root to run this"
			exit 1
		fi

        # Translations only work with utf8 locales
        if [ ! `echo $LANG | grep -i utf` ]; then
			LANG=C
        fi

        # Gettext internationalization
        export TEXTDOMAIN="install-upgrade-kernel"
        export TEXTDOMAINDIR="/usr/share/locale"
        . gettext.sh

        # Path needs to be extended in case you're only half a root :)
        export PATH="/usr/sbin:/sbin:${PATH}"
}

usage(){
        echo "`eval_gettext 'USAGE: install-upgrade-kernel [install|upgrade] [generic]'`"
        echo
        echo "`eval_gettext '   OPTIONS:'`"
        echo "`eval_gettext '   install  Install generic kernel'`"
        echo "`eval_gettext '   upgrade  Upgrade generic kernel removing the old one'`"
        echo "`eval_gettext '   generic  Download generic kernel and also create an initrd'`"
        exit 1
}

# Check which options are used from the command line
checkoptions(){
ROOTPART=$(lsblk -l -o name,mountpoint| grep /$|sed "s,\([^[:space:]]*\).*,/dev/\1,")
ROOTNAME=$(echo $ROOTPART|sed "s,/dev/,,")
ROOTUUID=$(lsblk -l -o name,uuid|grep ^$ROOTNAME|sed "s/[^ ]*[[:space:]]*//")
export FSTYPE=$(lsblk -l -o name,fstype|grep ^$ROOTNAME|sed "s/[^ ]*[[:space:]]*//")
ROOTDEVICE=/dev/$(lsblk -l -o name,pkname|grep ^$ROOTNAME|sed "s/[^ ]*[[:space:]]*//")

        flag=$1
        flagr=$2
        flagw=$3
        flagf=$4

		if [ $# -gt 4 ] ; then
			echo "`eval_gettext 'ERROR: Too many arguments'`"
			usage
		fi

        if [[ $flag = "install" ]]; then
			export flag="install"
        elif [[ $flag = "upgrade" ]]; then
			export flag="upgrade"
		else
		   usage
        fi

        #if switch is unknown or if asking for help
		if [[ $flagr = "help" ]] || [[ $flag = "h" ]]; then
			usage
	    elif [[ $flagr = "generic" ]]; then
			export flagr="generic"
		else
			usage
		fi
		
 if [ -z "$flagw" ]; then
			export WAIT=10
	   else
	        export WAIT=$flagw
	   fi

       if [ -z "$flagf" ]; then
			#export flagf="ext4"
			export flagf=$FSTYPE
	   elif [ "$flagf" = "ext2" ]; then
			export flagf="ext2"
		elif [ "$flagf" = "ext3" ]; then
			export flagf="ext3"
		elif [ "$flagf" = "ext4" ]; then
			export flagf="ext4"
		elif [ "$flagf" = "btrfs" ]; then
			export flagf="btrfs"
		elif [ "$flagf" = "jfs" ]; then
			export flagf="jfs"
		elif [ "$flagf" = "reiserfs" ]; then
			export flagf="reiserfs"
		elif [ "$flagf" = "xfs" ]; then
			export flagf="xfs"
		else
			usage
		fi		
}
execute_script(){
if [ -z "$startdir" ]; then
	startdir="$(pwd)"
	export startdir
fi

#moduleslist="squashfs:overlay:loop:xhci-pci:ohci-pci:ehci-pci:xhci-hcd:uhci-hcd:ehci-hcd:mmc-core:mmc-block:sdhci:sdhci-pci:sdhci-acpi:usb-storage:hid:usbhid:i2c-hid:hid-generic:hid-apple:hid-asus:hid-cherry:hid-logitech:hid-logitech-dj:hid-logitech-hidpp:hid-lenovo:hid-microsoft:hid_multitouch:jbd2:mbcache:crc32c-intel:ext3:ext4:isofs:fat:nls_cp437:nls_iso8859-1:msdos:vfat:ntfs"
#moduleslist="squashfs:overlay:efivarfs:loop:xhci-pci:ohci-pci:ehci-pci:xhci-hcd:uhci-hcd:ehci-hcd:mmc-core:mmc-block:sdhci:sdhci-pci:sdhci-acpi:usb-storage:hid:usbhid:i2c-hid:hid-generic:hid-apple:hid-asus:hid-cherry:hid-logitech:hid-logitech-dj:hid-logitech-hidpp:hid-lenovo:hid-microsoft:hid_multitouch:mptbase:mptscsih:mptspi:jbd2:mbcache:crc32c_intel:crc32c_generic:fuse:ext2:ext3:ext4:xfs:reiserfs:jfs:btrfs:isofs:fat:nls_cp437:nls_iso8859-1:msdos:vfat:ntfs"
moduleslist="squashfs:overlay:loop:efivarfs:xhci-pci:ohci-pci:ehci-pci:xhci-hcd:uhci-hcd:ehci-hcd:mmc-core:mmc-block:sdhci:sdhci-pci:sdhci-acpi:usb-storage:hid:usbhid:i2c-hid:hid-generic:hid-apple:hid-asus:hid-cherry:hid-logitech:hid-logitech-dj:hid-logitech-hidpp:hid-lenovo:hid-microsoft:hid_multitouch:mbcache:jbd2:mptscsih:mptspi:jbd:crc32c_intel:crc32c_generic:fuse:ext2:ext3:ext4:xfs:reiserfs:jfs:btrfs:isofs:fat:nls_cp437:nls_iso8859-1:msdos:vfat:ntfs:exfat:virtio_ring:virtio:virtio_blk:virtio_balloon:virtio_pci:virtio_pci_modern_dev:virtio_net:zstd_compress:lz4hc_compress:lz4_compress:rtsx_pci:rtsx_pci_sdmmc:uas"

#clear
#packages_dir contain packages.txz
packagesdirectory=$startdir/packages_dir
mkdir -p $packagesdirectory
slapt-get --clean
slapt-get -u
if [ "$flagr" == "generic" ]; then
	if [ `uname -m` == "x86_64" ]; then
		slapt-get -i --reinstall -d kernel-headers kernel-generic
	else
		slapt-get -i --reinstall -d kernel-headers kernel-generic
	fi
fi

if [ `uname -m` == "x86_64" ]; then
	cp `find /var/slapt-get/ -type f -name "kernel-headers*"` $packagesdirectory
	if [ "$flagr" == "generic" ]; then
		cp `find /var/slapt-get/ -type f -name "kernel-generic*"` $packagesdirectory
	fi	
	
	if [ "$flagr" == "generic" ]; then
		cd $packagesdirectory
		f=`ls kernel-generic*.txz`
		kv=`echo $f | cut -d'-' -f 3`
		cd $startdir
	fi
else
	cp `find /var/slapt-get/ -type f -name "kernel-headers*"` $packagesdirectory
	if [ "$flagr" == "generic" ]; then
		cp `find /var/slapt-get/ -type f -name "kernel-generic*"` $packagesdirectory
	fi
	
	if [ "$flagr" == "generic" ]; then
		cd $packagesdirectory
		f=`ls kernel-generic*.txz`
		kv=`echo $f | cut -d'-' -f 3`
		cd $startdir
	fi
fi

if [ -f /etc/lsb-release ]; then
	sed -i "s/DISTRIB_DESCRIPTION=.*/DISTRIB_DESCRIPTION='Slackel Linux $kv'/" /etc/lsb-release
fi

if ! [ -f /etc/slackel-version ]; then
	echo "Slackel 7.7" > /etc/slackel-version
fi

if [ "$flag" == "install" ]; then
	installpkg $packagesdirectory/*
	rm /boot/vmlinuz-huge /boot/vmlinuz-huge-smp >/dev/null 2>&1
	rm -rf /boot/initrd* >/dev/null 2>&1
	#kv=`ls -l /boot/vmlinuz | cut -f2 -d'>' | sed s/^[^0-9]*//`
	kv=`ls -l /boot/vmlinuz | cut -f2 -d'>' | cut -f2 -d'-'`
	if [ "$flagr" == "generic" ]; then
		echo "creating /boot/initrd-${kv}.gz"
		mkinitrd -c -f $flagf -u -w $WAIT -o /boot/initrd-${kv}.gz -r UUID=$ROOTUUID -k $kv  -m $moduleslist >/dev/null 2>&1
		rm -rf /boot/initrd-tree/
	fi
	rm /boot/vmlinuz-huge /boot/vmlinuz-huge-smp >/dev/null 2>&1
	if [ -f /usr/sbin/update-grub ]; then
		echo "updating grub"
		update-grub
	fi 
	rm -rf $packagesdirectory
elif [ "$flag" == "upgrade" ]; then
	upgradepkg $packagesdirectory/*
	rm /boot/vmlinuz-huge /boot/vmlinuz-huge-smp >/dev/null 2>&1
	rm -rf /boot/initrd* >/dev/null 2>&1
	#kv=`ls -l /boot/vmlinuz | cut -f2 -d'>' | sed s/^[^0-9]*//`
	kv=`ls -l /boot/vmlinuz | cut -f2 -d'>' | cut -f2 -d'-'`
	if [ "$flagr" == "generic" ];then
		echo "creating /boot/initrd-${kv}.gz"
		mkinitrd -c -f $flagf -u -w $WAIT -o /boot/initrd-${kv}.gz -r UUID=$ROOTUUID -k $kv  -m $moduleslist >/dev/null 2>&1
		rm -rf /boot/initrd-tree/
	fi
	rm /boot/vmlinuz-huge /boot/vmlinuz-huge-smp >/dev/null 2>&1

	if [ -f /usr/sbin/update-grub ]; then
		echo "updating grub"
		update-grub
	fi  
	rm -rf $packagesdirectory
fi
}

init
checkoptions $@
execute_script
