name: Build

on:
  push:
    tags:
      - "*.*"

jobs:
  patch:
    runs-on: windows-latest
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@main
      - name: Signing all .ps1, .psm1, .psd1 files
        run: |
          . Scripts\Sign.ps1

      - name: Dependencies
        run: |
          . "Scripts\Dependencies.ps1"

      - name: Sophia Script for Windows 10
        run: |
          . "Scripts\Windows_10.ps1"

      - name: Sophia Script for Windows 10 PowerShell 7
        run: |
          . "Scripts\Windows_10_PS_7.ps1"

      - name: Sophia Script for Windows 10 LTSC 2019
        run: |
          . "Scripts\Windows_10_LTSC_2019.ps1"

      - name: Sophia Script for Windows 10 LTSC 2021
        run: |
          . "Scripts\Windows_10_LTSC_2021.ps1"

      - name: Sophia Script for Windows 11
        run: |
          . "Scripts\Windows_11.ps1"

      - name: Sophia Script for Windows 11 PowerShell 7
        run: |
          . "Scripts\Windows_11_PS_7.ps1"

      - name: Sophia Script for Windows 11 LTSC 2024
        run: |
          . "Scripts\Windows_11_LTSC_2024.ps1"

      - name: Sophia Script Wrapper
        run: |
          . "Scripts\Wrapper.ps1"

      - name: ReleaseNotesTemplate
        id: read_release
        run: |
          # Get a penultimate build tag
          $Token = "${{ secrets.GITHUB_TOKEN }}"
          $Headers = @{
              Accept        = "application/json"
              Authorization = "Bearer $Token"
          }
          $Parameters = @{
              Uri             = "https://api.github.com/repos/farag2/Sophia-Script-for-Windows/releases"
              Headers         = $Headers
              UseBasicParsing = $true
              Verbose         = $true
          }
          $Penultimate = (Invoke-RestMethod @Parameters).tag_name | Select-Object -Index 1

          # Parse json for the latest script versions
          $Parameters = @{
              Uri             = "https://raw.githubusercontent.com/farag2/Sophia-Script-for-Windows/master/sophia_script_versions.json"
              UseBasicParsing = $true
              Verbose         = $true
          }
          $Sophia_Script_Windows_10_PowerShell_5_1 = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_10_PowerShell_5_1
          $Sophia_Script_Windows_10_PowerShell_7 = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_10_PowerShell_7
          $Sophia_Script_Windows_10_LTSC2019 = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_10_LTSC2019
          $Sophia_Script_Windows_10_LTSC2021 = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_10_LTSC2021
          $Sophia_Script_Windows_11_PowerShell_5_1 = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_11_PowerShell_5_1
          $Sophia_Script_Windows_11_PowerShell_7 = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_11_PowerShell_7
          $Sophia_Script_Windows_11_LTSC2024 = (Invoke-RestMethod @Parameters).Sophia_Script_Windows_11_LTSC2024
          $Sophia_Script_Wrapper = (Invoke-RestMethod @Parameters).Sophia_Script_Wrapper

          # Replace variables with script latest versions
          # No need to replace special characters with percent-encoding ones
          (Get-Content -Path ReleaseNotesTemplate.md -Encoding utf8 -Raw) | Foreach-Object -Process {
              $_ -replace "NewVersion", "${{ github.ref_name }}" `
              -replace "OldVersion", $Penultimate `
              -replace "Sophia_Script_Windows_10_PowerShell_5_1", $Sophia_Script_Windows_10_PowerShell_5_1 `
              -replace "Sophia_Script_Windows_10_PowerShell_7", $Sophia_Script_Windows_10_PowerShell_7 `
              -replace "Sophia_Script_Windows_10_LTSC2019", $Sophia_Script_Windows_10_LTSC2019 `
              -replace "Sophia_Script_Windows_10_LTSC2021", $Sophia_Script_Windows_10_LTSC2021 `
              -replace "Sophia_Script_Windows_11_PowerShell_5_1", $Sophia_Script_Windows_11_PowerShell_5_1 `
              -replace "Sophia_Script_Windows_11_PowerShell_7", $Sophia_Script_Windows_11_PowerShell_7 `
              -replace "Sophia_Script_Windows_11_LTSC2024", $Sophia_Script_Windows_11_LTSC2024 `
              -replace "Sophia_Script_Wrapper", $Sophia_Script_Wrapper
          } | Set-Content -Path ReleaseNotesTemplate.md -Encoding utf8 -Force

          # https://trstringer.com/github-actions-multiline-strings/
          Add-Content -Path $env:GITHUB_OUTPUT -Value "ReleaseBody=ReleaseNotesTemplate.md"

          $ReleaseName = Get-Date -f "dd.MM.yyyy"
          echo "RELEASE_NAME=$ReleaseName" >> $env:GITHUB_ENV

      - name: Uploading
        # https://github.com/softprops/action-gh-release/issues/556#issuecomment-2556947371
        uses: softprops/action-gh-release@v2.1.0
        with:
          name: ${{ steps.read_release.outputs.RELEASE_NAME }}
          token: ${{ github.token }}
          files: |
            Sophia*.zip
            SHA256SUM
          body_path: ${{ steps.read_release.outputs.ReleaseBody }}