#!/bin/sh

APDIR=/Users/XXX/Desktop
DMG="/Users/XXX/Desktop/osx-leopard105.dmg"

cpfix () 
{
    cp -r ./files/$1 $2
    chown -R root:wheel $2
    chmod -R 755 $2
}

cd $APDIR

echo Converting DMG to editable image...
hdiutil convert $DMG -format UDTO -o $APDIR/leopard.iso

echo Mounting new ISO read-write...
hdiutil attach -readwrite $APDIR/leopard.iso.cdr -owners on

echo Waiting for leopard.iso to be mounted...
sleep 5

echo Renaming image to osx86dvd...
diskutil rename /Volumes/Mac\ OS\ X\ Install\ DVD/ osx86dvd

##### Remove old files #####

echo Removing Xcode Tools...
rm -rf /Volumes/osx86dvd/Optional\ Installs/Xcode\ Tools/

echo Removing X11user and 6 languages files...
rm -rf /Volumes/osx86dvd/System/Installation/Packages/X11user.pkg
rm -rf /Volumes/osx86dvd/System/Installation/Packages/Dutch.pkg
rm -rf /Volumes/osx86dvd/System/Installation/Packages/Danish.pkg
rm -rf /Volumes/osx86dvd/System/Installation/Packages/Finnish.pkg
rm -rf /Volumes/osx86dvd/System/Installation/Packages/Polish.pkg
rm -rf /Volumes/osx86dvd/System/Installation/Packages/Russian.pkg
rm -rf /Volumes/osx86dvd/System/Installation/Packages/Swedish.pkg

echo Removing minibless...
rm -rf "/Volumes/osx86dvd/Install Mac OS X.app/Contents/Resources/minibless"

