#!/bin/sh

# Known issue: espeakup fails to start speakup with the soft synthesizer
# if another one (at least apollo) is loaded from the initramfs.

# This script is intended to update a system installed on an USB device
# after a kernel upgrade. It assumes that a generic kernel is used.
# It builds and installs a new initrd.

# Changes from 22 October 2018:
# 24 October 2018:
# Module espeakup_apollo removed, as workaround to allow blind users to
# have speech at least with the soft synthesizer when logging in.
#
# Default ROOTDELAY changed from 20 to 10 seconds.
#
# Set ROOTPART with lsblk instead of df for consistency to avoid issue
# of df -h naming /dev/root the root partition.
#
# Set KERNELVERSION to the one of the most recently installed
# generic kernel: preparation for allowing to keep the previous kernel
# as backup.
#
# Remove the previous /boot/vmlinuz-generic symlink
#
# Typo fix.
#
# 29 October 2018:
#
# Added the kernel module crc32c-intel to $ext4, needed for Linux 4.19.0 at
# least on some machines. Also added to $ext3, just in case.

ROOTDELAY=10
if [ ! $(id -u) -eq 0 ]; then
	echo "Only root may run this script."
	exit
fi
echo '
This script is intended to update a system installed on an USB device
after a kernel upgrade. It assumes that a generic kernel is used.
It builds and installs a new initrd.

Prior to run this script you should have updated the kernel. The script
runs lilo and update all that elilo needs. elilo.conf will be saved as
elilo.conf.orig before updating it so that you can easily redo any
customization. Please remove elilo.conf.orig after next boot so
elilo.conf be saved again after next kernel upgrade.

If a huge kernel is installed, it will be removed. Make sure that a
generic kernel be installed before proceeding!
'
read -p "Are you ready to continue[y/N]? " ANSWER
if [ ! "$ANSWER" = "y" ] && [ ! "$ANSWER" = "Y" ]; then
	echo "See you later, then."
	exit
fi
#SPEAKUP="speakup \
#speakup_audptr \
#speakup_dectlk \
#speakup_spkout \
#speakup \
#speakup_decext \
#speakup_bns \
#speakup_dummy \
#speakup_ltlk \
#speakup_txprt \
#speakup_acntsa \
#speakup_soft \
#speakup_apollo"
USB_HOST_AND_STORAGE="mmc_core \
pcmcia \
pcmcia_core \
ssb \
ehci-hcd \
ehci-pci \
ehci-platform \
fotg210-hcd \
isp116x-hcd \
isp1362-hcd \
ohci-hcd \
ohci-pci \
ohci-platform \
oxu210hp-hcd \
r8a66597-hcd \
sl811-hcd \
sl811_cs \
ssb-hcd \
uhci-hcd \
whci-hcd \
xhci-hcd \
xhci-pci \
xhci-plat-hcd \
uas \
ums-freecom \
usb-storage \
wusbcore \
umc \
uwb \
whci"
#EFI="efi-pstore \
#efivars"
SPEAKUP="speakup_soft"
USB_KBD="uhci-hcd \
usbhid \
hid-generic"
btrfs="btrfs"
ext2="mbcache \
ext2"
ext3="jbd2 \
mbcache \
crc32c-intel \
ext4"
ext4="jbd2 \
mbcache \
crc32c-intel \
ext4"
jfs="jfs"
reiserfs="reiserfs"
xfs="xfs"
MODULES="$USB_HOST_AND_STORAGE $SPEAKUP $USB_KBD"
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:]]*//")
FSTYPE=$(lsblk -l -o name,fstype|grep ^$ROOTNAME|sed "s/[^ ]*[[:space:]]*//")
ROOTDEVICE=/dev/$(lsblk -l -o name,pkname|grep ^$ROOTNAME|sed "s/[^ ]*[[:space:]]*//")
export KERNELVERSION=$(ls -1tr /boot/vmlinuz-generic-*|sed -n '/$/s,.*-,,p')
case $FSTYPE in
	btrfs) FS=$btrfs;;
	ext2) FS=$ext2;;
	ext3) FS=$ext3;;
	ext4) FS=$ext4;;
	jfs) FS=$jfs;;
	reiserfs) FS=$reiserfs;;
	xfs) FS=$xfs;;
	*) echo "Root file system $FSTYPE is not supported."
		exit
