#!/bin/bash
# Set up the Slack bot on the SAME box as vLLM + OpenHands (run gpu-setup.sh first).
# The bot launches OpenHands locally, which talks to vLLM on 127.0.0.1 — so it must be
# co-located with them. Usage (root): cd /srv/site-clones/slackbot && bash setup.sh
set -e

echo "=== 1. System packages ==="
apt update && apt install -y python3 python3-venv python3-pip curl git wget

echo "=== 2. Create directory structure ==="
mkdir -p /srv/slackbot   # holds .env only — secrets stay outside git

echo "=== 3. Python venv + deps ==="
cd /srv/site-clones/slackbot
python3 -m venv venv
# Activate before installing.
source venv/bin/activate
pip install -r requirements.txt
deactivate

echo "=== 4. Set up .env ==="
if [ ! -f /srv/slackbot/.env ]; then
    cp /srv/site-clones/slackbot/.env.example /srv/slackbot/.env
    echo ""
    echo ">>> Edit /srv/slackbot/.env: add SLACK_BOT_TOKEN, SLACK_APP_TOKEN, and set"
    echo "    OPENHANDS_PYTHON=/opt/openhands/venv/bin/python"
fi

echo "=== 5. Install systemd service ==="
cp /srv/site-clones/slackbot/clonebot.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable clonebot

cat <<'EOF'

=== Done! Next steps ===
  1. Confirm gpu-setup.sh finished and vLLM answers: curl http://127.0.0.1:8000/v1/models
  2. Edit /srv/slackbot/.env  (Slack tokens + OPENHANDS_PYTHON) BEFORE starting the service.
  3. Start: sudo systemctl start clonebot
  4. Logs:  journalctl -u clonebot -f
EOF
