#!/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
# 
############################################################################
#
# 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.
# This script will overwrite one of your Volumes
#
# You must run this script as the root user
#    sudo -s
#
# OR
#
# sudo ./createpikeinstallmedia [path_to_volume]
#
#

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"
	echo "\n\t\tWARNING: the volume you specify will be erased!\n\t\tMake sure you provide the correct path to the volume...\n\n\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

clear

echo "\n\n\n------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n"
echo "\tPikify3.1 contains a script and other items to create install media for Mac OS X El Capitan to be used on 32-bit EFI based Macintosh computers"
echo "\tIt uses Apple's \"Install OS X El Capitan.app\" application which must be located in your /Applications folder"
echo "\n\tThere are a number of processing steps, please be patient and let this script run through to completion"
echo "\tA small number of users have been too eager to get started and pulled their installer volume out of the USB socket too early"
echo "\n\tThe script will end with #########   DONE PIKIFYING   ########### if all goes well"
echo "\n\n------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n"

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

rm2=0
if [ -f /tmp/InstallESD.shadow ]; then
	echo "\t\tDetected the need to clean up InstallESD.dmg..."
	diskutil unmount /Volumes/OS\ X\ Install\ ESD &>/dev/null
	rm -f /tmp/InstallESD.shadow
	rm2=$?
fi

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


echo "\nRunning Apple's createinstallmedia tool..."
/Applications/Install\ OS\ X\ El\ Capitan.app/Contents/Resources/createinstallmedia --volume "$1" --applicationpath /Applications/Install\ OS\ X\ El\ Capitan.app --nointeraction
cim=$?
if [ "$cim" -ne "0" ]; then
	clear
	echo "\n\n================================================================================================================================="
	echo "=="
	echo "==\tERROR"
	echo "=="
	echo "================================================================================================================================="
	echo "=="
	echo "==\tA problem occurred when creating the install media with Apple's createinstallmedia tool"
	echo "==\tThe most common issue is that the installer volume could not be unmounted because some other process is using it"
	echo "==\tClose all other windows (especially terminal windows) and try again"
	echo "=="
	echo "================================================================================================================================="
	echo "\n\n"
	exit 100
fi

echo "Piking the .IABootFiles directory"
cp boot.efi /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/boot.efi 
cp1=$?
cp boot.efi /Volumes/Install\ OS\ X\ El\ Capitan/System/Library/CoreServices/boot.efi 
cp2=$?
cp boot.efi /Volumes/Install\ OS\ X\ El\ Capitan/usr/standalone/i386/boot.efi 
cp3=$?
if [ $cp1 -ne 0 ] || [ $cp2 -ne 0 ] || [ $cp3 -ne 0 ]; then
	clear
	echo "\n\n================================================================================================================================="
	echo "=="
	echo "==\tERROR"
	echo "=="
	echo "================================================================================================================================="
	echo "=="
	echo "==\tA problem occurred when replacing the boot.efi files for the installer 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 "================================================================================================================================="
	echo "\n\n"
	exit 200
fi

mv /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/PlatformSupport.plist /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/PlatformSupport.plist.apple
ps1=$?
awk '{ if ( $1 ~ /<array>/ ) { print; print "\t\t<string>Mac-F4208DC8</string>"; print "\t\t<string>Mac-F4208DA9</string>" } else { print } }' /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/PlatformSupport.plist.apple > /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/PlatformSupport.plist
ps2=$?
cp /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/PlatformSupport.plist /Volumes/Install\ OS\ X\ El\ Capitan/System/Library/CoreServices/
ps3=$?
if [ $ps1 -ne 0 ] || [ $ps2 -ne 0 ] || [ $ps3 -ne 0 ]; then
	clear
	echo "\n\n================================================================================================================================="
	echo "=="
	echo "==\tERROR"
	echo "=="
	echo "================================================================================================================================="
	echo "=="
	echo "==\tA problem occurred when modifying the PlatformSupport.plist files"
	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 300
