# GitHub Actions workflow
# to build `deltachat-rpc-server` binaries
# and upload them to the release.
#
# The workflow is automatically triggered on releases.
# It can also be triggered manually
# to produce binary artifacts for testing.

name: Build deltachat-rpc-server binaries

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  workflow_dispatch:
  release:
    types: [published]

jobs:
  # Build a version statically linked against musl libc
  # to avoid problems with glibc version incompatibility.
  build_linux:
    name: Linux
    strategy:
      fail-fast: false
      matrix:
        arch: [aarch64, armv7l, armv6l, i686, x86_64]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          show-progress: false
      - uses: DeterminateSystems/nix-installer-action@main
      - uses: DeterminateSystems/magic-nix-cache-action@main

      - name: Build deltachat-rpc-server binaries
        run: nix build .#deltachat-rpc-server-${{ matrix.arch }}-linux

      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: deltachat-rpc-server-${{ matrix.arch }}-linux
          path: result/bin/deltachat-rpc-server
          if-no-files-found: error

  build_windows:
    name: Windows
    strategy:
      fail-fast: false
      matrix:
        arch: [win32, win64]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          show-progress: false
      - uses: DeterminateSystems/nix-installer-action@main
      - uses: DeterminateSystems/magic-nix-cache-action@main

      - name: Build deltachat-rpc-server binaries
        run: nix build .#deltachat-rpc-server-${{ matrix.arch }}

      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: deltachat-rpc-server-${{ matrix.arch }}
          path: result/bin/deltachat-rpc-server.exe
          if-no-files-found: error

  build_macos:
    name: macOS
    strategy:
      fail-fast: false
      matrix:
        arch: [x86_64, aarch64]

    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v4
        with:
          show-progress: false

      - name: Setup rust target
        run: rustup target add ${{ matrix.arch }}-apple-darwin

      - name: Build
        run: cargo build --release --package deltachat-rpc-server --target ${{ matrix.arch }}-apple-darwin --features vendored

      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: deltachat-rpc-server-${{ matrix.arch }}-macos
          path: target/${{ matrix.arch }}-apple-darwin/release/deltachat-rpc-server
          if-no-files-found: error

  build_android:
    name: Android
    strategy:
      fail-fast: false
      matrix:
        arch: [arm64-v8a, armeabi-v7a]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          show-progress: false
      - uses: DeterminateSystems/nix-installer-action@main
      - uses: DeterminateSystems/magic-nix-cache-action@main

      - name: Build deltachat-rpc-server binaries
        run: nix build .#deltachat-rpc-server-${{ matrix.arch }}-android

      - name: Upload binary
        uses: actions/upload-artifact@v4
        with:
          name: deltachat-rpc-server-${{ matrix.arch }}-android
          path: result/bin/deltachat-rpc-server
          if-no-files-found: error

  publish:
    name: Build wheels and upload binaries to the release
    needs: ["build_linux", "build_windows", "build_macos"]
    environment:
      name: pypi
      url: https://pypi.org/p/deltachat-rpc-server
    permissions:
      id-token: write
      contents: write
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v4
        with:
          show-progress: false
      - uses: DeterminateSystems/nix-installer-action@main
      - uses: DeterminateSystems/magic-nix-cache-action@main

      - name: Download Linux aarch64 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-aarch64-linux
          path: deltachat-rpc-server-aarch64-linux.d

      - name: Download Linux armv7l binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-armv7l-linux
          path: deltachat-rpc-server-armv7l-linux.d

      - name: Download Linux armv6l binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-armv6l-linux
          path: deltachat-rpc-server-armv6l-linux.d

      - name: Download Linux i686 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-i686-linux
          path: deltachat-rpc-server-i686-linux.d

      - name: Download Linux x86_64 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-x86_64-linux
          path: deltachat-rpc-server-x86_64-linux.d

      - name: Download Win32 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-win32
          path: deltachat-rpc-server-win32.d

      - name: Download Win64 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-win64
          path: deltachat-rpc-server-win64.d

      - name: Download macOS binary for x86_64
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-x86_64-macos
          path: deltachat-rpc-server-x86_64-macos.d

      - name: Download macOS binary for aarch64
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-aarch64-macos
          path: deltachat-rpc-server-aarch64-macos.d

      - name: Download Android binary for arm64-v8a
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-arm64-v8a-android
          path: deltachat-rpc-server-arm64-v8a-android.d

      - name: Download Android binary for armeabi-v7a
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-armeabi-v7a-android
          path: deltachat-rpc-server-armeabi-v7a-android.d

      - name: Create bin/ directory
        run: |
          mkdir -p bin
          mv deltachat-rpc-server-aarch64-linux.d/deltachat-rpc-server bin/deltachat-rpc-server-aarch64-linux
          mv deltachat-rpc-server-armv7l-linux.d/deltachat-rpc-server bin/deltachat-rpc-server-armv7l-linux
          mv deltachat-rpc-server-armv6l-linux.d/deltachat-rpc-server bin/deltachat-rpc-server-armv6l-linux
          mv deltachat-rpc-server-i686-linux.d/deltachat-rpc-server bin/deltachat-rpc-server-i686-linux
          mv deltachat-rpc-server-x86_64-linux.d/deltachat-rpc-server bin/deltachat-rpc-server-x86_64-linux
          mv deltachat-rpc-server-win32.d/deltachat-rpc-server.exe bin/deltachat-rpc-server-win32.exe
          mv deltachat-rpc-server-win64.d/deltachat-rpc-server.exe bin/deltachat-rpc-server-win64.exe
          mv deltachat-rpc-server-x86_64-macos.d/deltachat-rpc-server bin/deltachat-rpc-server-x86_64-macos
          mv deltachat-rpc-server-aarch64-macos.d/deltachat-rpc-server bin/deltachat-rpc-server-aarch64-macos
          mv deltachat-rpc-server-arm64-v8a-android.d/deltachat-rpc-server bin/deltachat-rpc-server-arm64-v8a-android
          mv deltachat-rpc-server-armeabi-v7a-android.d/deltachat-rpc-server bin/deltachat-rpc-server-armeabi-v7a-android

      - name: List binaries
        run: ls -l bin/

      # Python 3.11 is needed for tomllib used in scripts/wheel-rpc-server.py
      - name: Install python 3.12
        uses: actions/setup-python@v5
        with:
          python-version: 3.12

      - name: Install wheel
        run: pip install wheel

      - name: Build deltachat-rpc-server Python wheels and source package
        run: |
          mkdir -p dist
          nix build .#deltachat-rpc-server-x86_64-linux-wheel
          cp result/*.whl dist/
          nix build .#deltachat-rpc-server-armv7l-linux-wheel
          cp result/*.whl dist/
          nix build .#deltachat-rpc-server-armv6l-linux-wheel
          cp result/*.whl dist/
          nix build .#deltachat-rpc-server-aarch64-linux-wheel
          cp result/*.whl dist/
          nix build .#deltachat-rpc-server-i686-linux-wheel
          cp result/*.whl dist/
          nix build .#deltachat-rpc-server-win64-wheel
          cp result/*.whl dist/
          nix build .#deltachat-rpc-server-win32-wheel
          cp result/*.whl dist/
          nix build .#deltachat-rpc-server-source
          cp result/*.tar.gz dist/
          python3 scripts/wheel-rpc-server.py x86_64-darwin bin/deltachat-rpc-server-x86_64-macos
          python3 scripts/wheel-rpc-server.py aarch64-darwin bin/deltachat-rpc-server-aarch64-macos
          mv *.whl dist/

      - name: List artifacts
        run: ls -l dist/

      - name: Upload binaries to the GitHub release
        if: github.event_name == 'release'
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
        run: |
          gh release upload ${{ github.ref_name }} \
            --repo ${{ github.repository }} \
            bin/* dist/*

      - name: Publish deltachat-rpc-client to PyPI
        if: github.event_name == 'release'
        uses: pypa/gh-action-pypi-publish@release/v1

  publish_npm_package:
    name: Build & Publish npm prebuilds and deltachat-rpc-server
    needs: ["build_linux", "build_windows", "build_macos"]
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v4
        with:
          show-progress: false
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11' 

      - name: Download Linux aarch64 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-aarch64-linux
          path: deltachat-rpc-server-aarch64-linux.d

      - name: Download Linux armv7l binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-armv7l-linux
          path: deltachat-rpc-server-armv7l-linux.d

      - name: Download Linux armv6l binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-armv6l-linux
          path: deltachat-rpc-server-armv6l-linux.d

      - name: Download Linux i686 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-i686-linux
          path: deltachat-rpc-server-i686-linux.d

      - name: Download Linux x86_64 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-x86_64-linux
          path: deltachat-rpc-server-x86_64-linux.d

      - name: Download Win32 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-win32
          path: deltachat-rpc-server-win32.d

      - name: Download Win64 binary
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-win64
          path: deltachat-rpc-server-win64.d

      - name: Download macOS binary for x86_64
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-x86_64-macos
          path: deltachat-rpc-server-x86_64-macos.d

      - name: Download macOS binary for aarch64
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-aarch64-macos
          path: deltachat-rpc-server-aarch64-macos.d

      - name: Download Android binary for arm64-v8a
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-arm64-v8a-android
          path: deltachat-rpc-server-arm64-v8a-android.d

      - name: Download Android binary for armeabi-v7a
        uses: actions/download-artifact@v4
        with:
          name: deltachat-rpc-server-armeabi-v7a-android
          path: deltachat-rpc-server-armeabi-v7a-android.d

      - name: make npm packets for prebuilds and `@deltachat/stdio-rpc-server`
        run: |
          cd deltachat-rpc-server/npm-package

          python --version

          python scripts/pack_binary_for_platform.py aarch64-unknown-linux-musl     ../../deltachat-rpc-server-aarch64-linux.d/deltachat-rpc-server
          python scripts/pack_binary_for_platform.py armv7-unknown-linux-musleabihf ../../deltachat-rpc-server-armv7l-linux.d/deltachat-rpc-server 
          python scripts/pack_binary_for_platform.py arm-unknown-linux-musleabihf   ../../deltachat-rpc-server-armv6l-linux.d/deltachat-rpc-server 
          python scripts/pack_binary_for_platform.py i686-unknown-linux-musl        ../../deltachat-rpc-server-i686-linux.d/deltachat-rpc-server 
          python scripts/pack_binary_for_platform.py x86_64-unknown-linux-musl      ../../deltachat-rpc-server-x86_64-linux.d/deltachat-rpc-server
          python scripts/pack_binary_for_platform.py i686-pc-windows-gnu            ../../deltachat-rpc-server-win32.d/deltachat-rpc-server.exe 
          python scripts/pack_binary_for_platform.py x86_64-pc-windows-gnu          ../../deltachat-rpc-server-win64.d/deltachat-rpc-server.exe 
          python scripts/pack_binary_for_platform.py x86_64-apple-darwin            ../../deltachat-rpc-server-x86_64-macos.d/deltachat-rpc-server 
          python scripts/pack_binary_for_platform.py aarch64-apple-darwin           ../../deltachat-rpc-server-aarch64-macos.d/deltachat-rpc-server 
          python scripts/pack_binary_for_platform.py aarch64-linux-android          ../../deltachat-rpc-server-arm64-v8a-android.d/deltachat-rpc-server
          python scripts/pack_binary_for_platform.py armv7-linux-androideabi        ../../deltachat-rpc-server-armeabi-v7a-android.d/deltachat-rpc-server

          ls -lah platform_package

          for platform in ./platform_package/*; do npm pack "$platform"; done
          npm pack    
          ls -lah
      
      - name: Upload to artifacts
        uses: actions/upload-artifact@v4
        with:
          name: deltachat-rpc-server-npm-package
          path: deltachat-rpc-server/npm-package/*.tgz
          if-no-files-found: error

      - name: Upload npm packets to the GitHub release
        if: github.event_name == 'release'
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
        run: |
          gh release upload ${{ github.ref_name }} \
            --repo ${{ github.repository }} \
            *.tgz

      - name: Publish npm packets for prebuilds and `@deltachat/stdio-rpc-server`
        if: github.event_name == 'release'
        run: |
          ls -lah platform_package

          for platform in *.tgz; do npm publish "$platform"; done
