$ErrorActionPreference = 'Stop'

param(
    [Parameter(Mandatory = $true)]
    [string]$Username,

    [Parameter(Mandatory = $true)]
    [string]$Password,

    [string]$Email = '',

    [switch]$ResetPassword
)

$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
$cacheRoot = Join-Path $repoRoot '.cache\go'
$buildCache = Join-Path $repoRoot '.cache\go-build'
$modCache = Join-Path $cacheRoot 'pkg\mod'

New-Item -ItemType Directory -Force $cacheRoot, $buildCache, $modCache | Out-Null

$env:GOPATH = $cacheRoot
$env:GOCACHE = $buildCache
$env:GOMODCACHE = $modCache
$env:UMS_ADMIN_USERNAME = $Username
$env:UMS_ADMIN_PASSWORD = $Password
$env:UMS_ADMIN_EMAIL = $Email
$env:UMS_ADMIN_RESET_PASSWORD = if ($ResetPassword.IsPresent) { 'true' } else { 'false' }

Push-Location $repoRoot
try {
    & go run .\tools\init_admin.go
} finally {
    Pop-Location
}
