name: "deploy"

on:
  push:
    branches:
      - main


jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      svelte: ${{steps.svelte.outputs.src}}
      tauri: ${{steps.tauri.outputs.src}}
    steps:
    - uses: actions/checkout@v3
    - uses: dorny/paths-filter@v2
      id: svelte
      with:
        filters: |
          src:
            - 'modules/gui/src/**'
            - 'modules/ui/**'
    - uses: dorny/paths-filter@v2
      id: tauri
      with:
        filters: |
          src:
            - 'modules/gui/src-tauri/**'
            - 'modules/gui/src/**'
            - 'modules/ui/src/**'
  build_svelte:
    needs: changes
    if: needs.changes.outputs.svelte == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          persist-credentials: false
      - name: build tauri for Linux
        if: startsWith(matrix.platform.name, 'linux')
        uses: docker://getneil/tea-builder:latest

  build_tauri:
    needs: changes
    if: needs.changes.outputs.tauri == 'true'
    runs-on: ${{ matrix.platform.os }}
    strategy:
      matrix:
        platform:
          - os: macos-11
            name: darwin+x86-64
          - os: ubuntu-latest
            name: linux+x86-64
          - os: [self-hosted, macOS, ARM64]
            name: darwin+aarch64
          # - os: [self-hosted, linux, ARM64]
          #   name: linux+aarch64
    container: ${{ matrix.platform.container }}
    steps:
    - uses: actions/checkout@v3

    - name: build tauri for MacOS
      # uses: teaxyz/setup@v0
      if: startsWith(matrix.platform.name, 'darwin')
      # FROM: https://tauri.app/v1/guides/distribution/sign-macos
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        ENABLE_CODE_SIGNING: ${{ secrets.GUI_APPLE_CERTIFICATE }}
        APPLE_CERTIFICATE: ${{ secrets.GUI_APPLE_CERTIFICATE }}
        APPLE_CERTIFICATE_PASSWORD: ${{ secrets.GUI_APPLE_CERTIFICATE_PASSWORD }}
        APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY }}
        # todo: uncomment when m1 with latest xcode is deployed
        # this only helps in notarization
        # APPLE_ID: ${{ secrets.APPLE_ID }}
        # APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
      run: |
        sh <(curl https://tea.xyz) -ES xc build

    - name: build tauri for Linux
      # uses: teaxyz/setup@v0
      if: startsWith(matrix.platform.name, 'linux')
      # TODO: https://tauri.app/v1/guides/distribution/sign-linux
      uses: docker://getneil/tea-builder:latest

    - name: Get current unix ts - seconds
      id: date
      run: echo "unix_seconds=$(date +'%s')" >> $GITHUB_OUTPUT
    - 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: build platform output
      id: build_platform
      env:
        platform: ${{ matrix.platform.name }}
      run: |
        BUILD_PLATFORM=$(echo $platform | sed -e "s/darwin+//g" | sed -e "s/linux+//g")
        EXTENSION=dmg
        [[ $BUILD_PLATFORM = "x86-64" ]] && BUILD_PLATFORM="x64" || BUILD_PLATFORM=$BUILD_PLATFORM
        [[ $platform = "linux+x86-64" ]] && BUILD_PLATFORM="amd64" || BUILD_PLATFORM=$BUILD_PLATFORM
        [[ $platform = "linux+x86-64" ]] && EXTENSION="deb"
        echo "build_platform=$BUILD_PLATFORM" >> $GITHUB_OUTPUT
        echo "extension=$EXTENSION" >> $GITHUB_OUTPUT
  
    - name: zip .app for MacOS
      if: startsWith(matrix.platform.name, 'darwin')
      run: |
        cd ./modules/gui/src-tauri/target/release/bundle/macos/ && zip -r tea.zip tea.app

    - name:  cp package images from prod to gui bucket
      env:
        platform: ${{ steps.build_platform.outputs.build_platform }}
        build_platform: ${{ matrix.platform.name }}
        extension: ${{steps.build_platform.outputs.extension}}
      run: |
        aws s3 cp "./modules/gui/src-tauri/target/release/bundle/$extension/tea_0.1.0_$platform.$extension" \
          "s3://preview.gui.tea.xyz/release/tea_${{ steps.date.outputs.unix_seconds }}_$platform.$extension"

    - name: cp package zip for MacOS
      if: startsWith(matrix.platform.name, 'darwin')
      env:
        platform: ${{ steps.build_platform.outputs.build_platform }}
        build_platform: ${{ matrix.platform.name }}
      run: |
        aws s3 cp ./modules/gui/src-tauri/target/release/bundle/macos/tea.zip "s3://preview.gui.tea.xyz/release/tea_${{ steps.date.outputs.unix_seconds }}_$platform.zip"
    - uses: actions/setup-node@v3
      with:
        node-version: 18
    - name: Slack Notification
      run: ./.github/notify-slack.js
      env:
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        PLATFORM: ${{ matrix.platform.name }}
        EXT: ${{ steps.build_platform.outputs.extension }}
        DOWNLOAD_URL:  http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/release/tea_${{ steps.date.outputs.unix_seconds }}_${{ steps.build_platform.outputs.build_platform }}.${{ steps.build_platform.outputs.extension }}

    - name: Slack Notification for .app Mac
      run: ./.github/notify-slack.js
      if: startsWith(matrix.platform.name, 'darwin')
      env:
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
        PLATFORM: ${{ matrix.platform.name }}
        EXT: .zip(.app)
        DOWNLOAD_URL:  http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/release/tea_${{ steps.date.outputs.unix_seconds }}_${{ steps.build_platform.outputs.build_platform }}.zip
