#!/usr/bin/env bash
# install_systemd.sh — Install saltybot systemd services on Orin
# Run as root: sudo ./systemd/install_systemd.sh
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(dirname "${SCRIPT_DIR}")"
SYSTEMD_DIR="/etc/systemd/system"
DEPLOY_DIR="/opt/saltybot/jetson"

log() { echo "[install_systemd] $*"; }

[[ "$(id -u)" == "0" ]] || { echo "Run as root"; exit 1; }

# Deploy repo to /opt/saltybot/jetson
log "Deploying to ${DEPLOY_DIR}..."
mkdir -p "${DEPLOY_DIR}"
rsync -a --exclude='.git' --exclude='__pycache__' \
    "${REPO_DIR}/" "${DEPLOY_DIR}/"

# Install service files
log "Installing systemd units..."
cp "${SCRIPT_DIR}/saltybot.target"        "${SYSTEMD_DIR}/"
cp "${SCRIPT_DIR}/saltybot-social.service" "${SYSTEMD_DIR}/"

# Reload and enable
systemctl daemon-reload
systemctl enable saltybot.target
systemctl enable saltybot-social.service

log "Services installed. Start with:"
log "  systemctl start saltybot-social"
log "  journalctl -fu saltybot-social"
