name: Deploy
on:
  push:
    tags: ["v[0-9]+.[0-9]+.[0-9]+"]
jobs:
  deploy:
    name: Build and Deploy
    runs-on: windows-latest
    permissions:
      contents: write
    steps:
      - name: Set APP_VERSION variable from tag
        shell: powershell
        run: |
          $APP_VERSION = $env:GITHUB_REF -replace '^refs/tags/v', ''
          Add-Content -Path $env:GITHUB_ENV -Value "APP_VERSION=$APP_VERSION"
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Check version in project
        shell: powershell
        run: |
          Select-String Elwig/Elwig.csproj -Pattern "<Version>"
          $res = Select-String Elwig/Elwig.csproj -Pattern "<Version>${{ env.APP_VERSION }}</Version>"
          if ($res -eq $null) {
            exit 1
          }
      - name: Setup MSBuild
        uses: microsoft/setup-msbuild@v1.1
      - name: Setup NuGet
        uses: nuget/setup-nuget@v1
      - name: Restore NuGet packages
        shell: powershell
        run: $(& nuget restore Elwig.sln; $a=$lastexitcode) | findstr x*; exit $a
      - name: Build Setup
        shell: powershell
        run: $(& msbuild -verbosity:quiet Setup/Setup.wixproj -property:Configuration=Release -property:Platform=x64; $a=$lastexitcode) | findstr x*; exit $a
      - name: Rename artifact
        shell: powershell
        run: Move-Item Setup/bin/x64/Release/Elwig.exe Setup/bin/x64/Release/Elwig-${{ env.APP_VERSION }}.exe
      - name: Create release
        uses: akkuman/gitea-release-action@v1
        with:
          name: Elwig ${{ env.APP_VERSION }}
          files: |-
            Setup/bin/x64/Release/Elwig-${{ env.APP_VERSION }}.exe
      - name: Upload to website
        shell: powershell
        run: |
          $content = [System.IO.File]::ReadAllBytes("Setup/bin/x64/Release/Elwig-${{ env.APP_VERSION }}.exe")
          Invoke-WebRequest `
            -Uri "https://www.necronda.net/elwig/files/Elwig-${{ env.APP_VERSION }}.exe" `
            -Method PUT `
            -Body $content `
            -Headers @{ Authorization = "${{ secrets.API_AUTHORIZATION }}" } `
            -ContentType "application/octet-stream"