## <script src="/dist/scripts/cli-bash.js"></script>
## <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/themes/prism-okaidia.min.css" rel="stylesheet" />
## <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/components/prism-core.min.js" data-manual></script>
## <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/components/prism-powershell.min.js"></script>
## <style>body {color: #272822; background-color: #272822; font-size: 0.8em;} </style>
# Love open-source, dev-tooling and passionate about code as much as we do?
# ---
# We're always looking for awesome hackers like you to join our 100% remote team!
# Check and see if you find any relevant position @ https://{{ spec.title | lower }}.io/company/careers 👩‍💻 😎
# (and let us know you found this message...)

# This script contains hidden JS code to allow better readability and syntax highlighting
# You can use "View source" of this page to see the full script.

# REPO
$GITHUB_x64_URL = "https://github.com/{{ sdk.gitUserName }}/{{ sdk.gitRepoName | caseDash }}/releases/download/{{ sdk.version }}/{{ language.params.executableName }}-cli-win-x64.exe"
$GITHUB_arm64_URL = "https://github.com/{{ sdk.gitUserName }}/{{ sdk.gitRepoName | caseDash }}/releases/download/{{ sdk.version }}/{{ language.params.executableName }}-cli-win-arm64.exe"

${{ spec.title | upper }}_BINARY_NAME = "{{ language.params.executableName }}.exe"

# {{ spec.title | caseUcfirst }} download directory
${{ spec.title | upper }}_DOWNLOAD_DIR = Join-Path -Path $env:TEMP -ChildPath ${{ spec.title | upper }}_BINARY_NAME

# {{ spec.title | caseUcfirst }} CLI location
${{ spec.title | upper }}_INSTALL_DIR = Join-Path -Path $env:LOCALAPPDATA -ChildPath "{{ spec.title | caseUcfirst }}"
${{ spec.title | upper }}_INSTALL_PATH = Join-Path -Path "${{ spec.title | upper }}_INSTALL_DIR" -ChildPath "${{ spec.title | upper }}_BINARY_NAME"

$USER_PATH_ENV_VAR = [Environment]::GetEnvironmentVariable("PATH", "User")

function Greeting {
    Write-Host @"
{{ language.params.logoUnescaped | raw }}

"@ -ForegroundColor red
    Write-Host "Welcome to the {{ spec.title | caseUcfirst }} CLI install shield."  
}


function CheckSystemInfo {
    Write-Host "[1/4] Getting System Info ..."
    if ((Get-ExecutionPolicy) -gt 'RemoteSigned' -or (Get-ExecutionPolicy) -eq 'ByPass') {
        Write-Host "PowerShell requires an execution policy of 'RemoteSigned'."
        Write-Host "To make this change please run:"
        Write-Host "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"
        break
    }
}

function DownloadBinary {
    Write-Host "[2/4] Downloading {{ spec.title | caseUcfirst }} CLI binary ..."
    Write-Host "🚦 Fetching latest version ... " -ForegroundColor green

    if((Get-CimInstance Win32_operatingsystem).OSArchitecture -like '*ARM*') {
      Invoke-WebRequest -Uri $GITHUB_arm64_URL -OutFile ${{ spec.title | upper }}_DOWNLOAD_DIR
    } else {
      Invoke-WebRequest -Uri $GITHUB_x64_URL -OutFile ${{ spec.title | upper }}_DOWNLOAD_DIR
    }

    New-Item -ItemType Directory -Path ${{ spec.title | upper }}_INSTALL_DIR
    Move-Item ${{ spec.title | upper }}_DOWNLOAD_DIR ${{ spec.title | upper }}_INSTALL_PATH
}


function Install {
    Write-Host "[3/4] Starting installation ..."

    if ($USER_PATH_ENV_VAR -like '*{{ spec.title | caseUcfirst }}*') {
        Write-Host "Skipping to add {{ spec.title | caseUcfirst }} to User Path."
    } else {
        [System.Environment]::SetEnvironmentVariable("PATH", $USER_PATH_ENV_VAR + ";${{ spec.title | upper }}_INSTALL_DIR", "User")
        $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") 
    }
}

function CleanUp {
    Write-Host "Cleaning up mess ..."
}

function InstallCompleted {
    Write-Host "[4/4] Finishing Installation ... "
    cleanup
    Write-Host "To get started with {{ spec.title | caseUcfirst }} CLI, please visit {{ sdk.url }}/docs/command-line"
    Write-Host "As first step, you can login to your {{ spec.title | caseUcfirst }} account using 'appwrite login'"
}


Greeting
CheckSystemInfo
DownloadBinary
Install
CleanUp
InstallCompleted
