#!/bin/sh

# Copyright 2009 org.darwinx86.app. All rights reserved.
#

# Modified Nov 18th 2011 by Rekursor
# Added 2 parameters to specify repectively :
# $1 = Extra dir  location
# $2 = cdboot file dir location
# Added error return codes to be able to better interface the script from the prefpane call
# Fixed BootCD.iso would be generated in the current script working directory
#


# Directories
if [[ $#  <  1 ]];then
	extra="/Extra"
else
	extra="$1"
fi

if [[ $# < 2 ]];then
	cdBootDir="${extra}"
else
	cdBootDir="$2"
fi

echo "Extra dir is ${extra} and cdboot dir is ${cdBootDir}"
mydir=`dirname $0`

tempDir=/tmp
finaldir=~/Desktop/Chameleon
isodir=/tmp/newiso
extradir=/tmp/newiso/Extra
preDir=/tmp/newiso/Extra/Preboot
prebootDir="${preDir}/Extra"
bkpdir="${finaldir}/Previous-dmg"

echo "Starting CD Boot ISO creation script..."

#errors
if ([ ! -f $extra/Extensions.mkext ] && [ ! -d $extra/Extensions ]);then
	echo "*** Error: no Extensions.mkext or Extensions folder. One of them is required"
	exit 1
fi

if [ ! -f $extra/org.chameleon.Boot.plist ];then
   echo "*** Error: no org.chameleon.Boot.plist found. File required"
   exit 1
fi

if [ ! -f $cdBootDir/cdboot ];then
   echo "*** Error: no cdboot found. File required"
   exit 1
fi

# Create a work directory
echo " Creating folders ..."
#copy Extra files to temp directory
if ([ ! -d $prebootDir ]);then
	mkdir -p $prebootDir
fi
if ([ ! -d $bkpdir ]);then
	mkdir -p $bkpdir
fi
echo " Copying files ..."

if [ -f $cdBootDir/cdboot ];then
   cp -R $cdBootDir/cdboot $isodir
fi

if [ -f $extra/dsdt.aml ];then
   cp -R $extra/dsdt.aml $prebootDir
fi

if [ -f $extra/DSDT.aml ];then
	cp -R $extra/DSDT.aml $prebootDir
fi

if [ -f $extra/NVIDIA.ROM ];then
	cp -R $extra/NVIDIA.ROM $prebootDir
fi

if [ -f $extra/smbios.plist ];then
   cp -R $extra/smbios.plist $prebootDir
fi

if [ -f $extra/Extensions.mkext ];then
   cp -R $extra/Extensions.mkext $prebootDir
fi

if [ -d $extra/Extensions ];then
   cp -Rp $extra/Extensions $prebootDir
fi

if [ -f $extra/org.chameleon.Boot.plist ];then
   	cp -R $extra/org.chameleon.Boot.plist $prebootDir
fi
if [ -f $tempDir/org.chameleon.Boot.plist ];then
    cp -R $tempDir/org.chameleon.Boot.plist $extradir
fi

echo " Files copied in temp folder"

# ramdisk creator
if [ -f $extradir/Preboot.dmg ];then
	rm $extradir/Preboot.dmg
fi
hdiutil create -srcfolder $preDir/ -layout GPTSPUD -fs HFS+ -format UDRW -volname Preboot $extradir/Preboot.dmg
rm -R $preDir
echo " Ram disk created"

# boot cd creator
hdiutil makehybrid -o $tempDir/BootCD.iso $isodir/ -iso -hfs -joliet -eltorito-boot $isodir/cdboot -no-emul-boot -hfs-volume-name "Boot CD" -joliet-volume-name "Boot CD"
if [ ! -f $tempDir/BootCD.iso ];then
	echo "*** Error: couldn't Create ISO Image."
	exit 1
else
	echo " Hybrid image created"
fi

# Create output and backup directories
if [ -f $finaldir/BootCD.iso ];then
   if [ ! -d $bkpdir ];then
   mkdir -p $bkpdir
   echo " Backup folder created"
	fi
   mv -f $finaldir/BootCD.iso $bkpdir/BootCd-$(date +"%d-%y-%Hh%M").iso
    echo " Previous ISO moved into backup folder"
fi
mv $tempDir/BootCD.iso $finaldir
echo " ISO moved on desktop"

# cleanup
rm -rf $isodir/*
echo " ISO created successfully"
exit 0