on:
  push:
    tags:
      - 'v*.*.*'

jobs:
  build_release_tauri:
    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 platform output
      id: build_platform
      env:
        platform: ${{ matrix.platform.name }}
      run: |
        EXTENSION=dmg
        BUILD_PLATFORM=$(echo $platform | sed -e "s/darwin+//g" | sed -e "s/linux+//g")
        [[ $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: build tauri for MacOS
      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

    # update and replace latest release bin in s3
    - name: Set tag
      id: tag
      run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT

    - name: publish release 
      env:
        platform: ${{ steps.build_platform.outputs.build_platform }}
        extension: ${{ steps.build_platform.outputs.extension }}
        tag: ${{ steps.tag.outputs.tag }}
      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_gui_latest_$platform.$extension"
        aws s3 cp "./modules/gui/src-tauri/target/release/bundle/$extension/tea_0.1.0_$platform.$extension" \
          "s3://preview.gui.tea.xyz/release/tea_gui_${{ steps.tag.outputs.tag }}_$platform.$extension"
  
    - 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: publish .zip(.app) for MacOS
      if: startsWith(matrix.platform.name, 'darwin')
      env:
        platform: ${{ steps.build_platform.outputs.build_platform }}
        extension: ${{ steps.build_platform.outputs.extension }}
        tag: ${{ steps.tag.outputs.tag }}
      run: |
        aws s3 cp "./modules/gui/src-tauri/target/release/bundle/macos/tea.zip" \
          "s3://preview.gui.tea.xyz/release/tea_gui_latest_$platform.zip"
        aws s3 cp "./modules/gui/src-tauri/target/release/bundle/macos/tea.zip" \
          "s3://preview.gui.tea.xyz/release/tea_gui_${{ steps.tag.outputs.tag }}_$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 }}
        VERSION: ${{steps.tag.outputs.tag}}
        EXT: ${{ steps.build_platform.outputs.extension }}
        DOWNLOAD_URL:  http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/release/tea_gui_${{steps.tag.outputs.tag}}_${{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 }}
        VERSION: ${{steps.tag.outputs.tag}}
        EXT: .zip(.app)
        DOWNLOAD_URL:  http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/release/tea_gui_${{steps.tag.outputs.tag}}_${{steps.build_platform.outputs.build_platform}}.zip
