param()

trap
{
    Write-Host "An error occurred"
    Write-Host $_
    Write-Host $_.Exception.StackTrace
    exit 1
}

$ErrorActionPreference = 'Stop'

cd $PSScriptRoot\..
$root = Get-Location

if (!(Test-Path "$root\LevelEditor"))
{
    mkdir $root\LevelEditor
}

if (Test-Path "$root\LevelEditor\super-ball-brawlers-level-editor")
{
    # There's already a copy of the level editor repository here, so reset it and pull the latest version
    pushd $root\LevelEditor\super-ball-brawlers-level-editor
    git reset --hard HEAD
    if ($LastExitCode -ne 0) { exit 1 }
    git fetch --all
    if ($LastExitCode -ne 0) { exit 1 }
    git checkout -f origin/master
    if ($LastExitCode -ne 0) { exit 1 }
    popd
} 
else
{
    # We need to clone the repository
    git clone --progress ssh://git@code.redpointgames.com.au/diffusion/241/super-ball-brawlers-level-editor.git $root\LevelEditor\super-ball-brawlers-level-editor
    if ($LastExitCode -ne 0) { exit 1 }
}

# Copy the game's editor bundle to the level editor repository.
#Copy-Item -Force $root\MinuteOfMayhem.Common\obj\Windows\AnyCPU\Debug\bundle.MinuteOfMayhem.Common.xsd $root\LevelEditor\super-ball-brawlers-level-editor\GameBundle\bundle.MinuteOfMayhem.Common.xsd
Copy-Item -Force $root\SuperBallBrawlers\obj\Windows\AnyCPU\Debug\bundle.SuperBallBrawlers.xsd $root\LevelEditor\super-ball-brawlers-level-editor\GameBundle\bundle.SuperBallBrawlers.xsd

# Use the automated build script in the level editor repository.
pushd $root\LevelEditor\super-ball-brawlers-level-editor
.\Protobuild.exe --automated-build
exit $LastExitCode