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

$db = $null
if ($args.count -eq 1)
{
    $db = $args[0].ToLower()
}

if ($db -eq "cedar")
{
    $dbTables = "--dataTables=ops_integration,flx_config,eav_config"
} 
elseif ($db -eq "cisa") {
    $DSN = $DSN -replace "database='test_CEDAR';","database='test_$CISA_DATABASE';"
    $DSN = $DSN -replace "database='CEDAR';","database='$CISA_DATABASE';"
    $DSN = $DSN -replace "database='$Global:DB_DATABASE';","database='$Global:CISA_DATABASE';"
    #$dbTables = "--dataTables=integration"
}
else {
    Write-Host "Unknown database (cisa | cedar)"
    exit
}

[void](Remove-Item "$DbSchemaFolder/data/" -Recurse -ErrorAction SilentlyContinue)
[void](Remove-Item "$DbSchemaFolder/functions/" -Recurse -ErrorAction SilentlyContinue)
[void](Remove-Item "$DbSchemaFolder/procedures/" -Recurse -ErrorAction SilentlyContinue)
[void](Remove-Item "$DbSchemaFolder/synonyms/" -Recurse -ErrorAction SilentlyContinue)
[void](Remove-Item "$DbSchemaFolder/tables/" -Recurse -ErrorAction SilentlyContinue)
[void](Remove-Item "$DbSchemaFolder/views/" -Recurse -ErrorAction SilentlyContinue)
[void](Remove-Item "$DbSchemaFolder/user_defined_types/" -Recurse -ErrorAction SilentlyContinue)
[void](Remove-Item "$DbSchemaFolder/props.sql" -ErrorAction SilentlyContinue)

&"$SchemaZen" script -c $DSN -o -d "$DbSchemaFolder" --onlyTypes user_defined_types,functions,procedures,views,synonyms,props,data $dbTables

&"$PSScriptRoot\database-combine.ps1" $db #todo passthrough
