#!/bin/sh
#
#
############## modification record #########################################
#
# 2016-06-19 : Original script by rthpjm, this one is for Mac OS X 10.12 Sierra Developer Preview ONLY
# 
############################################################################
#
# 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

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

clear

echo "\n\n\n------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n"
echo "\tPikify3.2 contains a script and other items to create install media for Mac OS X Sierra (Developer Preview) to be used on 32-bit EFI based Macintosh computers"
echo "\tIt uses Apple's \"Install 10.12 Developer Preview.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"


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

if [ -d "/Volumes/OS X Install ESD" ];then
	echo "\n\n================================================================================================================================="
	echo "=="
	echo "==\tWarning:"
	echo "=="
	echo "================================================================================================================================="
	echo "=="
	echo "==	InstallESD.dmg is already mounted, attempting to detach..."
	echo "=="
	hdiutil detach -force /Volumes/OS\ X\ Install\ ESD &>/dev/null
	ie=$?
	if [ "$ie" -ne "0" ];then
        echo "\n\n================================================================================================================================="
        echo "=="
        echo "==\tERROR"
        echo "=="
        echo "================================================================================================================================="
        echo "=="
		echo "==	InstallESD.dmg is already mounted, could not detach. Stopping..."
        echo "=="
		exit -10
	fi
fi


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

rm2=0
if [ -f /tmp/InstallESD.shadow ]; then
	echo "\n\n================================================================================================================================="
	echo "=="
	echo "==\tWarning:"
	echo "=="
	echo "================================================================================================================================="
	echo "=="
	echo "==\t\tDetected the need to clean up InstallESD.dmg..."
	echo "=="
	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



needsPikifying=1

if [ -f /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD-original.dmg ]; then
        echo "\n\n================================================================================================================================="
        echo "=="
        echo "==\tWarning:"
        echo "=="
        echo "================================================================================================================================="
        echo "=="
        echo "==  It looks like the copy of Install 10.12 Developer Preview in /Applications has already been pikified..."
        echo "=="
        echo "==	What would you like to do?"
        echo "=="
        echo "==		1. Reset Install 10.12 Developer Preview back to the Apple version, then stop."
        echo "==		2. Continue on using the pikified version."
        echo "=="
        echo "=="
        /bin/echo -n "	Enter the first letter of your choice [r]eset or (default) [c]ontinue  : "
        read choice
		if [ "${choice:0:1}" == "r" ] || [ "${choice:0:1}" == "R" ]; then
			rm /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD.dmg
			mv /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD-original.dmg /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD.dmg
			clear
			echo "\n\n================================================================================================================================="
			echo "=="
			echo "==\Install 10.12 Developer Preview has been reset"
			echo "=="
			echo "================================================================================================================================="
			exit 0
		fi
		
		needsPikifying=0
fi

if [ "$needsPikifying" == "1" ]; then
	if [ ! -f /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD-original.dmg ]; then
		echo "Saving an original copy of the Install 10.12 Developer Preview InstallESD.dmg...."
		mv /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD.dmg /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD-original.dmg
		dt=$?
		if [ "$dt" -ne "0" ]; then
			clear
			echo "\n\n================================================================================================================================="
			echo "=="
			echo "==\tERROR"
			echo "=="
			echo "================================================================================================================================="
			echo "=="
			echo "==\tA problem occurred when saving a copy of Install 10.12 Developer Preview InstallESD.dmg"
			exit 100
		fi
	fi


	echo "\n\nOpen the InstallESD disk image"
	hdiutil attach -nobrowse -noverify -owners on -kernel /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD-original.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 10.12 Developer Preview\" 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 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 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 "\n\nPut the modified BaseSystem back into the InstallESD disk image"
	hdiutil convert -format UDZO -imagekey zlib-level=9 /tmp/BaseSystem.dmg -shadow /tmp/BaseSystem.shadow -o /Volumes/OS\ X\ Install\ ESD/BaseSystem.dmg
	bsn1=$?
	rm /tmp/BaseSystem.*
	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 "\n\nRebuild the InstallESD"
	hdiutil convert -format UDZO -imagekey zlib-level=9 /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD-original.dmg -shadow /tmp/InstallESD.shadow -o /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD.dmg
	iesd2=$?
	rm /tmp/InstallESD.shadow
	iesd3=$?
	if [ $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
fi


echo "\nRunning Apple's createinstallmedia tool..."
/Applications/Install\ 10.12\ Developer\ Preview.app/Contents/Resources/createinstallmedia --volume "$1" --applicationpath /Applications/Install\ 10.12\ Developer\ Preview.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\ 10.12\ Developer\ Preview/.IABootFiles/boot.efi 
cp1=$?
cp boot.efi /Volumes/Install\ 10.12\ Developer\ Preview/System/Library/CoreServices/boot.efi 
cp2=$?
cp boot.efi /Volumes/Install\ 10.12\ Developer\ Preview/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\ 10.12\ Developer\ Preview/.IABootFiles/PlatformSupport.plist /Volumes/Install\ 10.12\ Developer\ Preview/.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\ 10.12\ Developer\ Preview/.IABootFiles/PlatformSupport.plist.apple > /Volumes/Install\ 10.12\ Developer\ Preview/.IABootFiles/PlatformSupport.plist
ps2=$?
cp /Volumes/Install\ 10.12\ Developer\ Preview/.IABootFiles/PlatformSupport.plist /Volumes/Install\ 10.12\ Developer\ Preview/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


bless --folder /Volumes/Install\ 10.12\ Developer\ Preview/.IABootFiles --file /Volumes/Install\ 10.12\ Developer\ Preview/.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