fi


echo "Open the InstallESD disk image"
hdiutil attach -nobrowse -noverify -owners on -kernel /Applications/Install\ OS\ X\ El\ Capitan.app/Contents/SharedSupport/InstallESD.dmg -shadow /tmp/InstallESD.shadow &> /dev/null
if [ "$?" -ne "0" ]; then
        clear
        echo "\n\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 be damaged"
	echo "==\tYou may need to download a fresh copy from Apple..."
        echo "=="
        echo "================================================================================================================================="
        echo "\n\n"
        exit 400
fi


echo "Piking the OSInstall collection"
cp OSInstall.collection /Volumes/OS\ X\ Install\ ESD/Packages/
ia1=$?
cat > /Volumes/OS\ X\ Install\ ESD/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 /Volumes/OS\ X\ Install\ ESD/Packages/
ia3=$?
mv /Volumes/OS\ X\ Install\ ESD/Packages/InstallableMachines.plist /Volumes/OS\ X\ Install\ ESD/Packages/InstallableMachines.plist.apple
ia4=$?
awk '{ if ( $1 ~ /<array>/ ) { print; print "\t\t<string>Mac-F4208DC8</string>"; print "\t\t<string>Mac-F4208DA9</string>" } else { print } }' /Volumes/OS\ X\ Install\ ESD/Packages/InstallableMachines.plist.apple > /Volumes/OS\ X\ Install\ ESD/Packages/InstallableMachines.plist
ia5=$?
rm /Volumes/OS\ X\ Install\ ESD/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\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


echo "Copy the BaseSystem disk image to /tmp"
rsync -ah --progress /Volumes/OS\ X\ Install\ ESD/BaseSystem.dmg /tmp
if [ "$?" -ne "0" ]; then
        clear
        echo "\n\n================================================================================================================================="
        echo "=="
        echo "==\tERROR"
        echo "=="
        echo "================================================================================================================================="
        echo "=="
        echo "==\tA problem occurred when copying the BaseSystem disk image to /tmp"
        echo "==\tThis is very odd and does not usually happen."
        echo "==\tCheck that you have sufficient disk space left on your system to complete the copy"
	echo "==\tYou need approximately 465MB or more of free disk space for /tmp"
        echo "=="
        echo "================================================================================================================================="
        echo "\n\n"
        exit 600
fi
rm /Volumes/OS\ X\ Install\ ESD/BaseSystem.dmg
if [ "$?" -ne "0" ]; then
        clear
        echo "\n\n================================================================================================================================="
        echo "=="
        echo "==\tERROR"
        echo "=="
        echo "================================================================================================================================="
        echo "=="
        echo "==\tA problem occurred when deleting the BaseSystem disk image from the installer 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 the copy"
	echo "==\tYou need approximately 465MB or more of free disk space for /tmp"
        echo "=="
        echo "================================================================================================================================="
        echo "\n\n"
        exit 700
fi

echo "Open the BaseSystem disk image"
hdiutil attach -nobrowse -noverify -owners on -kernel /tmp/BaseSystem.dmg -shadow &>/dev/null
if [ "$?" -ne "0" ]; then
        clear
        echo "\n\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 "================================================================================================================================="
        echo "\n\n"
        exit 800
fi

echo "Piking the Base System"
chflags nouchg /Volumes/OS\ X\ Base\ System/System/Library/CoreServices/boot.efi
bs1=$?
cp boot.efi /Volumes/OS\ X\ Base\ System/System/Library/CoreServices/boot.efi 
bs2=$?
cp bootbase.efi /Volumes/OS\ X\ Base\ System/System/Library/CoreServices/bootbase.efi 
bs3=$?
cp boot.efi /Volumes/OS\ X\ Base\ System/usr/standalone/i386/boot.efi 
bs4=$?
if [ $bs1 -ne 0 ] || [ $bs2 -ne 0 ] || [ $bs3 -ne 0 ] || [ $bs4 -ne 0 ]; then
	clear
	echo "\n\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 "================================================================================================================================="
	echo "\n\n"
	exit 900
