#!/bin/sh
#
#
############## modification record #########################################
#
# 2015-11-01 : Original script by rthpjm
#
# 2015-11-30 : Version 2, added missing fonts which reduces the number of entries in the
#                         log files SIGNIFICANTLY
#
# 2015-12-01 : Version 3, fixed a bug
#
# 2015-12-03 : Version 4, fixed a bug
#
# 2015-12-11 : Version 5, added the mods to the Sandbox exclusion to ease the use of
#                         Boot64
#
# 2015-12-15 : Version 6, added support for home-baked Fusion drives
#
# 2015-12-22 : Version 7, added an enormous amoount of error checking and clean up code
#
# 2015-12-31 : Version 8, now checks for the existance of
#                         /Applications/Install OS X El Capitan.app
#
# 2016-06-11 : Version 9, trying to make the build process faster...
#
# 2016-06-13 : Version 10, catch the condition where a user already has the
#                          disk images open...
#
# 2016-07-07 : Version 11, v10 caused problems on install media of 8GB (USB memory)
#                          because the total size had become around 12Gb, fixed
#
# 2016-09-20 : Version 12, adds better processing of the original copies, makes it
#                          easier to restore the correct original
# 
# 2016-10-25 : Version 13, adds more directives to the mdutil command in an attempt
#						   to really, really make sure indexing is turned off.
#							Also adds a check to see if the installer volume needs
#							to be re-blessed...
# 2017-02-06 : Version 14, New code to try an in-place install rather than using 
#							a USB memory key. Similar to Apple's method
# 
############################################################################
#
# createpikeinstallmedia is a script wrapper around the Apple supplied
# createinstallmedia commandline utility
#
# This script requires some accompanying files:
#			boot.efi
#			OSInstall.collection
#			pikify.pkg
#
# and the assumption that your copy of the El Capitan Installer is located in
# /Applications
#
# This script will create an install volume from which you can boot an unsupported
# Intel Macintosh and install El Capitan
# It has a unique feature in that the resulting El Capitan installation will be
# immediately bootable (pre-patched)
#
# MacRumors.com user Inspector42 modified one of my earlier scripts, I'm going to
# re-use one of his ideas in return
# (It only seems fair! :)
#
#
#***************************
# Absolutely NO warranty.
#***************************
#
# You must run this script as the root user
#    sudo -s
#
# OR
#
# sudo ./createpikeinstallmedia [path_to_volume]
#
#

versionNumber="14"

pikifyID=$$

savedDMGpath="/Users/$SUDO_USER/Library/Application Support/pikify3.1"
mountPointESD="$savedDMGpath/ESD"
mountPointBaseSystem="$savedDMGpath/BASE"
savedESDfile="$savedDMGpath/InstallESD-$pikifyID.dmg"
appESDfile="/Applications/Install OS X El Capitan.app/Contents/SharedSupport/InstallESD.dmg"
#
savedBaseSystemfile="/tmp/BaseSystem.dmg"
ESDBaseSystem="$mountPointESD/BaseSystem.dmg"
#
HDDInstallPath="$1/OS X Install Data"
HDDoriginalESD="$HDDInstallPath/InstallESD-orig.dmg"
HDDESD="$HDDInstallPath/InstallESD.dmg"
#
#
pikifyDataFile="/Applications/Install OS X El Capitan.app/Contents/SharedSupport/pikifyData.txt"
#
#
# Might need to undo the v10 changes
#
v10savedESDfile="/Applications/Install OS X El Capitan.app/Contents/SharedSupport/InstallESD-original.dmg"
#
#
# Might need to undo the v11 changes
#
v11savedESDfile="$savedDMGpath/InstallESD-original.dmg"

effectiveuid=$(id -u)

if [ "$effectiveuid" != "0" ]; then
	echo "\n\tYou need to be the root user to run this script"
	echo "\t\tTry one of the following:"
	echo "\t\t\tsudo -s"
	echo "\t\t\tsudo $0 $1"
	exit -1
fi

if [ "X$1" = "X" ]; then
	echo "\n\tUsage $0 /Volume/your_volume\n"
	exit -2
fi

if [ ! -d "$1" ]; then
        echo "\n\tCannot find the volume $1\n\n\n"
	exit -3	
fi

diskutil info "$1" > /dev/null

