
if ((Test-Path -Type Leaf -Path "$PSScriptRoot/vars-config.ps1"))
{
    &"$PSScriptRoot/vars-config.ps1"
    &"$PSScriptRoot/vars-db-schema.ps1"
    $savePath = $ROOT
} else
{
    $CWD = [string](Get-Location)
    $savePath = $CWD
    $DbSchemaFolder = $CWD
    $SchemaFolder = 'Cisa'
}

##################################################################################################################

$fileSave = "$savePath/combined-synonyms.sql"
if (Test-Path $fileSave -PathType Leaf) {
    Clear-Content $fileSave
}

$files = Get-ChildItem "$DbSchemaFolder/Synonyms/" -Filter *.sql

$content = "`r`n -- drop`r`n"

$files | Foreach-Object {
    $sss = $_.Name
    $sss = $sss -replace "^dbo\.",''
    $sss = $sss -replace "\.sql$",''

    $content = $content + "DROP SYNONYM IF EXISTS [dbo].[$sss]"
    $content = $content + "`r`n"
}

$content = $content + "GO"
$content = $content + "`r`n"
$content = $content + "-- insert"
$content = $content + "`r`n"

$files | Foreach-Object {
    $content = $content + (Get-Content $_.FullName -Encoding UTF8 -Raw)
}

$content | Out-File $fileSave

##################################################################################################################

$fileSave = "$savePath/combined-procedures.sql"
if (Test-Path $fileSave -PathType Leaf) {
    Clear-Content $fileSave
}

$files = Get-ChildItem "$DbSchemaFolder/procedures/" -Filter *.sql

$content = "`r`n -- drop`r`n"

$files | Foreach-Object {
    $sss = $_.Name
    $sss = $sss -replace "^dbo\.",''
    $sss = $sss -replace "\.sql$",''

    $content = $content + "DROP PROCEDURE IF EXISTS [dbo].[$sss]"
    $content = $content + "`r`n"
}

$content = $content + "GO"
$content = $content + "`r`n"
$content = $content + "-- insert"
$content = $content + "`r`n"

$files | Foreach-Object {
    $content = $content + (Get-Content $_.FullName -Encoding UTF8 -Raw)
}

$content | Out-File $fileSave

##################################################################################################################

$fileSave = "$savePath\combined-views.sql"
if (Test-Path $fileSave -PathType Leaf) {
    Clear-Content $fileSave
}

if ($SchemaFolder -eq "Cisa")
{
    $views = $null
} else {
    $views = "vw_person", "vw_student", "vw_staff", "vw_enrolment", "vw_activity", "vw_moduleActivity", "vw_module", "vw_studentLearningPlanHours", "vw_moduleRelation"    
}

$content = "`r`n -- drop`r`n"
Foreach ($view in $views) {
    $sss = $view
    $content = $content + "DROP VIEW IF EXISTS [dbo].[$sss]"
    $content = $content + "`r`n"
}
$content = $content + "GO"
$content = $content + "`r`n"
$content = $content + "-- insert"
$content = $content + "`r`n"
Foreach ($view in $views) {
    $content = $content + (Get-Content "$DbSchemaFolder/Views/$view.sql" -Encoding UTF8 -Raw)
}

$files = Get-ChildItem "$DbSchemaFolder/Views/" -Filter *.sql -ErrorAction SilentlyContinue

$content = $content + "`r`n -- drop`r`n"

$files | Foreach-Object {
    $sss = $_.Name
    $sss = $sss -replace "^dbo\.",''
    $sss = $sss -replace "\.sql$",''

    if ($views.Contains($sss)) {
        return
    }

    $content = $content + "DROP VIEW IF EXISTS [dbo].[$sss]"
    $content = $content + "`r`n"
}

$content = $content + "GO"
$content = $content + "`r`n"
$content = $content + "-- insert"
$content = $content + "`r`n"

$files | Foreach-Object {
    $sss = $_.Name
    $sss = $sss -replace "^dbo\.",''
    $sss = $sss -replace "\.sql$",''

    if ($views.Contains($sss)) {
        return
    }

    $content = $content + (Get-Content $_.FullName -Encoding UTF8 -Raw)
}

$content | Out-File $fileSave

##################################################################################################################

$fileSave = "$savePath/combined-fn.sql"

if (Test-Path -Type Container -Path "$savePath/DB/Types/User-defined Data Types/")
{
    $files = Get-ChildItem "$savePath/DB/Types/User-defined Data Types/" -Filter *.sql -Force -ErrorAction SilentlyContinue
    $content = "`r`n -- drop`r`n"
    $files | Foreach-Object {
        $sss = $_.Name
        $sss = $sss -replace "^dbo\.",''
        $sss = $sss -replace "\.sql$",''
        $content = $content + "DROP TYPE IF EXISTS [dbo].[$sss]"
        $content = $content + "`r`n"
    }
    $content = $content + "GO"
    $content = $content + "`r`n"
    $content = $content + "-- insert"
    $content = $content + "`r`n"

    $files | Foreach-Object {
        $content = $content + (Get-Content $_.FullName -Encoding UTF8 -Raw)
    }
}

if ($SchemaFolder -eq "Cisa")
{
    $fns = New-Object -TypeName 'System.Collections.ArrayList'
} else {
    $fns= "flx_fn_studentTargetGrades", "ops_fn_splitNumericString", "flx_fn_moduleTree", "flx_fn_moduleParents", "flx_fn_moduleChildren"
}

$content = "`r`n -- drop`r`n"
Foreach ($fn in $fns) {
    $content = $content + "DROP FUNCTION IF EXISTS [dbo].[$fn]"
    $content = $content + "`r`n"
}
$content = $content + "GO"
$content = $content + "`r`n"
$content = $content + "-- insert"
$content = $content + "`r`n"
Foreach ($fn in $fns) {
    $content = $content + (Get-Content "$DbSchemaFolder/functions/$fn.sql" -Encoding UTF8 -Raw)
}

$files = Get-ChildItem "$DbSchemaFolder/Functions/" -Filter *.sql

$content = "`r`n -- drop`r`n"

$files | Foreach-Object {
    $sss = $_.Name
    $sss = $sss -replace "^dbo\.",''
    $sss = $sss -replace "\.sql$",''

    if ($fns.Contains($sss)) {
        return
    }

    $content = $content + "DROP FUNCTION IF EXISTS [dbo].[$sss]"
    $content = $content + "`r`n"
}

$content = $content + "GO"
$content = $content + "`r`n"
$content = $content + "-- insert"
$content = $content + "`r`n"

$files | Foreach-Object {
    $sss = $_.Name
    $sss = $sss -replace "^dbo\.",''
    $sss = $sss -replace "\.sql$",''

    if ($fns.Contains($sss)) {
        return
    }

    $content = $content + (Get-Content $_.FullName -Encoding UTF8 -Raw)
}

$content | Out-File $fileSave