name: Make Stable Folder On EmulatorJS CDN
on:
  release:
    types: [published]
  workflow_dispatch:
permissions:
  contents: read
jobs:
  run:
    if: github.repository == 'EmulatorJS/EmulatorJS'
    runs-on: emulatorjs-server
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set Variables
        run: |
          cd /mnt/HDD/public/
          wget -q "https://api.github.com/repos/EmulatorJS/EmulatorJS/releases/latest" -O .tmp.json
          version=$(jq -r '.tag_name' .tmp.json)
          download_url=$(jq -r '.assets[0].browser_download_url' .tmp.json)
          name=$(jq -r '.assets[0].name' .tmp.json)
          old_version=$(jq -r '.github' versions.json)
          v_version="$version"
          if [[ $version == v* ]]; then
              version="${version:1}"
          fi
          rm .tmp.json
          echo "VERSION=$version" >> $GITHUB_ENV
          echo "DOWNLOAD_URL=$download_url" >> $GITHUB_ENV
          echo "NAME=$name" >> $GITHUB_ENV
          echo "OLD_VERSION=$old_version" >> $GITHUB_ENV
          echo "V_VERSION=$v_version" >> $GITHUB_ENV
      - name: Download Stable
        run: |
          cd /mnt/HDD/public/
          mkdir -p ".stable"
          wget -q "$DOWNLOAD_URL" -O ".stable/$NAME"
      - name: Extract Stable
        run: |
          cd /mnt/HDD/public/.stable/
          7z x "$NAME"
      - name: Move Versions
        run: |
          cd /mnt/HDD/public/
          mv ".stable/$NAME" releases/
          mv "$OLD_VERSION" "$VERSION"
          mv stable "$OLD_VERSION"
          mv .stable stable
          jq --arg version "$VERSION" '.github = $version' versions.json | sponge versions.json
      - name: Set Permissions
        run: |
          cd /mnt/HDD/public
          chmod -R 755 stable/
      - name: Update Stable Cores
        run: |
          rsync -a --delete /mnt/HDD/public/stable/data/cores/ /mnt/HDD/public/.EmulatorJS/data/cores/
      - name: Zip Stable
        run: |
          cd /mnt/HDD/public/stable/data/
          zip emulator.min.zip emulator.min.js emulator.min.css
      - name: Update Version
        run: |
          cd /mnt/HDD/public/
          jq --arg old_version "$OLD_VERSION" '.versions += {($old_version): ($old_version + "/")}' versions.json | sponge versions.json
      - name: Clean Up
        run: |
          cd /mnt/HDD/public/
          rm -f "$OLD_VERSION/data/emulator.min.zip"
          rm -rf "$OLD_VERSION/node_modules/"