if [ $? -eq 1 ]; then
	echo "\n\tCannot find the volume $1\n\t\t$1 does not appear to be an available volume\n\n\n"
	exit -4
fi

if [ ! -f boot.efi ]; then
    echo "\n\tCannot find copy of Pike's boot.efi\n\t\tPlace a copy of Pike's boot.efi into the same folder as this script\n\n\n"
    exit -5
fi

if [ ! -f OSInstall.collection ]; then
    echo "\n\tCannot find copy of OSInstall.collection\n\t\tPlace a copy of OSInstall.collection into the same folder as this script\n\n\n"
    exit -6
fi

if [ ! -f pikify.pkg ]; then
    echo "\n\tCannot find copy of pikify.pkg\n\t\tPlace a copy of pikify.pkg into the same folder as this script\n\n\n"
    exit -7
fi

if [ ! -f "/Applications/Install OS X El Capitan.app/Contents/Resources/createinstallmedia" ]; then
    echo "\n\tCannot find copy of Apple's Install OS X El Capitan application in your Applications folder.\nPlease use the App Store to download a copy...\n\n\n"
    exit -8
fi

#
# Save the target installer device for later, to look up the mount point name
#
#devNode=$(diskutil info "$1" | awk '/Device Node/ {print $3}')

clear
echo "\n--------------------------- Version $versionNumber -----------------------------------------\n"
echo "\tPikify3.1 contains a script and other items to create install"
echo "\tmedia for Mac OS X El Capitan to be used on 32-bit-EFI based"
echo "\tMacPro 1,1 or 2,1 computers.\n"
echo "\tIt uses Apple's \"Install OS X El Capitan.app\" application"
echo "\twhich must be located in your /Applications folder\n"
echo "\n\tThere are a number of processing steps, please be patient"
echo "\tand let this script run through to completion.\n"
echo "\tIf all goes well, the script will end with\n"
echo "\t#########   DONE PIKIFYING   ###########\n"
echo "\tYou will be then asked if you are ready to reboot your machine"
echo "\n--------------------------------------------------------------------------------\n\n"

/bin/echo -n "Please wait ... "
for (( i=10; i > 0; i-- ))
do
	/usr/bin/printf "%02u\033[2D" "$i"
	sleep 1
done
echo "00"
clear

answer="not set"
while [ "$answer" == "not set" ]; do
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tSetup:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==	Would you like to use the black or grey boot screen background"
	echo "=="
	echo "================================================================================"
	/bin/echo -n "\n\n\tChoose [b]lack or [g]rey ? (default: g): "
	read answer
	case "${answer:0:1}" in
		g|G|'')		cp -f boot_grey_3_1.efi boot.efi
					;;
		b|B)		cp -f boot_black_3_1.efi boot.efi
					;;
		*)			answer="not set"
					errorMsg="Please answer 'b', 'g', or press the enter key for grey"
					;;
	esac
done



if [ -f "$pikifyDataFile" ]; then
	checkESD=$(awk -F= '/savedESDfile=/ {print $2}' "$pikifyDataFile")
	if [ -f "$checkESD" ]; then
		clear
		echo "\n================================================================================"
		echo "=="
		echo "==\tWarning:"
		echo "=="
		echo "================================================================================"
		echo "=="
		echo "==	You may have already run the pikify scripts previously"
		echo "=="
		echo "==		Resetting the Apple installer back to it's original condition"
		echo "=="
		echo "================================================================================"
		sleep 2
		rm "$appESDfile"
		mv "$checkESD" "$appESDfile"
		rm "$pikifyDataFile"
		clear
		echo "================================================================================"
		echo "=="
		echo "==\tInstall OS X El Capitan has been reset"
		echo "=="
		echo "================================================================================"
		sleep 2
	fi
fi

if [ -d "$mountPointBaseSystem/System" ];then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tWarning:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==	BaseSystem.dmg is already mounted, attempting to detach..."
	echo "=="
	echo "================================================================================"
	hdiutil detach -force "$mountPointBaseSystem" &>/dev/null
	bs=$?
	echo "=="
	echo "================================================================================"
	sleep 2
	if [ "$bs" -ne "0" ];then
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
        echo "=="
		echo "==	BaseSystem.dmg is already mounted, could not detach. Stopping..."
		echo "=="
		echo "================================================================================"
		exit -9
	fi
