#!/bin/bash

echo "==============================================="
echo "Check Proceed: Can the script continue?"
echo "***************************************"

# Checks the selected volume is present and the disk is partitioned.

# Receives targetVolume: Volume to install to (Will be '/Volumes/EFI' if EFI install)
# Receives targetDevice: Stores device number, for example /dev/disk2s1.
# Receives installerVolume: Volume to write the installer log to.
# Receives scriptDir: The location of the main script dir.

if [ "$#" -eq 4 ]; then
	targetVolume="$1"
	targetDevice="$2"
	installerVolume="$3"
	scriptDir="$4"
	echo "DEBUG: passed argument for targetVolume = $targetVolume"
	echo "DEBUG: passed argument for targetDevice = $targetDevice"
	echo "DEBUG: passed argument for installerVolume = $installerVolume"
	echo "DEBUG: passed argument for scriptDir = $scriptDir"
else
	echo "Error - wrong number of values passed"
	exit 9
fi

if [ -z "$targetVolume" ]; then
	echo "*** Cannot find the volume. Exiting."
	"$scriptDir"InstallLog.sh "${installerVolume}" "FAIL: Cannot file the volume: $targetVolume."
	exit 1
else
	echo "Confirming target volume exists"
fi

if [ "$targetDevice" = "$targetDevice#*disk*s" ]; then
	echo "*** ERROR Volume does not use slices. Exiting."
	"$scriptDir"InstallLog.sh "${installerVolume}" "FAIL: $targetVolume doesn't use slices."
	exit 1		
else
	echo "Confirming target device uses slices"
fi

# Add check for installing to a 'small' HFS device like a
# 1GB USB flash drive which won't have an EFI System Partition.

if [ "$targetVolume" = "/Volumes/EFI" ]; then
	existESP=$( df | grep $targetDevice | awk {'print $6'} )
	if [ ! "$existESP" = "/Volumes/EFI" ]; then
		echo "*** The selected volume doesn't have an EFI System Partition. Exiting."
		"$scriptDir"InstallLog.sh "${installerVolume}" "FAIL: $installerVolume doesn't have an EFI System Partition."
		exit 1
	fi
fi

echo "-----------------------------------------------"
echo ""

"$scriptDir"InstallLog.sh "${installerVolume}" "CheckProceed: PASS"

exit 0