#!/bin/sh
################################## CHANGE ME ##################################
TargetDriveName="/Volumes/Macintosh HD"
################################## CHANGE ME ##################################

unset -v IFS
IFS=""
default="default "

if (( $# != 0 )); then
	TargetDriveName="/Volumes/$*"
	default=""
fi

if [[ -f "boot.efi" ]]; then
	xattr -d com.apple.quarantine boot.efi &>/dev/null
else
	echo
	echo "    No boot.efi found, please place it in the same directory as this script and re-run."
	echo
	exit
fi	

if [[ -d "$TargetDriveName" ]]; then
	echo
	echo "    Using ${default}volume '$(basename $TargetDriveName)':"
	echo
else
	echo
	echo "    Please supply a volume to use:"
	echo
	echo "    $0 VolumeToUse"
	echo
	echo "    or name a volume '$(basename $TargetDriveName)'."
	echo
	exit
fi

printf "    Looking for companion 'Recovery HD'....................."
RecoveryDevice=$(diskutil info "$TargetDriveName" | grep 'Device Node' | cut -d : -f 2 | sed 's/^ *//g' | sed 's/ *2$/3/g') &>/dev/null
TargetRecoveryDrive=/Volumes/TemporaryRecoveryVolumeMount

if [[ "$RecoveryDevice" ]]; then
	mkdir -p $TargetRecoveryDrive
	diskutil mount -mountPoint $TargetRecoveryDrive $RecoveryDevice &>/dev/null
	printf "[..FOUND...]\n\n"
else
	printf "[.NOTFOUND.]\n"
fi

if [[ -f "$TargetDriveName/.IABootFiles/boot.efi" ]]; then
	chflags nouchg "$TargetDriveName/.IABootFiles"
	cp boot.efi "$TargetDriveName/.IABootFiles/boot.efi"
	if [[ $? -eq 0 ]]; then
		echo "    /.IABootFiles/boot.efi..................................[*REPLACED*]"
	else
		echo "    /.IABootFiles/boot.efi..................................[¡¡FAILED!!]"
	fi
else
		echo "    /.IABootFiles/boot.efi..................................[.NOTFOUND.]"
fi

if [[ -f "$TargetDriveName/OS X Install Data/boot.efi" ]]; then
	chflags nouchg "$TargetDriveName/OS X Install Data/boot.efi" &>/dev/null
	cp boot.efi "$TargetDriveName/OS X Install Data/boot.efi" &>/dev/null
	if [[ $? -eq 0 ]]; then
		echo "    /OS X Install Data/boot.efi.............................[*REPLACED*]"
	else
		echo "    /OS X Install Data/boot.efi.............................[¡¡FAILED!!]"
	fi
else
		echo "    /OS X Install Data/boot.efi.............................[.NOTFOUND.]"
fi

if [[ -f "$TargetDriveName/usr/standalone/i386/boot.efi" ]]; then
	chflags nouchg "$TargetDriveName/usr/standalone/i386/boot.efi" &>/dev/null
	cp boot.efi "$TargetDriveName/usr/standalone/i386/boot.efi" &>/dev/null
	if [[ $? -eq 0 ]]; then
		echo "    /usr/standalone/i386/boot.efi...........................[*REPLACED*]"
	else
		echo "    /usr/standalone/i386/boot.efi...........................[¡¡FAILED!!]"
	fi
else
		echo "    /usr/standalone/i386/boot.efi...........................[.NOTFOUND.]"
fi

if [[ -f "$TargetDriveName/System/Library/CoreServices/boot.efi" ]]; then
	chflags nouchg "$TargetDriveName/System/Library/CoreServices/boot.efi" &>/dev/null
	cp boot.efi "$TargetDriveName/System/Library/CoreServices/boot.efi" &>/dev/null
	if [[ $? -eq 0 ]]; then
		echo "    /System/Library/CoreServices/boot.efi...................[*REPLACED*]"
	else
		echo "    /System/Library/CoreServices/boot.efi...................[¡¡FAILED!!]"
	fi
else
		echo "    /System/Library/CoreServices/boot.efi...................[.NOTFOUND.]"
fi

if [[ -f "$TargetDriveName/System/Library/CoreServices/bootbase.efi" ]]; then
	chflags nouchg "$TargetDriveName/System/Library/CoreServices/boot.efi" &>/dev/null
	cp boot.efi "$TargetDriveName/System/Library/CoreServices/boot.efi" &>/dev/null
	if [[ $? -eq 0 ]]; then
		echo "    /System/Library/CoreServices/bootbase.efi...............[*REPLACED*]"
	else
		echo "    /System/Library/CoreServices/bootbase.efi...............[¡¡FAILED!!]"
	fi
else
		echo "    /System/Library/CoreServices/bootbase.efi...............[.NOTFOUND.]"
fi

if [[ -f "$TargetRecoveryDrive/com.apple.recovery.boot/boot.efi" ]]; then
	chflags nouchg "$TargetRecoveryDrive/com.apple.recovery.boot/boot.efi" &>/dev/null
	cp boot.efi "$TargetRecoveryDrive/com.apple.recovery.boot/boot.efi" &>/dev/null
	if [[ $? -eq 0 ]]; then
		echo "    /Recovery HD/com.apple.recovery.boot/boot.efi...........[*REPLACED*]"
	else
		echo "    /Recovery HD/com.apple.recovery.boot/boot.efi...........[¡¡FAILED!!]"
	fi
else
		echo "    /Recovery HD/com.apple.recovery.boot/boot.efi...........[.NOTFOUND.]"
fi

echo
echo "    [.NOTFOUND.] just indicates the file is not present on the target."
echo
echo "    If you see any [¡¡FAILED!!] above, try re-running with sudo:"
echo
echo "    sudo ./${0#./}"
echo

diskutil unmount $RecoveryDevice &>/dev/null
unset -v IFS
exit