fi

if [ -f "$mountPointESD/BaseSystem.dmg" ];then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tWarning:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==	InstallESD.dmg is already mounted, attempting to detach..."
	echo "=="
	echo "================================================================================"
	hdiutil detach -force "$mountPointESD" &>/dev/null
	ie=$?
	sleep 2
	if [ "$ie" -ne "0" ];then
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
        echo "=="
		echo "==	InstallESD.dmg is already mounted, could not detach. Stopping..."
		echo "=="
		echo "================================================================================"
		exit -10
	fi
fi

rm1=0
if [ -f /tmp/BaseSystem.dmg ] || [ -f /tmp/BaseSystem.shadow ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tWarning:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tDetected the need to clean up for BaseSystem.dmg..."
	echo "=="
	echo "================================================================================"
	diskutil unmount /Volumes/OS\ X\ Base\ System &>/dev/null
	rm -f /tmp/BaseSystem.*
	rm1=$?
	sleep 2
fi

rm2=0
if [ -f /tmp/InstallESD.shadow ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tWarning:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tDetected the need to clean up InstallESD.dmg..."
	echo "=="
	echo "================================================================================"
	rm -f /tmp/InstallESD.shadow
	rm2=$?
	sleep 2
fi

if [ "$rm1" -ne "0" ] || [ "$rm2" -ne "0" ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tERROR:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tThere seems to be a number of residual files from a previously unsuccessful"
	echo "==\tbuild. The clean up attempt has failed. This usally means the disk images "
	echo "==\tare still mounted. Please try to unmount the image files before re-runnig,"
	echo "==\tor reboot this machine if you find that easier, then re-run"
	echo "=="
	echo "================================================================================"
	exit 100
fi

if [ ! -d "$savedDMGpath" ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tWorking, please be patient...."
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tCreating a folder to store the original Apple files ($savedDMGpath)"
	echo "=="
	echo "================================================================================"
	mkdir -p "$savedDMGpath"
	mkd=$?
	sleep 2
	if [ "$mkd" -ne "0" ]; then
        clear
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
        echo "=="
        echo "==\tCould not create the folder '$savedDMGpath'"
        echo "=="
		echo "================================================================================"
        exit 110
	fi
fi

if [ ! -d "$mountPointESD" ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tWorking, please be patient...."
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tCreating a folder to mount the InstallESD volume ($mountPointESD)"
	echo "=="
	echo "================================================================================"
	mkdir "$mountPointESD"
	mkd=$?
	sleep 2
	if [ "$mkd" -ne "0" ]; then
        clear
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
        echo "=="
        echo "==\tCould not create the folder '$mountPointESD'"
        echo "=="
		echo "================================================================================"
        echo "\n\n"
        exit 112
	fi
fi

if [ ! -d "$mountPointBaseSystem" ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tWorking, please be patient...."
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tCreating a folder to mount the original BaseSystem volume ($mountPointBaseSystem)"
	echo "=="
	echo "================================================================================"
	mkdir "$mountPointBaseSystem"
	mkd=$?
	sleep 2
	if [ "$mkd" -ne "0" ]; then
        clear
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
        echo "=="
        echo "==\tCould not create the folder '$mountPointBaseSystem'"
        echo "=="
		echo "================================================================================"
        exit 114
	fi
fi

if [ ! -d "$HDDInstallPath" ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tWorking, please be patient...."
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tCreating the installation folder ($HDDInstallPath)"
	echo "=="
	echo "================================================================================"
	mkdir "$HDDInstallPath"
	mkd=$?
	sleep 2
	if [ "$mkd" -ne "0" ]; then
        clear
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
        echo "=="
        echo "==\tCould not create the folder '$HDDInstallPath'"
        echo "=="
		echo "================================================================================"
        exit 115
	fi
fi

#clear
#echo "\n================================================================================"
#echo "=="
#echo "==\tWorking, please be patient...."
#echo "=="
#echo "================================================================================"
#echo "=="
#echo "==\tCopying the installer DMG..."
#echo "=="
#echo "================================================================================"
#rsync -aP "${appESDfile}" "$HDDoriginalESD"
#if [ "$?" -ne "0" ]; then
#		clear
#		echo "\n================================================================================"
#		echo "=="
#		echo "==\tERROR:"
#		echo "=="
#		echo "================================================================================"
#		echo "=="
#		echo "==\tA problem occurred when copying the InstallESD disk image"
#		echo "==\tThis is very odd and does not usually happen."
#		echo "=="
#		echo "================================================================================"
#		exit 400
#fi
#sleep 2


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tOpening the InstallESD disk image"
echo "=="
echo "================================================================================"
sleep 2
hdiutil attach -nobrowse -noverify -owners on -kernel -mountpoint "$mountPointESD" "$appESDfile" -shadow /tmp/InstallESD.shadow &> /dev/null
if [ "$?" -ne "0" ]; then
		clear
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
		echo "=="
		echo "==\tA problem occurred when opening the InstallESD disk image file as read/write"
		echo "==\tThis is very odd and does not usually happen."
		echo "==\tThe \"Install OS X El Capitan\" application in your Applications folder may"
		echo "==\tbe damaged"
		echo "=="
		echo "==\tYou may need to download a fresh copy from Apple..."
		echo "=="
		echo "================================================================================"
		echo "\n\n"
		exit 400
fi


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tTurning off spotlight indexing for InstallESD.dmg"
echo "=="
echo "================================================================================"
sleep 2
mdutil -i off -d "$mountPointESD" -V "$mountPointESD" &> /dev/null
mdutil -vs "$mountPointESD"


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tPiking the OSInstall collection"
echo "=="
echo "================================================================================"
sleep 2
cp OSInstall.collection "$mountPointESD/Packages"
ia1=$?
cat > "$mountPointESD/Packages/OSInstall.collection" <<_EOF_
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<string>/System/Installation/Packages/OSInstall.mpkg</string>
	<string>/System/Installation/Packages/OSInstall.mpkg</string>
	<string>/System/Installation/Packages/pikify.pkg</string>
</array>
</plist>
_EOF_
ia2=$?

cp pikify.pkg "$mountPointESD/Packages"
ia3=$?
mv "$mountPointESD/Packages/InstallableMachines.plist" "$mountPointESD/Packages/InstallableMachines.plist.apple"
ia4=$?
awk '
	{
		if ( $1 ~ /F42C88C8/ ) { 
			print "\t\t<string>Mac-F4208DC8</string>"; 
			print "\t\t<string>Mac-F4208DA9</string>"; 
			print; 
		}
		else if ( $1 ~ /MacPro3/ ) {
			print "\t\t<string>MacPro1,1</string>"; 
			print "\t\t<string>MacPro2,1</string>"; 
			print; 
		}
		else {
			print 
		}
	}' "$mountPointESD/Packages/InstallableMachines.plist.apple" > "$mountPointESD/Packages/InstallableMachines.plist"
ia5=$?
rm "$mountPointESD/Packages/InstallableMachines.plist.apple"
ia6=$?
if [ $ia1 -ne 0 ] || [ $ia2 -ne 0 ] || [ $ia3 -ne 0 ] || [ $ia4 -ne 0 ] || [ $ia5 -ne 0 ] || [ $ia6 -ne 0 ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tERROR:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tA problem occurred when modifying the OSInstall options"
	echo "==\tThis is very odd and does not usually happen. I don't know what to do!"
	echo "==\tClose all other windows (especially terminal windows) and try again"
	echo "=="
	echo "================================================================================"
	echo "\n\n"
	exit 500
fi


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tSaving the original copy of the BaseSystem.dmg...."
echo "=="
echo "================================================================================"
sleep 2
rsync -aP "${ESDBaseSystem}" "$savedBaseSystemfile"
if [ "$?" -ne "0" ]; then
		clear
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
		echo "=="
		echo "==\tA problem occurred when renaming the BaseSystem disk image "
		echo "==\tThis is very odd and does not usually happen."
		echo "==\tCheck that you have sufficient disk space left on your system to complete"
		echo "==\tthe copy, you need approximately 465MB or more of free disk space"
		echo "=="
		echo "================================================================================"
		echo "\n\n"
		exit 600
fi
rm -f "${ESDBaseSystem}"


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tOpening the BaseSystem disk image"
echo "=="
echo "================================================================================"
sleep 2
hdiutil attach -nobrowse -noverify -owners on -kernel -mountpoint "$mountPointBaseSystem" "$savedBaseSystemfile" -shadow /tmp/BaseSystem.shadow &>/dev/null
if [ "$?" -ne "0" ]; then
		clear
		echo "\n================================================================================"
		echo "=="
		echo "==\tERROR:"
		echo "=="
		echo "================================================================================"
		echo "=="
		echo "==\tA problem occurred when mounting the BaseSytem disk image to be read/write"
		echo "==\tThis is very odd and does not usually happen."
		echo "==\tClose all other windows (especially terminal windows) and try again"
		echo "=="
		echo "================================================================================"
		exit 800
fi


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tTurning off spotlight indexing for BaseSystem.dmg"
echo "=="
echo "================================================================================"
sleep 2
mdutil -i off -d "$mountPointBaseSystem" -V "$mountPointBaseSystem" &>/dev/null
mdutil -vs "$mountPointBaseSystem"


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tPiking the Base System"
echo "=="
echo "================================================================================"
sleep 2
chflags nouchg "$mountPointBaseSystem/System/Library/CoreServices/boot.efi"
bs1=$?
cp boot.efi "$mountPointBaseSystem/System/Library/CoreServices/boot.efi"
bs2=$?
cp bootbase.efi "$mountPointBaseSystem/System/Library/CoreServices/bootbase.efi"
bs3=$?
cp boot.efi "$mountPointBaseSystem/usr/standalone/i386/boot.efi"
bs4=$?
if [ $bs1 -ne 0 ] || [ $bs2 -ne 0 ] || [ $bs3 -ne 0 ] || [ $bs4 -ne 0 ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tERROR:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tA problem occurred when replacing the boot.efi files in the BaseSystem image"
	echo "==\tThis is very odd and does not usually happen. I don't know what to do!"
	echo "==\tClose all other windows (especially terminal windows) and try again"
	echo "=="
	echo "================================================================================"
	exit 900
fi


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tAdding the missing fonts..."
echo "=="
echo "================================================================================"
sleep 2
missingFontsPath=$(pwd)
(cd "$mountPointBaseSystem/System/Library/Fonts"; tar zxf "$missingFontsPath/missing-fonts.tgz" )
fnts=$?
if [ $fnts -ne 0 ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tERROR:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tA problem occurred when Fixing the missing fonts from Apple's tools"
	echo "==\tThis is very odd and does not usually happen."
	echo "==\tHave you accidentally moved or deleted a file from $missingFontsPath ?..."
	echo "=="
	echo "================================================================================"
	echo "\n\n"
	exit 1000
fi


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tAmmending PlatformSupport.plist file"
echo "=="
echo "================================================================================"
sleep 2
mv "$mountPointBaseSystem/System/Library/CoreServices/PlatformSupport.plist" "$mountPointBaseSystem/System/Library/CoreServices/PlatformSupport.plist.apple"
ps1=$?
awk '
		{
			if ( $1 ~ /F42C88C8/ ) { 
				print "\t\t<string>Mac-F4208DC8</string>"; 
				print "\t\t<string>Mac-F4208DA9</string>"; 
				print; 
			}
			else if ( $1 ~ /MacPro3/ ) {
				print "\t\t<string>MacPro1,1</string>"; 
				print "\t\t<string>MacPro2,1</string>"; 
				print; 
			}
			else {
				print 
			}
		}' "$mountPointBaseSystem/System/Library/CoreServices/PlatformSupport.plist.apple" > "$mountPointBaseSystem/System/Library/CoreServices/PlatformSupport.plist"
rm "$mountPointBaseSystem/System/Library/CoreServices/PlatformSupport.plist.apple"


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tExtracting Boot Bits from Inner DMG:"
echo "=="
echo "================================================================================"
sleep 2


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tCopying prelinkedkernel"
echo "=="
echo "================================================================================"
sleep 2
cp "$mountPointBaseSystem/System/Library/PrelinkedKernels/prelinkedkernel" "$1/OS X Install Data"


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tCopying boot.efi"
echo "=="
echo "================================================================================"
sleep 2
cp "$mountPointBaseSystem/System/Library/CoreServices/boot.efi" "$1/OS X Install Data"


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tCopying PlatformSupport.plist"
echo "=="
echo "================================================================================"
sleep 2
cp "$mountPointBaseSystem/System/Library/CoreServices/PlatformSupport.plist" "$1/OS X Install Data"


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tGenerating the com.apple.Boot.plist file"
echo "=="
echo "================================================================================"
sleep 2
cat > "$1/OS X Install Data/com.apple.Boot.plist" <<_EOFBoot_
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Kernel Cache</key>
        <string>/OS X Install Data/prelinkedkernel</string>
        <key>Kernel Flags</key>
        <string>container-dmg=file:///OS%20X%20Install%20Data/InstallESD.dmg root-dmg=file:///BaseSystem.dmg</string>
</dict>
</plist>
_EOFBoot_


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tClosing the BaseSystem disk image"
echo "=="
echo "================================================================================"
sleep 2
hdiutil detach "$mountPointBaseSystem" -force &>/dev/null
if [ "$?" -ne "0" ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tERROR:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tA problem occurred when unmounting the BaseSystem disk image"
	echo "==\tThis is very odd and does not usually happen."
	echo "==\tClose all other windows (especially terminal windows) and try again"
	echo "=="
	echo "================================================================================"
	echo "\n\n"
	exit 1100
fi


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tPutting the modified BaseSystem back into the InstallESD disk image"
echo "=="
echo "================================================================================"
sleep 2
hdiutil convert -format UDZO -imagekey zlib-level=9 "$savedBaseSystemfile" -shadow /tmp/BaseSystem.shadow -o "$ESDBaseSystem"
bsn1=$?
rm -f "$savedBaseSystemfile" /tmp/BaseSystem.shadow
bsn2=$?
if [ $bsn1 -ne 0 ] || [ $bsn2 -ne 0 ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tERROR:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tA problem occurred when putting the BaseSystem image back into the"
	echo "==\tinstaller image."
	echo "==\tThis is very odd and does not usually happen. I don't know what to do!"
	echo "==\tClose all other windows (especially terminal windows) and try again"
	echo "=="
	echo "================================================================================"
	exit 1200
fi


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tClosing the InstallESD disk image"
echo "=="
echo "================================================================================"
sleep 2
hdiutil detach "$mountPointESD" -force &> /dev/null
if [ "$?" -ne "0" ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tERROR:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tA problem occurred when unmounting the InstallESD disk image"
	echo "==\tThis is very odd and does not usually happen."
	echo "==\tClose all other windows (especially terminal windows) and try again"
	echo "=="
	echo "================================================================================"
	exit 1300
fi


clear
echo "\n================================================================================"
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tRebuild the InstallESD"
echo "=="
echo "================================================================================"
sleep 2
if [ -f "$HDDESD" ]; then
	rm -f "$HDDESD"
fi
hdiutil convert -format UDZO -imagekey zlib-level=9 "$appESDfile" -shadow /tmp/InstallESD.shadow -o "$HDDESD"
iesd2=$?
rm -f /tmp/InstallESD.shadow
iesd3=$?
if [ $iesd2 -ne 0 ] || [ $iesd3 -ne 0 ]; then
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\tERROR:"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "==\tA problem occurred when putting the InstallESD image back into the"
	echo "==\tinstaller volume"
	echo "==\tThis is very odd and does not usually happen. I don't know what to do!"
	echo "==\tClose all other windows (especially terminal windows) and try again"
	echo "=="
	echo "================================================================================"
	exit 1400
fi


clear
echo "\n================================================================================="
echo "=="
echo "==\tWorking, please be patient...."
echo "=="
echo "================================================================================"
echo "=="
echo "==\tBlessing $1/OS X Install Data"
echo "=="
echo "================================================================================"
sleep 2
/usr/sbin/bless -setBoot -folder "$1/OS X Install Data" -bootefi "$1/OS X Install Data/boot.efi" -options config="\OS X Install Data\com.apple.Boot" -label OS X Installer







answer="not set"
errorMsg=""
while [ "$answer" == "not set" ]
do
	clear
	echo "\n================================================================================"
	echo "=="
	echo "==\t\t#########   DONE PIKIFYING   ###########"
	echo "=="
	echo "================================================================================"
	echo "=="
	echo "== $errorMsg"
	echo "=="
	/bin/echo -n "== Reboot the system now to start the installation process? (default: no): "
	read answer
	case "${answer:0:1}" in
		n|N|'')		;;
		y|Y)		reboot
					;;
		*)			answer="not set"
					errorMsg="Please answer 'y', 'n', or press the enter key for no"
					;;
	esac
done


exit 0

















