#!/bin/sh
#EFI mount generator for ROSA Linux
#Aleksandr Kazantcev 2013
#Alexander Lakhin 2015
#ROSA Lab

if [ $# -gt 0 ]; then
	systemd_unit_path=$1
else
	systemd_unit_path=/run/systemd/generator/
fi

#Check if EFI present and running system is not live
if [ -d /sys/firmware/efi ] && [ ! -d /run/initramfs/live ]; then

while : ; do
	for disk in $(lsblk -S -n -o NAME); do
		EFI_PART_NO="$(gdisk -l /dev/$disk | grep EF00 | awk '{print $1}')"
		[ -n "$EFI_PART_NO" ] && EFI_PART="/dev/$disk$EFI_PART_NO" && break;
	done
	if [ -n "$EFI_PART" ]; then
	# Try to find EFI by gdisk CODE EF00
		EFI_UUID=$(blkid -s PARTUUID $EFI_PART | sed 's/.*="//;s/".*//')
		EFI_BY_DEV='by-partuuid'
		[ ! -z $EFI_UUID ] && [ -e /dev/disk/$EFI_BY_DEV/$EFI_UUID ] && break;

		EFI_UUID=$(blkid -s UUID $EFI_PART | sed 's/.*="//;s/".*//')
		EFI_BY_DEV='by-uuid'
		[ ! -z $EFI_UUID ] && [ -e /dev/disk/$EFI_BY_DEV/$EFI_UUID ] && break;
	fi

	#Try to determine by PARTLABEL=EFI system partition and UUID
	EFI_UUID=$(lsblk -Po UUID,PARTUUID,PARTLABEL,FSTYPE | grep vfat | grep 'PARTLABEL="EFI system partition"' | sed -e 's/^UUID="//;s/".*//')
	EFI_BY_DEV='by-uuid'
	[ ! -z $EFI_UUID ] && [ -e /dev/disk/$EFI_BY_DEV/$EFI_UUID ] && break;

	#Try to determine by PARTLABEL=EF and UUID
	EFI_UUID=$(lsblk -Po UUID,PARTUUID,PARTLABEL,FSTYPE | grep vfat | grep 'PARTLABEL="EF"' | sed -e 's/^UUID="//;s/".*//')
	EFI_BY_DEV='by-uuid'
	[ ! -z $EFI_UUID ] && [ -e /dev/disk/$EFI_BY_DEV/$EFI_UUID ] && break;

	#Try to determine by PARTLABEL=EFI system partition and PARTUUID
	EFI_UUID=$(lsblk -Po UUID,PARTUUID,PARTLABEL,FSTYPE | grep vfat | grep 'PARTLABEL="EFI system partition"' | sed -e 's/.*PARTUUID="//;s/".*//')
	EFI_BY_DEV='by-partuuid'
	[ ! -z $EFI_UUID ] && [ -e /dev/disk/$EFI_BY_DEV/$EFI_UUID ] && break;

	#Try to determine by PARTLABEL=EF and PARTUUID
	EFI_UUID=$(lsblk -Po UUID,PARTUUID,PARTLABEL,FSTYPE | grep vfat | grep 'PARTLABEL="EF"' | sed -e 's/.*PARTUUID="//;s/".*//')
	EFI_BY_DEV='by-partuuid'
	[ ! -z $EFI_UUID ] && [ -e /dev/disk/$EFI_BY_DEV/$EFI_UUID ] && break;

	echo "systemd-efi-boot-generator failed to determine ESP UUID" > /dev/kmsg
	exit 0
done

echo "# Automatially generated by systemd-efi-boot-generator" > $systemd_unit_path/boot-efi.mount
echo "[Unit]" >> $systemd_unit_path/boot-efi.mount
echo "Description=EFI System Partition" >> $systemd_unit_path/boot-efi.mount
echo "[Mount]" >> $systemd_unit_path/boot-efi.mount
echo "Where=/boot/efi" >> $systemd_unit_path/boot-efi.mount
echo "What=/dev/disk/$EFI_BY_DEV/$EFI_UUID" >> $systemd_unit_path/boot-efi.mount
echo "Options=umask=0077,noauto" >> $systemd_unit_path/boot-efi.mount

echo "# Automatially generated by systemd-efi-boot-generator" > $systemd_unit_path/boot-efi.automount
echo "[Unit]" >> $systemd_unit_path/boot-efi.automount
echo "Description=EFI System Partition Automount" >> $systemd_unit_path/boot-efi.automount
echo "[Automount]" >> $systemd_unit_path/boot-efi.automount
echo "Where=/boot/efi" >> $systemd_unit_path/boot-efi.automount

if [ ! -d $systemd_unit_path/local-fs.target.wants ]; then
	mkdir -p $systemd_unit_path/local-fs.target.wants
fi

ln -sf $systemd_unit_path/boot-efi.automount $systemd_unit_path/local-fs.target.wants/boot-efi.automount

fi

exit 0
