defaults:
  run:
    shell: powershell

name: Pack ZGM mission PBO files
on:
  issues:
    types: [opened]

jobs:
  CreatePboFiles:
    name: Create PBO files
    runs-on: windows
    if: ${{ gitea.event.issue.title == 'publish-release' }}
    steps:
      - name: Diagnostics
        run: |
          echo "repository: ${{ gitea.repository }}"
          echo "ref:  ${{ gitea.ref }}"
          echo "ref_name: ${{ gitea.ref_name }}"
          echo "sha: ${{ gitea.sha }}"
          echo "api_url: ${{ gitea.api_url }}"
          echo "workflow: ${{ gitea.workflow }}"
          echo "event: ${{ gitea.event.issue.number }}"

      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          ref: 'main'

      - name: Pull MissionTemplate
        uses: actions/checkout@v3
        with:
          repository: hizumi/testrepo
          fetch-depth: 0
          ref: 'main'
          token: ${{ secrets.RUNNER_REPO_READ_TOKEN }}
          path: MissionTemplate

      - name: Prepare tools
        run: mkdir zgm-files

      - name: Prepare template
        id: prep_template
        run: |
          mkdir template
          cd template
          Get-ChildItem ..\MissionTemplate\ | Copy-Item -Recurse
          Remove-Item -Recurse aaFrameworkUtils
          Remove-Item -Recurse framework\x
          Remove-Item -Recurse .git*
          Get-ChildItem

      - name: Get mission list
        run: |
          Get-ChildItem missions

      - name: Pack PBOs
        run: |
          $revision = git rev-parse --short=10 HEAD
          cd MissionTemplate
          $tag = git describe --abbrev=0 --tags
          cd ..
          $date = Get-Date -Format "yyyy-MM-dd"
          foreach ($file in Get-ChildItem missions) {
            $map = (Get-Item missions\$file).Basename.split('\.')[-1]
            Copy-Item missions\$file\mission.sqm template\
            (Get-Content .\template\mission_settings.hpp).Replace("MISSION AUTHOR","17th Ranger Battalion") | Set-Content .\template\mission_settings.hpp
            (Get-Content .\template\mission_settings.hpp).Replace("THIS APPEARS ON TOP OF THE LOADING SCREEN IMAGE","GAME MASTER") | Set-Content .\template\mission_settings.hpp
            (Get-Content .\template\mission_settings.hpp).Replace("THIS APPEARS BELOW THE LOADING SCREEN IMAGE","Forcing you to respawn since 2013") | Set-Content .\template\mission_settings.hpp
            (Get-Content .\template\mission_settings.hpp).Replace("THIS IS THE NAME ON THE #MISSIONS LIST","BASE ZGM $date v$tag") | Set-Content .\template\mission_settings.hpp
            (Get-Content .\template\mission_settings.hpp).Replace("THIS IS WHERE YOU DESCRIBE THE MISSION IN THE #MISSION LIST","ZGM generated $date on framework v$tag $revision") | Set-Content .\template\mission_settings.hpp
            (Get-Content .\template\mission_settings.hpp).Replace("MY SERIES NAME","ZEUS GAME MASTER") | Set-Content .\template\mission_settings.hpp

            $missionName = Get-Content .\template\mission.sqm | Select-String -Pattern 'sourceName="(.*)";'
            $missionName = $missionName.Matches.groups[1].value
            (Get-Content .\template\mission.sqm).Replace($missionName,"$map ZGM $date v$tag") | Set-Content .\template\mission.sqm

            tools\PBOconsole.exe -pack template zgm-files\BASE-ZGM-$date-v$tag.$map.pbo
          }

      - name: List PBOs
        run: |
          cd zgm-files
          Get-ChildItem

      - name: Create archive
        run: |
          $revision = git rev-parse --short=10 HEAD
          cd MissionTemplate
          $tag = git describe --abbrev=0 --tags
          cd ..
          Compress-Archive zgm-files zgm-pbo-files-v$tag-$revision.zip
          Get-ChildItem zgm-pbo-files-v$tag-$revision.zip

      - name: Backup artifacts from runner
        uses: actions/upload-artifact@v3
        with:
          name: zgm-pbo-files
          path: zgm-pbo-files*.zip

      - name: Delete old release
        run: |
          $headers = @{
            "accept" = "application/json"
            "Authorization" = "token ${{ gitea.token }}"
          }
          Invoke-WebRequest -Uri "${{ gitea.api_url }}/repos/${{ gitea.repository }}/releases/tags/release" -Method Delete -Headers $headers

      - name: Delete old release tag
        run: |
          $headers = @{
            "accept" = "application/json"
            "Authorization" = "token  ${{ gitea.token }}"
          }
          Invoke-WebRequest -Uri "${{ gitea.api_url }}/repos/${{ gitea.repository }}/tags/release" -Method Delete -Headers $headers

      - name: Create new release tag
        run: |
          $headers = @{
            "accept" = "application/json"
            "Authorization" = "token  ${{ gitea.token }}"
          }
          Invoke-WebRequest -Uri "${{ gitea.api_url }}/repos/${{ gitea.repository }}/tags" -Method Post -Headers $headers -ContentType "application/json" -Body "{`"message`": `"release`", `"tag_name`": `"release`", `"target`": `"${{ gitea.sha }}`"}"

      - name: Create release
        uses: https://gitea.com/actions/gitea-release-action@v1.3.0
        with:
          name: release
          tag_name: release
          files: |-
            zgm-pbo-files*.zip

      - name: Delete issue
        run: |
          $headers = @{
            "accept" = "application/json"
            "Authorization" = "token ${{ secrets.RUNNER_ISSUE_TOKEN }}"
          }
          $response = Invoke-RestMethod -Uri "${{ gitea.api_url }}/repos/${{ gitea.repository }}/issues/${{ gitea.event.issue.number }}" -Method Delete -Headers $headers