fi


echo "Adding the missing fonts..."
missingFontsPath=$(pwd)
(cd /Volumes/OS\ X\ Base\ System/System/Library/Fonts; tar zxf "$missingFontsPath/missing-fonts.tgz" )
fnts=$?
if [ $fnts -ne 0 ]; then
	clear
	echo "\n\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


echo "Close the BaseSystem disk image"
hdiutil detach /Volumes/OS\ X\ Base\ System/ &>/dev/null
if [ "$?" -ne "0" ]; then
	clear
	echo "\n\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

echo "Put the modified BaseSystem back into the InstallESD disk image"
hdiutil convert -format UDZO -imagekey zlib-level=9 /tmp/BaseSystem.dmg -shadow -o /Volumes/OS\ X\ Install\ ESD/BaseSystem.dmg
bsn1=$?
rm /tmp/BaseSystem.dmg /tmp/BaseSystem.dmg.shadow
bsn2=$?
if [ $bsn1 -ne 0 ] || [ $bsn2 -ne 0 ]; then
	clear
	echo "\n\n================================================================================================================================="
	echo "=="
	echo "==\tERROR"
	echo "=="
	echo "================================================================================================================================="
	echo "=="
	echo "==\tA problem occurred when putting the BaseSystem image back into the installer 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 "================================================================================================================================="
	echo "\n\n"
	exit 1200
fi


echo "Close the InstallESD disk image"
hdiutil detach /Volumes/OS\ X\ Install\ ESD &>/dev/null
if [ "$?" -ne "0" ]; then
	clear
	echo "\n\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 "================================================================================================================================="
	echo "\n\n"
	exit 1300
fi


echo "Put the modified InstallESD back into the Install OS X El Capitan volume"
rm /Volumes/Install\ OS\ X\ El\ Capitan/Install\ OS\ X\ El\ Capitan.app/Contents/SharedSupport/InstallESD.dmg
iesd1=$?
hdiutil convert -format UDZO -imagekey zlib-level=9 /Applications/Install\ OS\ X\ El\ Capitan.app/Contents/SharedSupport/InstallESD.dmg -shadow /tmp/InstallESD.shadow -o /Volumes/Install\ OS\ X\ El\ Capitan/Install\ OS\ X\ El\ Capitan.app/Contents/SharedSupport/InstallESD
iesd2=$?
rm /tmp/InstallESD.shadow
iesd3=$?
if [ $iesd1 -ne 0 ] || [ $iesd2 -ne 0 ] || [ $iesd3 -ne 0 ]; then
	clear
	echo "\n\n================================================================================================================================="
	echo "=="
	echo "==\tERROR"
	echo "=="
	echo "================================================================================================================================="
	echo "=="
	echo "==\tA problem occurred when putting the InstallESD image back into the installer 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 "================================================================================================================================="
	echo "\n\n"
	exit 1400
fi

bless --folder /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles --file /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/boot.efi

clear
echo "\n\n\n\t\t#########   DONE PIKIFYING   ###########\n\n\n"




hw=$(/usr/sbin/system_profiler SPHardwareDataType)
model=$(echo "$hw" | awk '/Identifier/ {print $3}')
mem=$(echo "$hw" | awk '/Memory/ {print $2}')
mult=$(echo "$hw" | awk '/Memory/ {print $3}')
if [ "$model" == "MacPro1,1" ] || [ "$model" == "MacPro2,1" ]; then
	if [ "$mult" != "GB" ] || [ "$mem" -lt "12" ]; then
		echo "Your current system appears to be a $model with $mem$mult of RAM"
		echo "Please note that if you intend to install El Capitan with this pikified installer"
		echo "then you MUST have at least 12GB of RAM installed\n"
		echo "We think there is a memory leak somewhere, and the installer will fail with <12GB"
	fi
fi
exit 0
