#!/bin/sh
# Joins the dbmoe training and inference network.  Worker source: https://codex.opentransformers.online/dbmoe/code
# Installs into ~/dbmoe-worker (or $DBMOE_DIR).  Ctrl+C stops it; delete that folder to remove it.
# Options go after "sh -s --", for example:  curl -fsSL https://codex.opentransformers.online/dbmoe/join.sh | sh -s -- --threads 4 --hours 1
set -eu
BASE="https://codex.opentransformers.online/dbmoe"
DIR="${DBMOE_DIR:-$HOME/dbmoe-worker}"
mkdir -p "$DIR"
cd "$DIR"
PY="$(command -v python3 || command -v python || true)"
[ -n "$PY" ] || { echo "dbmoe needs Python 3.9 or newer" >&2; exit 1; }
if ! "$PY" -c 'import torch' 2>/dev/null; then
  [ -x venv/bin/python ] || "$PY" -m venv venv
  PY=venv/bin/python
  if ! "$PY" -c 'import torch' 2>/dev/null; then
    echo "Installing PyTorch (CPU build) into $DIR/venv ..."
    "$PY" -m pip install --quiet --upgrade pip
    if [ "$(uname -s)" = Darwin ]; then "$PY" -m pip install --quiet torch; else "$PY" -m pip install --quiet torch --index-url https://download.pytorch.org/whl/cpu; fi
  fi
fi
curl -fsSL "$BASE/code" -o dbmoe.py.new
mv dbmoe.py.new dbmoe.py
exec "$PY" -u dbmoe.py worker --url "$BASE" "$@"
