name: Publish S3

on:
  workflow_call:
    inputs:
      build-flavor:
        required: true
        type: string
      build-number:
        required: true
        type: string
      build-title:
        required: true
        type: string

    secrets:
      S3_ACCESS_KEY:
        required: true
      S3_BUCKET:
        required: true
      S3_ENDPOINT:
        required: true
      S3_KEY_ID:
        required: true
      S3_REGION:
        required: true

jobs:
  s3:
    name: S3
    runs-on: ubuntu-latest
    steps:
      - name: Setup Environment
        uses: JagandeepBrar/LunaSea/.github/actions/prepare_for_deployment@master
        with:
          channel: s3

      - name: Download Android App Package
        uses: actions/download-artifact@v3
        with:
          name: android-app-package
          path: ${{ github.workspace }}/output

      - name: Download iOS App Package
        uses: actions/download-artifact@v3
        with:
          name: ios-appstore-package
          path: ${{ github.workspace }}/output

      - name: Download Linux Snap
        uses: actions/download-artifact@v3
        with:
          name: linux-snapcraft
          path: ${{ github.workspace }}/output

      - name: Download macOS App Package
        uses: actions/download-artifact@v3
        with:
          name: macos-app-package
          path: ${{ github.workspace }}/output

      - name: Download macOS Disk Image
        uses: actions/download-artifact@v3
        with:
          name: macos-disk-image
          path: ${{ github.workspace }}/output

      - name: Download Web Archive
        uses: actions/download-artifact@v3
        with:
          name: web-archive
          path: ${{ github.workspace }}/output

      - name: Download Windows App Package
        uses: actions/download-artifact@v3
        with:
          name: windows-app-package
          path: ${{ github.workspace }}/output

      - name: Download Windows MSIX Installer
        uses: actions/download-artifact@v3
        with:
          name: windows-msix-installer
          path: ${{ github.workspace }}/output

      - name: Create Version File
        working-directory: ${{ github.workspace }}/output
        run: |
          echo ${{ inputs.build-number }} > VERSION.txt
          echo ${{ inputs.build-number }} > VERSION_ANDROID.txt
          echo ${{ inputs.build-number }} > VERSION_IOS.txt
          echo ${{ inputs.build-number }} > VERSION_LINUX.txt
          echo ${{ inputs.build-number }} > VERSION_MACOS.txt
          echo ${{ inputs.build-number }} > VERSION_WEB.txt
          echo ${{ inputs.build-number }} > VERSION_WINDOWS.txt

      - name: Upload to S3 Bucket (Build Title)
        uses: jakejarvis/s3-sync-action@v0.5.1
        with:
          args: --acl public-read --follow-symlinks 
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.S3_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }}
          AWS_S3_BUCKET: ${{ secrets.S3_BUCKET }}
          AWS_S3_ENDPOINT: ${{ secrets.S3_ENDPOINT }}
          AWS_REGION: ${{ secrets.S3_REGION }}
          SOURCE_DIR: ${{ github.workspace }}/output
          DEST_DIR: ${{ inputs.build-title }}

      - name: Upload to S3 Bucket (Latest)
        uses: jakejarvis/s3-sync-action@v0.5.1
        with:
          args: --acl public-read --follow-symlinks 
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.S3_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }}
          AWS_S3_BUCKET: ${{ secrets.S3_BUCKET }}
          AWS_S3_ENDPOINT: ${{ secrets.S3_ENDPOINT }}
          AWS_REGION: ${{ secrets.S3_REGION }}
          SOURCE_DIR: ${{ github.workspace }}/output
          DEST_DIR: latest/${{ inputs.build-flavor }}