esac
MODULES=$(printf "$MODULES $FS"|sed "s/ /:/g")
lsblk -l -o uuid,type,name|grep part| \
sed "s, *\([^ ]*\) *[^ ]*  *\([^ ]*\).*,s;/dev/\2;UUID=\1;," > /tmp/part_list
if [ ! -f /etc/fstab ]; then
	echo "No /etc/fstab found. Something should be wrong."
	echo "Bye."
fi
mkinitrd -c -k $KERNELVERSION -f $FSTYPE -r UUID=$ROOTUUID -m $MODULES -w $ROOTDELAY -u -o /boot/initrd.gz
if [ ! -f /etc/fstab.orig ]; then
	cp /etc/fstab /etc/fstab.orig
fi
sed -f /tmp/part_list /etc/fstab >dummy
mv dummy /etc/fstab
# Let's replace the huge kernel by a generic one
# Remove the huge kernel
if [ -f /boot/vmlinuz-huge-* ]; then
	removepkg kernel-huge 
	# Fix the symlinks
	( cd /boot
	rm -f System.map config config-generic vmlinuz vmlinuz-generic
	ln -s System.map-generic-* System.map
	ln -s vmlinuz-generic-* vmlinuz
	ln -s vmlinuz-generic-* vmlinuz-generic
	)
fi
# We assume that elilo.conf has been installed by the script
# install_with_UUID.sh and that no "root=.." line has been added to it.
#if [ -f /etc/lilo.conf ]; then
#	sed "s,^boot.*,boot = $ROOTDEVICE,"  /etc/lilo.conf > dummy
#	mv dummy /etc/lilo.conf
#	chown root:root /etc/lilo.conf
#	lilo
#fi
#if grep -q /boot/efi /etc/fstab; then
#	if [ "$(df -h|grep /boot/efi)" = "" ]; then
#		mount /boot/efi
#	fi
#	BRAND=$(grep ^NAME /etc/os-release|sed "s/.*=//")
#	if [ -f /boot/efi/EFI/$BRAND/elilo.conf ]; then
#		if [ ! -f /boot/efi/EFI/$BRAND/elilo.conf.orig ]; then
#			cp /boot/efi/EFI/$BRAND/elilo.conf \
#			/boot/efi/EFI/$BRAND/elilo.conf.orig
#		fi
#		sed "s/root=[^[:space:]]*[[:space:]]*//" /boot/efi/EFI/$BRAND/elilo.conf > dummy
#		mv dummy /boot/efi/EFI/$BRAND/elilo.conf 2>/dev/null
#		if ! grep -q initrd.gz /boot/efi/EFI/$BRAND/elilo.conf; then
#			echo "  initrd = initrd.gz" >> /boot/efi/EFI/$BRAND/elilo.conf
#		fi
#		cp /boot/vmlinuz /boot/efi/EFI/$BRAND/
#		cp /boot/initrd.gz /boot/efi/EFI/$BRAND/
#	fi
#fi
cat << EOF > /boot/grub/grub.cfg
set menu_color_normal=white/black
set menu_color_highlight=white/blue

terminal_output console
if [ x$feature_timeout_style = xy ] ; then
  set timeout_style=menu
  set timeout=10
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
  set timeout=10
fi
play 480 262 1 330 1 392 1 523 1

# No grub module for f2fs?
menuentry 'Boot Slint on ROOT_DEVICE' {
	echo "Starting Slint.  Please wait..."
	insmod part_gpt
	insmod part_msdos
	insmod btrfs
	insmod ext2
	insmod jfs
	insmod reiserfs
	insmod xfs
#   insmod f2fs
	search --fs-uuid --set=root $ROOTUUID
	linux /boot/vmlinuz root=UUID=$ROOTUUID vga=normal ro
	initrd /boot/initrd.gz
}
# Detect installed operating system and allow booting them from GRUB:
menuentry "Detect/boot any installed operating system" {
  configfile "/boot/grub/osdetect.cfg"
}
EOF
grub-install \
-d /usr/lib64/grub/x86_64-efi/ \
--efi-directory /boot/efi \
--boot-directory /boot \
--locale-directory=/usr/share/locale \
--target=x86_64-efi \
--no-nvram \
--removable \
--bootloader-id=slint  \
$ROOTDEVICE
