#!/bin/bash
#
# Installer script for mounting EFI partition on destination volume
#
#	Usage:
#
#		mount-EFI "Destination volume name in double quotes"
#
#	Note:
#
#		Must be run with sudo privileges
#
#	Return Codes:
#
#		0 - EFI partition for destination volume is present, mounted and contains Clover config.plist
#		1 - EFI partition for destination volume does not contain a valid Clover config.plist
#		2 - Mount of EFI partition failed
#		3 - destination volume is not formatted as GPT
#
# 	Copyright (C) tonymacx86 LLC

DESTINATION_VOLUME=$1

if [ "${DESTINATION_VOLUME}" == "/" ]; then
	DESTINATION_VOLUME=$(mount | grep " / " | awk '{print $1}')
fi

#
#	Function to test for Clover config.plist
#

plist () {
	if $( plutil -s /Volumes/EFI/EFI/CLOVER/config.plist 2>&1 >/dev/null ) ; then
		exit 0
	else
		echo -e "\n!!! EFI partition for /Volumes/${DESTINATION_VOLUME} does not contain a valid Clover config.plist !!!\n\n"
		exit 1
	fi
}

#
#	Check destination volume to see if it's a GPT disk
#

if [ $(diskutil list "${DESTINATION_VOLUME}" | grep -c FDisk_partition_scheme) -eq 1 ]; then
	echo -e "\n!!! /Volumes/${DESTINATION_VOLUME} is not formatted as GPT !!!"
	exit 3
fi

#
#	If /Volumes/ESP/EFI/CLOVER/config.plist exists than Clover was just installed or updated
#

if [ -s "/Volumes/ESP/EFI/CLOVER/config.plist" ]; then	
	exit 0
fi

#
#	If there is more than one EFI partition mounted unmount all except for 1
#

if [ $(mount | grep -c EFI) -gt 1 ]; then
	COUNT=$(mount | grep -c EFI)
	until [ $COUNT -eq 1 ]; do
		let COUNT-=1
		diskutil quiet umount "/Volumes/EFI $COUNT"
		done
fi

#
#	Check for mounted EFI partition and see if it's for destination volume
#	If the mounted EFI partition is not for destination volume, unmount it
#

if [ $(mount | grep -c EFI) -eq 1 ]; then
	EFI_PARTITION=$(mount | grep EFI | grep -o /dev/disk.s.)
	if [ $(mount | grep -c "${DESTINATION_VOLUME}") -eq 0 ]; then
		if [ $(diskutil list / | grep -c "Physical Store") -eq 1 ]; then		
			DESTINATION_EFI=$(diskutil list / | grep 'Physical Store' | awk '/disk?s?/{ print "/dev/"$3; }' | sed 's/s2/s1/')
		else
			DESTINATION_EFI=$(mount | grep " / " | awk '{gsub(/s2/,"s1"); print$1}')
		fi
	else
		if [ $(diskutil list "${DESTINATION_VOLUME}" | grep -c "Physical Store") -eq 1 ]; then			
			DESTINATION_EFI=$(diskutil list "${DESTINATION_VOLUME}" | grep 'Physical Store' | awk '/disk?s?/{ print "/dev/"$3; }' | sed 's/s2/s1/')
		else
			DESTINATION_EFI=$(mount | awk -v pattern="${DESTINATION_VOLUME}" '$0 ~ pattern {sub(/s2 /, "s1 "); print$1}')
		fi
	fi
	
	if [ ${EFI_PARTITION} == ${DESTINATION_EFI} ]; then
		plist ${EFI_PARTITION}
	else
		diskutil quiet umount ${EFI_PARTITION}
	fi
fi

#
#	Find EFI partition for destination volume
#

if [ $(mount | grep -c "${DESTINATION_VOLUME}") -eq 0 ]; then
	if [ $(diskutil list / | grep -c "Physical Store") -eq 1 ]; then
		DESTINATION_EFI=$(diskutil list / | grep 'Physical Store' | awk '/disk?s?/{ print "/dev/"$3; }' | sed 's/s2/s1/')
	else
		DESTINATION_EFI=$((mount | grep " / " | awk '{gsub(/s2/,"s1"); print$1}'))
	fi
else
	if [ $(diskutil list "${DESTINATION_VOLUME}" | grep -c "Physical Store") -eq 1 ]; then
		DESTINATION_EFI=$(diskutil list "${DESTINATION_VOLUME}" | grep 'Physical Store' | awk '/disk?s?/{ print "/dev/"$3; }' | sed 's/s2/s1/')
	else
		DESTINATION_EFI=$(mount | awk -v pattern="${DESTINATION_VOLUME}" '$0 ~ pattern {sub(/s2 /, "s1 "); print$1}')
	fi	
fi

#
#	Mount EFI partition for destination volume and check for Clover config.plist
#

if diskutil quiet mount ${DESTINATION_EFI} ; then
	 plist ${EFI_PARTITION} 
else
	echo -e "\n!!!  Mount of EFI partition on /Volumes/${DESTINATION_VOLUME} failed !!!\n\n"
	exit 2
fi
