#!/usr/bin/env bash
# Deploy arXiv monitor script to the VPS
# Run once from local machine: bash scripts/deploy-monitor.sh
set -euo pipefail

DEPLOY_HOST="${DEPLOY_HOST}"
SCRIPTS_DIR="/opt/scripts"

echo "=== Deploying ShieldX arXiv Monitor to the VPS ==="

# 1. Copy monitor script
scp scripts/arxiv-monitor.mjs "${DEPLOY_HOST}:${SCRIPTS_DIR}/arxiv-monitor.mjs"
ssh "$DEPLOY_HOST" "chmod +x ${SCRIPTS_DIR}/arxiv-monitor.mjs"

# 2. Create .env if not exists
ssh "$DEPLOY_HOST" "if [ ! -f ${SCRIPTS_DIR}/.env ]; then
  cat > ${SCRIPTS_DIR}/.env << 'ENVEOF'
# ShieldX Monitor Config
ANTHROPIC_API_KEY=YOUR_KEY_HERE
GITEA_TOKEN=${GITEA_TOKEN}
GITEA_BASE_URL=https://gitea.example.com
GITEA_USER=user
LOG_DIR=/opt/scripts/logs
WORK_DIR=/tmp/shieldx-monitor
ENVEOF
  echo 'Created .env — set ANTHROPIC_API_KEY!'
else
  echo '.env already exists — not overwriting'
fi"

# 3. Add cron if not already set
ssh "$DEPLOY_HOST" "(crontab -l 2>/dev/null | grep -q 'arxiv-monitor') && echo 'Cron already set' || (crontab -l 2>/dev/null; echo '0 6 * * * node /opt/scripts/arxiv-monitor.mjs >> /opt/scripts/logs/arxiv-monitor.log 2>&1') | crontab -"

echo ""
echo "=== Done ==="
echo ""
echo "Next steps on the VPS:"
echo "  1. Set ANTHROPIC_API_KEY in ${SCRIPTS_DIR}/.env"
echo "  2. Test run: node /opt/scripts/arxiv-monitor.mjs"
echo "  3. Check logs: tail -f /opt/scripts/logs/arxiv-monitor.log"
echo "  4. Cron runs daily at 6:00 UTC (8:00 Berlin)"
