name: snapshots

on:
  schedule:
    - cron: "0 00 * * *" # Daily at 00:00 UTC
  workflow_dispatch:

permissions:
  contents: write

jobs:
  snapshots-windows:
    uses: ./.github/workflows/snapshots-windows.yml
    secrets: inherit
  snapshots-android:
    uses: ./.github/workflows/snapshots-android.yml
    secrets: inherit
  snapshots:
    runs-on: ubuntu-latest
    if: ${{ github.repository_owner == 'exult' }}
    needs: [snapshots-windows, snapshots-android]
    steps:
      - name: Download Windows artifacts
        uses: actions/download-artifact@v4
        with:
          name: "windows snapshot"
      - name: Download Android artifacts
        uses: actions/download-artifact@v4
        with:
          name: "android snapshot"
      - name: Check if artifacts downloaded
        shell: bash
        run: |

          if [ -f Exult.exe ]; then
            echo HAVE_WINDOWS_SNAPSHOT=true >> $GITHUB_ENV
          else
            echo HAVE_WINDOWS_SNAPSHOT=false >> $GITHUB_ENV
            echo Couldn\'t find Exult.exe for Windows snapshot. No release will be made
          fi
            
          if [ -f exult-snapshot-signed.apk ]; then
            echo HAVE_ANDROID_SNAPSHOT=true >> $GITHUB_ENV
          else
            echo HAVE_ANDROID_SNAPSHOT=false >> $GITHUB_ENV
            echo Couldn\'t find exult-snapshot-signed.apk for Android snapshot. No release will be made
          fi
          echo "GITHUB_REF=v1.9.0.$(date --utc +'%Y%m%d')" >> $GITHUB_ENV
      - name: Install SSH key
        if: ${{ (env.HAVE_WINDOWS_SNAPSHOT == 'true' && env.HAVE_ANDROID_SNAPSHOT == 'true')}}
        uses: shimataro/ssh-key-action@v2
        with:
          key: "${{ secrets.SSH_PRIVATE_KEY }}"
          name: id_ed25519
          known_hosts: ${{ secrets.SSH_KNOWN_HOSTS }}

      - name: VirusTotal Scan
        if: ${{ (env.HAVE_WINDOWS_SNAPSHOT == 'true' && env.HAVE_ANDROID_SNAPSHOT == 'true')}}
        id: scan_files
        uses: crazy-max/ghaction-virustotal@v4
        with:
          vt_api_key: ${{ secrets.VT_API_KEY }}
          github_token: ${{ github.token }}
          update_release_body: true
          files: |
            Exult.exe
            ExultStudio.exe
            ExultTools.exe
            Gimp20Plugin.exe
      - name: Generate VirusTotal Body
        if: ${{ (env.HAVE_WINDOWS_SNAPSHOT == 'true' && env.HAVE_ANDROID_SNAPSHOT == 'true')}}
        run: |
          echo "Snapshot ${{ env.GITHUB_REF }}" > body.txt
          echo "" >> body.txt
          echo "🛡 [VirusTotal GitHub Action](https://github.com/crazy-max/ghaction-virustotal) analysis:" >> body.txt
          echo "" >> body.txt
          analysis="${{ steps.scan_files.outputs.analysis }}"
          while read -d, -r pair; do
            IFS='=' read -r filename analysisURL <<<"$pair"
            echo "* [$(basename $filename)]($analysisURL)" >> body.txt
          done <<<"$analysis,"
      - name: Create release
        if: ${{ (env.HAVE_WINDOWS_SNAPSHOT == 'true' && env.HAVE_ANDROID_SNAPSHOT == 'true')}}
        id: create_release
        uses: softprops/action-gh-release@v2
        with:
          token: ${{ github.token }}
          name: Snapshot ${{ env.GITHUB_REF }}
          tag_name: snapshot-${{ env.GITHUB_REF }}
          body_path: body.txt
          prerelease: false
          fail_on_unmatched_files: true
          files: |
            Exult.exe
            ExultStudio.exe
            ExultTools.exe
            Gimp20Plugin.exe
            Keyring.zip
            SFisland.zip
            Sifixes.zip
            exult-snapshot-signed.apk
            exult-snapshot-debug.apk
      - name: Upload snapshots
        shell: bash
        if: ${{ (env.HAVE_WINDOWS_SNAPSHOT == 'true' && env.HAVE_ANDROID_SNAPSHOT == 'true')}}
        run: |
          cd $GITHUB_WORKSPACE
          (for ii in $(seq 1 5); do scp -o BatchMode=yes -o PasswordAuthentication=no -i ~/.ssh/id_ed25519 *.exe *.zip *.apk ${{ secrets.SCP_HOST }}:${{ secrets.PROJECT_HOME }}/htdocs/snapshots/ && exit 0 || true; done; exit 255)
      - name: Delete artifacts
        uses: geekyeggo/delete-artifact@v5
        with:
          name: |
            windows*
            android*
          failOnError: false
