name: Build and Deploy

on:
  push:
    branches:
      - main
    tags:
      - 'v*'
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown

      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Mark as development version
        if: github.ref == 'refs/heads/main'
        run: |
          sed -i 's/^version = "\(.*\)"/version = "\1-dev+'"${GITHUB_SHA:0:7}"'"/' Cargo.toml

      - name: Build
        run: ./build.sh

      - name: Deploy to root (main branch)
        if: github.ref == 'refs/heads/main'
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./deploy
          keep_files: true
          cname: wasm.onerom.org

      - name: Extract version from tag
        if: startsWith(github.ref, 'refs/tags/v')
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Deploy to version directory (tag)
        if: startsWith(github.ref, 'refs/tags/v')
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./deploy
          destination_dir: releases/${{ steps.version.outputs.VERSION }}
          keep_files: true

      - name: Checkout gh-pages to generate releases index
        if: startsWith(github.ref, 'refs/tags/v')
        uses: actions/checkout@v4
        with:
          ref: gh-pages
          path: gh-pages-branch

      - name: Generate releases index
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          source scripts/html-utils.sh
          generate_index "gh-pages-branch/releases" "One ROM Releases"
          mkdir -p releases-listing/releases
          mv gh-pages-branch/releases/index.html releases-listing/releases/

      - name: Deploy releases index to root
        if: startsWith(github.ref, 'refs/tags/v')
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./releases-listing
          keep_files: true