name: "deploy"

on:
  push:
    branches:
      - main

jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      desktop: ${{steps.desktop.outputs.src}}
      preview_folder: ${{steps.preview.outputs.folder}}
    steps:
    - uses: actions/checkout@v3
    - uses: dorny/paths-filter@v2
      id: desktop
      with:
        filters: |
          src:
            - 'modules/desktop/**'
            - 'modules/ui/**'
            - '.github/**'
    - name: get s3 preview folder
      id: preview
      run: echo "folder=${{ github.event.number }}-merge" >> $GITHUB_OUTPUT

  build_desktop:
    needs: changes
    # if: needs.changes.outputs.desktop == 'true'
    strategy:
      matrix:
        platform:
          # X86+64 is built with ARM64 also
          - darwin+aarch64
    uses: ./.github/workflows/build-sign-notarize.yml
    with:
      platform: ${{ matrix.platform }}
      s3-prefix: main
      debug: 1
      sync-translation: 1
    secrets: inherit
  upload:
    needs: [build_desktop]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform:
          # - linux+x86-64
          - darwin+aarch64
          # - linux+aarch64
    steps:
    - uses: actions/checkout@v3
    - uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1
    - name: s3 artifact download
      env:
        S3_INSTALLER_KEY: ${{ needs.build_desktop.outputs.s3-custom-notarized-installers-key }}
      run: aws s3 cp $S3_INSTALLER_KEY dist.tgz

    - run: |
        mkdir dist
        tar xzf dist.tgz -C dist

    - name: get installer filenames of arm64 and x86+64
      id: app_files
      working-directory: ./dist
      run: |
        ARM64_ZIP=$(ls | grep -Ev blockmap | grep arm64-mac.zip)
        X86_ZIP=$(ls | grep -Ev blockmap | grep -Ev arm64 | grep mac.zip)
        ARM64_DMG=$(ls | grep -Ev blockmap | grep arm64.dmg)
        X86_DMG=$(ls | grep -Ev blockmap | grep -Ev arm64 | grep dmg)
        echo zip_arm64=$ARM64_ZIP >> $GITHUB_OUTPUT
        echo zip_x86=$X86_ZIP >> $GITHUB_OUTPUT
        echo dmg_arm64=$ARM64_DMG >> $GITHUB_OUTPUT
        echo dmg_x86=$X86_DMG >> $GITHUB_OUTPUT
  
    - name: Get current unix ts - seconds
      id: date
      run: echo "unix_seconds=$(date +'%s')" >> $GITHUB_OUTPUT

    - name: build platform output
      id: build_platform
      env:
        platform: ${{ matrix.platform }}
      run: |
        BUILD_PLATFORM=$(echo $platform | sed -e "s/darwin+//g" | sed -e "s/linux+//g")
        EXTENSION=dmg
        case $platform in
          "linux+x86-64")
            BUILD_PLATFORM="amd64"
            EXTENSION="deb"
            ;;
          "linux+aarch64")
            BUILD_PLATFORM="aarch64"
            EXTENSION="deb"
            ;;
          "darwin+aarch64")
            BUILD_PLATFORM="aarch64"
            EXTENSION="dmg"
            ;;
          "darwin+x86-64")
            BUILD_PLATFORM="x64"
            EXTENSION="dmg"
            ;;
          *)
            echo "Unknown platform $platform"
            exit 1
            ;;
        esac
        echo "build_platform=$BUILD_PLATFORM" >> $GITHUB_OUTPUT
        echo "extension=$EXTENSION" >> $GITHUB_OUTPUT

    - name:  cp package images from prod to gui bucket
      env:
        arm64: ${{ steps.app_files.outputs.dmg_arm64 }}
        x86: ${{ steps.app_files.outputs.dmg_x86 }}
      run: |
        aws s3 cp \
          "dist/$arm64" \
          "s3://preview.gui.tea.xyz/main/$arm64"
        aws s3 cp \
          "dist/$x86" \
          "s3://preview.gui.tea.xyz/main/$x86"

    - uses: actions/setup-node@v3
      with:
        node-version: 18

    - name: Slack Notification ARM64
      run: ./.github/notify-slack.js
      env:
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        PLATFORM: darwin-aarch64
        EXT: dmg
        DOWNLOAD_URL:  http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/main/${{ steps.app_files.outputs.dmg_arm64 }}

    - name: Slack Notification X86
      run: ./.github/notify-slack.js
      env:
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        PLATFORM: darwin-x86+64
        EXT: dmg
        DOWNLOAD_URL:  http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/main/${{ steps.app_files.outputs.dmg_x86 }}
