#!/bin/sh
# We will install GRUB in the parent device of the root partition, so
# that the system stays bootable if moved, at least if the firmware of
# the machine where this devices lands is set to boot from it first.
export TEXTDOMAIN=slint
. gettext.sh
if [ ! $(id -u) -eq 0 ]; then
	gettext "Only root may run this script."
	exit
fi
if [ "$(df|grep /mnt$)" = "" ]; then
	echo "The root partition (/) of the newly installed system should be"
	echo "mounted as /mnt before running this script."
	exit
fi
export ROOTDELAY=5
TMP=$(mktemp)
dialog \
--title "$(gettext "INCLUDE EXTRA PARAMETERS IN THE BOOT COMMAND LINE?")" \
--inputbox \
"$(gettext "You might need extra parameters to be included in the boot command \
line of the installed system. If you typed parameters before booting \
the Slint installer, you will probably want to enter the same ones here. \
If you don't need any extra parameter to be included, just press ENTER \
to continue.
")" 12 72 2> $TMP
export KERNELPARAMS=$(<$TMP)
rm $TMP
USB_HOST_AND_STORAGE="mmc \
mmc_block \
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"
USB_KBD="uhci-hcd \
usbhid \
hid-generic"
btrfs="btrfs"
ext2="mbcache \
ext2"
ext3="jbd2 \
mbcache \
crc32c-intel \
ext4"
ext4="jbd2 \
mbcache \
crc32c-intel \
ext4"
# f2fs="f2fs"
jfs="jfs"
reiserfs="reiserfs"
xfs="xfs"
MODULES="$USB_HOST_AND_STORAGE $USB_KBD"
ROOTPART=$(lsblk -l -o name,mountpoint| grep /mnt$|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:]]*//")
BRAND=$(grep ^NAME /mnt/etc/os-release|sed "s/.*=//")
KERNELVERSION=$(ls -1tr /mnt/boot/vmlinuz-generic-*|sed -n '/$/s,.*-,,p')
EFIINSTALL=n
check_efi() {
	# We will install grub for UEFI booting if and only if an ESP on the
	# same drive as the root partition is mounted on /mnt/boot/efi
	# If no partition is mounted as /boot/efi, bail out.
	PARTNAME=$(lsblk -l -o name,mountpoint |grep /mnt/boot/efi|sed "s/[[:space:]]\{1,\}.*//")
	if [ "$PARTNAME" = "" ]; then
		return
	fi
	# If the partition mounted as /boot/efi is not an ESP, bail out.
	# The UEFI specification states that an ESP has a GUID of
	# C12A7328-F81F-11D2-BA4B-00A0C93EC93B in case of a GPT layout.
	# In case of a DOS layout instead, an ESP should have an OS type of
	# 0xEF. lsblk writes these values in the same field PARTTYPE.
	ESPPARTTYPE=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
	OSTYPE=0xEF
	PARTTYPE=$(lsblk -l -o parttype,name|grep $PARTNAME|sed "s/[[:space:]]\{1,\}.*//")
	if [ "$(echo $PARTTYPE|grep -i $ESPPARTTYPE)" = "" ] && \
	[ "$(echo $PARTTYPE|grep -i $OSTYPE)" = "" ]; then
		return
	fi
	# If the file system of the partition mounted as /efi/boot is not
	# VFAT, bail out.
	PARTFSTYPE=$(lsblk -l -o fstype,name|grep $PARTNAME|sed  "s/[[:space:]]\{1,\}.*//")
	if [ "(echo $PARTFSTYPE| grep -i  vfat)" = "" ]; then
		return
	fi
	# The ESP mounted as /mnt/boot/efi should be on the same device as 
	# the target root partition.
	ESPDEVICE=/dev/$(lsblk -l -o pkname,name|grep ${PARTNAME}$|sed "s/[[:space:]]\{1,\}.*//")
	if [ ! "$ESPDEVICE" = "$ROOTDEVICE" ]; then
		return
	fi
	EFIINSTALL=y
}
check_efi
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
export MODULES=$(printf "$MODULES $FS"|sed "s/ /:/g")
lsblk -l -o uuid,type,name|grep part| \
sed "s, *\([^ ]*\) *[^ ]*  *\([^ ]*\).*,s;/dev/\2;UUID=\1;," > /mnt/tmp/part_list
export SLINTVERSION=$(</mnt/etc/slint-version)

cat << EOF > /mnt/boot/grub/grub.cfg
# This grub.cfg is part of a portable Slint.
# Do NOT remove or modify the line above, as it indicates to the script
# update-kernel that it should install a new initrd but run the script
# update-portable instead of update-grub after a kernel upgrade.

set menu_color_normal=white/black
set menu_color_highlight=white/blue

terminal_output console
set timeout=10
play 480 262 1 330 1 392 1 523 1

menuentry 'Boot $SLINTVERSION kernel $KERNELVERSION' {
	echo "Starting $SLINTVERSION kernel $KERNELVERSION.  Please wait..."
	insmod part_gpt
	insmod part_msdos
	insmod btrfs
	insmod f2fs
	insmod ext2
	insmod jfs
	insmod reiserfs
	insmod xfs
	insmod all_video
	search --fs-uuid --set=root $ROOTUUID
	linux /boot/vmlinuz-generic-$KERNELVERSION root=UUID=$ROOTUUID vga=normal ro $KERNELPARAMS
	initrd /boot/initrd.gz
}
EOF
cat << EOF > /mnt/bin/post_install
#!/bin/sh
if [ ! -f /etc/fstab ]; then
	gettext "I can't find /etc/fstab in the newly installed system.
Please complete its installation before running this script."
	echo
	exit
fi
mkinitrd -c -k $KERNELVERSION -f $FSTYPE -r UUID=$ROOTUUID -m $MODULES -w $ROOTDELAY -u -o /boot/initrd.gz 1>/dev/null 2>/dev/null
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, the generic one should already be there.
if [ -f /boot/vmlinuz-huge-* ]; then
	removepkg kernel-huge 
	( cd /boot
	rm -f System.map config config-generic vmlinuz vmlinuz-generic
	)
fi

EOF
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
mount --bind /dev /mnt/dev
chmod 755 /mnt/bin/post_install
chroot /mnt /bin/post_install

# We install in both Legacy and EFI modes if possible.
echo ROOTDEVICE=$ROOTDEVICE
chroot /mnt grub-install --target=i386-pc $ROOTDEVICE
if [ "$EFIINSTALL" = "y" ]; then
	chroot /mnt grub-install --target=x86_64-efi --no-nvram --removable $ROOTDEVICE
fi
rm /mnt/bin/post_install /mnt/tmp/part_list
umount /mnt/proc
umount /mnt/sys
umount /mnt/dev
