#!/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
# 
############################################################################
#
# 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\nRunning Apple's createinstallmedia tool...\n\n\n"
/Applications/Install\ OS\ X\ El\ Capitan.app/Contents/Resources/createinstallmedia --volume "$1" --applicationpath /Applications/Install\ OS\ X\ El\ Capitan.app --nointeraction

echo "\n\n\nPiking the .IABootFiles directory"
cp boot.efi /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/boot.efi 
cp boot.efi /Volumes/Install\ OS\ X\ El\ Capitan/System/Library/CoreServices/boot.efi 
cp boot.efi /Volumes/Install\ OS\ X\ El\ Capitan/usr/standalone/i386/boot.efi 

mv /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/PlatformSupport.plist /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/PlatformSupport.plist.apple
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
cp /Volumes/Install\ OS\ X\ El\ Capitan/.IABootFiles/PlatformSupport.plist /Volumes/Install\ OS\ X\ El\ Capitan/System/Library/CoreServices/


echo "\n\n\nOpen 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


echo "\n\n\nPiking the OSInstall collection"
cp OSInstall.collection /Volumes/OS\ X\ Install\ ESD/Packages/
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_

cp pikify.pkg /Volumes/OS\ X\ Install\ ESD/Packages/
mv /Volumes/OS\ X\ Install\ ESD/Packages/InstallableMachines.plist /Volumes/OS\ X\ Install\ ESD/Packages/InstallableMachines.plist.apple
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
rm /Volumes/OS\ X\ Install\ ESD/Packages/InstallableMachines.plist.apple

echo "\n\n\nCopy the BaseSystem disk image to /tmp"
rsync -ah --progress /Volumes/OS\ X\ Install\ ESD/BaseSystem.dmg /tmp
rm /Volumes/OS\ X\ Install\ ESD/BaseSystem.dmg

echo "\n\n\nOpen the BaseSystem disk image"
hdiutil attach -nobrowse -noverify -owners on -kernel /tmp/BaseSystem.dmg -shadow

echo "\n\n\nPiking the Base System\n"
chflags nouchg /Volumes/OS\ X\ Base\ System/System/Library/CoreServices/boot.efi
cp boot.efi /Volumes/OS\ X\ Base\ System/System/Library/CoreServices/boot.efi 
cp bootbase.efi /Volumes/OS\ X\ Base\ System/System/Library/CoreServices/bootbase.efi 
cp boot.efi /Volumes/OS\ X\ Base\ System/usr/standalone/i386/boot.efi 

echo "\n\n\nAdding the missing fonts...\n"
missingFontsPath=$(pwd)
(cd /Volumes/OS\ X\ Base\ System/System/Library/Fonts; tar zxf "$missingFontsPath/missing-fonts.tgz" )

echo "\n\n\nClose the BaseSystem disk image"
hdiutil detach /Volumes/OS\ X\ Base\ System/

echo "\n\n\nPut the modified BaseSystem back into the InstallESD disk image\n\n\n"
hdiutil convert -format UDZO -imagekey zlib-level=9 /tmp/BaseSystem.dmg -shadow -o /Volumes/OS\ X\ Install\ ESD/BaseSystem.dmg
rm /tmp/BaseSystem.dmg /tmp/BaseSystem.dmg.shadow

echo "\n\n\nClose the InstallESD disk image"
hdiutil detach /Volumes/OS\ X\ Install\ ESD

echo "\n\n\nPut the modified InstallESD back into the Install OS X El Capitan volume\n\n\n"
rm /Volumes/Install\ OS\ X\ El\ Capitan/Install\ OS\ X\ El\ Capitan.app/Contents/SharedSupport/InstallESD.dmg
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
rm /tmp/InstallESD.shadow

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