echo Removing printer packages...
rm -rf /Volumes/osx86dvd/System/Installation/Packages/*Printer*

echo Removing OSInstall...
rm -rf /Volumes/osx86dvd/System/Installation/Packages/OSInstall.mpkg

echo Removing Kernel files...
rm -rf /Volumes/osx86dvd/mach_kernel
rm -rf /Volumes/osx86dvd/mach.sys

echo Removing Boot-related things...
rm -rf /Volumes/osx86dvd/usr/standalone/i386
rm -rf /Volumes/osx86dvd/usr/sbin/bless
rm -rf /Volumes/osx86dvd/System/Library/CoreServices/boot.efi

echo Removing Extensions...
rm -rf /Volumes/osx86dvd/System/Library/Extensions.mkext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleSMBIOS.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleACPIPlatform.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleIntelIntegratedFramebuffer.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/Natit.kext

rm -rf /Volumes/osx86dvd/System/Library/Extensions/IOATAFamily.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/IOAHCIFamily.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleAC97Audio.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleAHCIPort.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleAzaliaAudio.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleVIA.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleVIAATA.kext
rm -rf /Volumes/osx86dvd/System/Library/Extensions/IOHDIXController.kext

rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleHWSensor.kext

echo Removing files Needed for kernels without power_management_init called
rm -rf /Volumes/osx86dvd/System/Library/Extensions/AppleIntelCPUPowerManagement.kext 
rm -rf /Volumes/osx86dvd/System/Library/LaunchDaemons/ossmgr.plist

##### Adding necessary files #####

echo Replacing minibless...
cp files/minibless "/Volumes/osx86dvd/Install Mac OS X.app/Contents/Resources/minibless"
chown root:wheel "/Volumes/osx86dvd/Install Mac OS X.app/Contents/Resources/minibless"
chmod 755 "/Volumes/osx86dvd/Install Mac OS X.app/Contents/Resources/minibless"

echo Replacing OSInstall.mpkg...
cpfix OSInstall.mpkg /Volumes/osx86dvd/System/Installation/Packages/OSInstall.mpkg

echo Replacing boot-related files...
cpfix i386 /Volumes/osx86dvd/usr/standalone/i386
cpfix bless /Volumes/osx86dvd/usr/sbin/bless

echo Replacing Kernel...
cpfix mach_kernel /Volumes/osx86dvd/mach_kernel

echo Replacing extensions...
for kext in `ls files/Extensions`; do
	cpfix Extensions/$kext /Volumes/osx86dvd/System/Library/Extensions/
done

#echo Editing IOATAFamily.kext Info.plist, ICH2-ICH6 ATA Fix
#cpfix Info.plist /Volumes/osx86dvd/System/Library/Extensions/IOATAFamily.kext/Contents/PlugIns/AppleIntelPIIXATA.kext/Contents/Info.plist

echo ALL PATCHES APPLIED !!!

echo Creating bootable DVD in 10 seconds...
echo You might want to CTRL-Z and inspect /Volumes/osx86dev now.
sleep 10

##### Generate Bootable ISO #####

MKISOFS="$APDIR"/resources/mkisofs
VOLNAME="osx86dvd"
MKSIOFS_QUIET=
HDIUTIL_QUIET=
I386ISO=/tmp/i386booter.iso
MASTERCD="$APDIR"/leopard.iso
MSTRCDMNTD=/Volumes/osx86dvd/
# This gives you a DVD-R sized disc image
SIZE="4481M"
# The following can be anywhere you like its the the output image
CDDMG="$APDIR"/Leo_Patched_DVD

mkdir -p /tmp/i386
cp $MSTRCDMNTD/usr/standalone/i386/* /tmp/i386/
cd /tmp/i386
"$MKISOFS" -V "$VOLNAME" -no-emul-boot -boot-load-size 4 -c boot.cat -b cdboot $MKISOFS_QUIET -o "$I386ISO" .

sectors=`du "$I386ISO" | tail -1 | awk '{print $1}'`

echo Creating a bootable image and remove any previous copies...
cd "$MSTRCDMNTD"
if [ -f "$CDDMG" -o -f "$CDDMG".dmg ]; then
        rm -f "$CDDMG" "$CDDMG".dmg
fi
hdiutil create $HDIUTIL_QUIET "$CDDMG".dmg -size $SIZE -layout NONE
dev=`hdid -nomount "$CDDMG".dmg | tail -1 | awk '{print $1}'`
rdev=`echo $dev | sed s/disk/rdisk/`

pdisk $rdev -initialize
blocks=`pdisk $rdev -dump | grep 2: | awk -F" " '{print $4}'`
if [ "$QUIET" == "" ]; then
        pdisk $rdev -dump
fi

echo Creating partition on the image...
pdisk $rdev -createPartition "$VOLNAME" Apple_HFS $sectors `expr $blocks - $sectors`
# Figure out what slice the partition was created on
slice=`pdisk $rdev -dump | grep "$VOLNAME" | awk -F: '{print $1}' | awk -F" " '{print $1}'`

echo Copying data onto the image...
dd if="$I386ISO" of=$rdev skip=64 seek=64 bs=512
newfs_hfs -v "$VOLNAME" ${rdev}s${slice}
mkdir -p /mnt
mount -t hfs -o perm ${dev}s${slice} /mnt

echo DITTOING...
ditto -rsrc "$MSTRCDMNTD" /mnt

echo BLESSING...
bless -folder /mnt/System/Library/CoreServices -bootinfo /mnt/usr/standalone/i386/boot -label "$VOLNAME"
umount /mnt
hdiutil eject $HDIUTIL_QUIET $dev
cd /tmp/

echo Update prebinding...
update_prebinding -root /Volumes/osx86dvd -force

echo Renaming  to original image name...
diskutil rename /Volumes/osx86dvd Mac\ OS\ X\ Install\ DVD
hdiutil eject /Volumes/Mac\ OS\ X\ Install\ DVD

echo Renaming image to actual extension type...
mv "$CDDMG".dmg "$CDDMG".iso
hdiutil mount "$CDDMG".iso -readwrite -owners on
diskutil rename /Volumes/osx86dvd Mac\ OS\ X\ Install\ Disc\ x86
hdiutil eject /Volumes/Mac\ OS\ X\ Install\ Disc\ x86

echo Removing tempdisk and useless dirs...
rm -rf "$APDIR"/leopard.iso.cdr
#rm -rf /mnt

echo
echo ALL Done !!

