name: Deploy to FTP

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    container:
      image: node:20

    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Inject commit SHA
        run: |
          echo "${{ env.GITHUB_SHA }}" > version.sha

      - name: Create latest.zip
        run: |
          set -e
          # ensure a small staging dir for upload (absolute path so later cd won't affect it)
          mkdir -p "$GITHUB_WORKSPACE/dist/"
          # install zip utility (container may not have it)
          apt-get update && apt-get install -y zip
          # create latest.zip containing the assets directory (so zip root contains 'assets/')
          cd app/src/main
          zip -r "$GITHUB_WORKSPACE/latest.zip" assets
          # move zip into the workspace dist so the FTP action can upload just that file
          mv "$GITHUB_WORKSPACE/latest.zip" "$GITHUB_WORKSPACE/dist/"

      - name: Upload latest.zip via FTP to releases
        uses: SamKirkland/FTP-Deploy-Action@v4.3.4
        with:
          server: ${{ secrets.FTP_HOST }}
          username: ${{ secrets.FTP_USER }}
          password: ${{ secrets.FTP_PASS }}
          local-dir: "$GITHUB_WORKSPACE/dist/"
          server-dir: /releases/

      - name: Upload via FTP
        uses: SamKirkland/FTP-Deploy-Action@v4.3.4
        with:
          server: ${{ secrets.FTP_HOST }}
          username: ${{ secrets.FTP_USER }}
          password: ${{ secrets.FTP_PASS }}
          local-dir: app/src/main/assets/
          server-dir: /

