name: Pack ZGM mission PBO files
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on:
  push:
    branches: [main]

jobs:
  GetMissionList:
    runs-on: windows
    steps:
      - name: Checkout missions
        uses: actions/checkout@v3
        with:
          repository: 17th-Ranger-Battalion-ORG/MissionsForZGM
          fetch-depth: 0
          ref: 'main'
          token: ${{ secrets.READ_TOKEN }}
          path: template

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

      - name: Current Directory
        run: |
          Get-Location

  CreatePboFiles:
    runs-on: windows
    steps:
      - name: Checkout template
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          ref: 'main'
          token: ${{ secrets.READ_TOKEN }}
          path: template

      - name: Prepare template
        run: |
          Remove-Item -Recurse template\aaFrameworkUtils
          Remove-Item -Recurse template\framework\x
          Remove-Item -Recurse template\.git*
      
      - name: Checkout missions
        uses: actions/checkout@v3
        with:
          repository: 17th-Ranger-Battalion-ORG/MissionsForZGM
          fetch-depth: 0
          ref: 'main'
          token: ${{ secrets.READ_TOKEN }}
          path: zgm-files

      - name: Prepare tools
        run: mkdir packed-files

      - name: Pack PBOs
        run: |
          $date = Get-Date -Format "yyyy-MM-dd"
          foreach ($file in Get-ChildItem missions) {
            $map = (Get-Item zgm-files\missions\$file).Basename.split('\.')[-1]
            Copy-Item zgm-files\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") | Set-Content .\template\mission_settings.hpp
            (Get-Content .\template\mission_settings.hpp).Replace("THIS IS WHERE YOU DESCRIBE THE MISSION IN THE #MISSION LIST","I have 2 headless clients, and am most likely an airfield spawn") | 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,"FRAMEWORK ZGM $date") | Set-Content .\template\mission.sqm

            zgm-files\tools\PBOconsole.exe -pack template packed-files\mission.$map.pbo
          }

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

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

      - name: Create release
        uses: https://gitea.com/actions/gitea-release-action@v1.1.6
        with:
          tag_name: 'snapshot-release'
          release_name: 'snapshot-release'
          files: |-
            packed-files/**            
          api_key: '${{secrets.RELEASE_TOKEN}}'

