name: Build & deploy documentation on rs.delta.chat, c.delta.chat, py.delta.chat and cffi.delta.chat

on:
  push:
    branches:
      - main

permissions: {}

jobs:
  build-rs:
    runs-on: ubuntu-latest
    environment:
      name: rs.delta.chat
      url: https://rs.delta.chat/

    steps:
      - uses: actions/checkout@v6
        with:
          show-progress: false
          persist-credentials: false
      - name: Build the documentation with cargo
        run: |
          cargo doc --package deltachat --no-deps --document-private-items
      - name: Upload to rs.delta.chat
        run: |
          mkdir -p "$HOME/.ssh"
          echo "${{ secrets.RS_DOCS_SSH_KEY }}" > "$HOME/.ssh/key"
          chmod 600 "$HOME/.ssh/key"
          rsync -avzh -e "ssh -i $HOME/.ssh/key -o StrictHostKeyChecking=no" $GITHUB_WORKSPACE/target/doc "${{ secrets.RS_DOCS_SSH_USER }}@rs.delta.chat:/var/www/html/rs.delta.chat/"

  build-python:
    runs-on: ubuntu-latest
    environment:
      name: py.delta.chat
      url: https://py.delta.chat/

    steps:
      - uses: actions/checkout@v6
        with:
          show-progress: false
          persist-credentials: false
          fetch-depth: 0 # Fetch history to calculate VCS version number.
      - uses: cachix/install-nix-action@2126ae7fc54c9df00dd18f7f18754393182c73cd # v31.9.1
      - name: Build Python documentation
        run: nix build .#python-docs
      - name: Upload to py.delta.chat
        run: |
          mkdir -p "$HOME/.ssh"
          echo "${{ secrets.PY_DOCS_SSH_KEY }}" > "$HOME/.ssh/key"
          chmod 600 "$HOME/.ssh/key"
          rsync -avzh --delete -e "ssh -i $HOME/.ssh/key -o StrictHostKeyChecking=no" $GITHUB_WORKSPACE/result/html/ "${{ secrets.PY_DOCS_SSH_USER }}@py.delta.chat:/var/www/html/py.delta.chat"

  build-c:
    runs-on: ubuntu-latest
    environment:
      name: c.delta.chat
      url: https://c.delta.chat/

    steps:
      - uses: actions/checkout@v6
        with:
          show-progress: false
          persist-credentials: false
          fetch-depth: 0 # Fetch history to calculate VCS version number.
      - uses: cachix/install-nix-action@2126ae7fc54c9df00dd18f7f18754393182c73cd # v31.9.1
      - name: Build C documentation
        run: nix build .#docs
      - name: Upload to c.delta.chat
        run: |
          mkdir -p "$HOME/.ssh"
          echo "${{ secrets.C_DOCS_SSH_KEY }}" > "$HOME/.ssh/key"
          chmod 600 "$HOME/.ssh/key"
          rsync -avzh --delete -e "ssh -i $HOME/.ssh/key -o StrictHostKeyChecking=no" $GITHUB_WORKSPACE/result/html/ "${{ secrets.C_DOCS_SSH_USER }}@c.delta.chat:/var/www/html/c.delta.chat"

  build-ts:
    runs-on: ubuntu-latest
    environment:
      name: js.jsonrpc.delta.chat
      url: https://js.jsonrpc.delta.chat/

    defaults:
      run:
        working-directory: ./deltachat-jsonrpc/typescript

    steps:
      - uses: actions/checkout@v6
        with:
          show-progress: false
          persist-credentials: false
          fetch-depth: 0 # Fetch history to calculate VCS version number.
      - name: Use Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '18'
      - name: npm install
        run: npm install
      - name: npm run build
        run: npm run build
      - name: Run docs script
        run: npm run docs
      - name: Upload to js.jsonrpc.delta.chat
        run: |
          mkdir -p "$HOME/.ssh"
          echo "${{ secrets.JS_JSONRPC_DOCS_SSH_KEY }}" > "$HOME/.ssh/key"
          chmod 600 "$HOME/.ssh/key"
          rsync -avzh --delete -e "ssh -i $HOME/.ssh/key -o StrictHostKeyChecking=no" $GITHUB_WORKSPACE/deltachat-jsonrpc/typescript/docs/ "${{ secrets.JS_JSONRPC_DOCS_SSH_USER }}@js.jsonrpc.delta.chat:/var/www/html/js.jsonrpc.delta.chat/"

  build-cffi:
    runs-on: ubuntu-latest
    environment:
      name: cffi.delta.chat
      url: https://cffi.delta.chat/

    steps:
      - uses: actions/checkout@v6
        with:
          show-progress: false
          persist-credentials: false
      - name: Build the documentation with cargo
        run: |
          cargo doc --package deltachat_ffi --no-deps
      - name: Upload to cffi.delta.chat
        run: |
          mkdir -p "$HOME/.ssh"
          echo "${{ secrets.CFFI_DOCS_SSH_KEY }}" > "$HOME/.ssh/key"
          chmod 600 "$HOME/.ssh/key"
          rsync -avzh --delete -e "ssh -i $HOME/.ssh/key -o StrictHostKeyChecking=no" $GITHUB_WORKSPACE/target/doc/ "${{ secrets.CFFI_DOCS_SSH_USER }}@delta.chat:/var/www/html/cffi.delta.chat/"
