#!/bin/bash

# Written by Sergio Vicari (2016-2023) <devteamtuc@gmail.com>

# update-initrd: Generate initrd file for generic karnel in  Slackware.

#    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 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


if [ "$EUID" -ne 0 ];then
    echo "To generate a new initrd you need to be root"
    exit 1
fi

ACTDIR=$(pwd)
cd /boot

KERNV=$(ls -l vmlinuz-generic | awk -F  "-" '{print $5}')
EXTV=$(mount | grep "on / " | awk '{print $5}')
SDXI=$(mount | grep "on / " | awk '{print $1}')
ININAME=initrd-generic-$KERNV.img.gz
LNKNAME=initrd-generic.img.gz

echo Generating initrd-generic-$KERNV.img.gz with root partition: $SDXI
mkinitrd -c -k $KERNV -m $EXTV -f $EXTV -r $SDXI  -o $ININAME
    if [ $? -eq 0 ]; then
	echo  -SUCCESS-
	rm /boot/$LNKNAME
	ln -s  $ININAME /boot/$LNKNAME
	cd $ACTDIR
	
	echo You should now run lilo [perhaps before editing /etc/lilo.conf].
	echo Example: Typical configuration in /etc/lilo.conf
	echo -
	
	echo +++++++++++++++++++++++++++++++++++++++++++++++++
	echo  Linux bootable partition config begins
	echo +++++++++++++++++++++++++++++++++++++++++++++++++
	echo image = /boot/vmlinuz-generic
	echo   root = $SDXI
	echo   label = GNU_Slackware_Linux
	echo   initrd = /boot/$LNKNAME
	echo   read-only
	echo +++++++++++++++++++++++++++++++++++++++++++++++++
	echo Linux bootable partition config ends
	echo +++++++++++++++++++++++++++++++++++++++++++++++++

    else
	cd $ACTDIR
	echo  -FAILED-
#	read -p "Press any key to skip and continue"
    fi
echo END
