
&"$PSScriptRoot\vars-config.ps1"
&"$PSScriptRoot\vars-db-schema.ps1"

Write-Host ""
Write-Host "DANGER DANGER : This script fetches a copy of database scripts. These might not align with the release, causing problems."
Write-Host ""

$ver = Read-Host "Pull 'version' or 'latest' copy of database files: "
if ((-Not ($ver -eq 'version')) -and (-Not ($ver -eq 'latest'))) {
    exit
}

if ($ver -eq 'latest')
{
    [void](Invoke-WebRequest -Uri "$CedarReleases/sp.zip" -OutFile "$DbSchemaFolder/sp.zip" -SkipCertificateCheck)
    Expand-Archive -Force "$DbSchemaFolder/sp.zip" -DestinationPath "$DbSchemaFolder"
}

if ($ver -eq 'version')
{
    &"$PSScriptRoot\version.ps1"

    [void](Invoke-WebRequest -Uri "$CedarReleases/$version.sp.zip" -OutFile "$DbSchemaFolder/$version.sp.zip" -SkipCertificateCheck)
    Expand-Archive -Force "$DbSchemaFolder/$version.sp.zip" -DestinationPath "$DbSchemaFolder/"
}

if (-Not(Test-Path -Path "$DbSchemaFolder\procedures" -PathType Container))
{
    Write-Host "Schema not found in $DbSchemaFolder"
    exit
}

&"$PHPexe" "$ROOT/src/bootstrap.php" upgrade:refresh:db
