param (
    [string] $baseFolder = ".",
    [string] $appShort,
    [string] $mainGo,
    [switch] $skipApiFetch,
    [switch] $skipGoFetch
)


$cd = Get-Location
Set-Location $baseFolder

$wellFile = "./.well-known/devops.json"
if (!(Test-Path $wellFile -PathType Leaf)) {
    Write-Host "The specification file '$wellFile' not found" -ForegroundColor Red
    Set-Location $cd
    Throw "The specification file '$wellFile' not found"
}
Write-Host "Using specification file '$wellFile' " -ForegroundColor White

$devops =  Get-Content $wellFile | ConvertFrom-JSON 

$openApiFile = ""
$devops.apis | ForEach-Object {
    if ($_.engine.url.endswith(".json") -and ($_.engine.category -eq "API") -and ($_.engine.name -eq "openapi")) {
        $openApiFile = $baseFolder + $_.engine.url
    }
}


if ((Test-Path -Path "./build") -eq $false) {
    Write-Host "Missing './build' directory" -ForegroundColor Red
    Set-Location $cd
    Throw "Missing './build' directory"
}



# API Source URL 
$openApiUrl = ""
if ($openApiFile -eq "" -and $skipApiFetch -eq $false) {
    $openApiUrl = ""
    if ($openApiFile -eq "") {
        $openApiFile = "./build/openapi.json"
    }
}

# Download the file
if ($openApiUrl -ne "" -and $skipApiFetch -eq $false) {
    Write-Host "Fetching Open API definition"
    try
    {
        $Response = Invoke-WebRequest -Uri $openApiUrl -OutFile $openApiFile
        $StatusCode = $Response.StatusCode
        Write-Host "Fetch completed"
    } catch {
        $StatusCode = $_.Exception.Response.StatusCode.value__
    }
    $StatusCode
}

# Find the Open API file
if ($openApiFile -eq "") {
    Get-ChildItem –Path "./build/" -Filter *.json | Foreach-Object {
        $openApiFile = $_.FullName
    }
}


if ($openApiFile -eq "") {
    Write-Host "Missing Open API in './build' directory" -ForegroundColor Red
    Set-Location $cd
    Throw "Missing Open API in './build' directory"
}

$openApiFile = Resolve-Path -Path $openApiFile 

if ($openApiFile -ne "./static/openapi.json") {
    Copy-Item $openApiFile -Destination "./static/openapi.json" 
}

Set-Location "./src"

# Find the main Go build file
if ($mainGo -eq "") {
    Get-ChildItem –Path "./" -Filter *.go | Foreach-Object {
        $mainGo = $_.FullName   
    }
}

if ($mainGo -eq "") {
    if ((Test-Path -Path "./servermain.go") -eq $false) {
        New-Item -Path './servermain.go' -ItemType File| Out-Null
    }
    $mainGo = "./servermain.go"
}


if ($mainGo -eq "") {
    Write-Host "Missing main Go file in current directory" -ForegroundColor Red
    Set-Location $cd
    Throw "Missing main Go file in current directory"
}


if ($isWindows) {
    go env -w GOOS=windows
} else {
    go env -w GOOS=linux
}
go env -w GOARCH=386


if ($skipGoFetch -eq $false) {
    Write-Host "Fetching Go packages"
    go get gopkg.in/yaml.v2
    go get github.com/deepmap/oapi-codegen/pkg/codegen
    go get github.com/deepmap/oapi-codegen/pkg/util
    go get github.com/deepmap/oapi-codegen/pkg/types@v1.11.0
    go get github.com/labstack/echo/v4/middleware@v4.7.2
    go get github.com/getkin/kin-openapi/routers/gorillamux@v0.94.0
    go get github.com/dgrijalva/jwt-go
}

$codeGen = $env:USERPROFILE + "/go/pkg/mod/github.com/deepmap/oapi-codegen@v1.11.0/cmd/oapi-codegen/oapi-codegen.go"

Write-Host "Generating code from $openApiFile"
go run $codeGen  --config ../build/types.cfg.yaml $openApiFile 
go run $codeGen  --config ../build/server.cfg.yaml $openApiFile

Write-Host "Building Go executable $mainGo"
go build $mainGo


Set-Location $cd

Write-Host "Code generation completed" -ForegroundColor Green