#!/usr/bin/env bash
set -euo pipefail
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
ENV_FILE="$APP_DIR/.env"
if [ ! -f "$ENV_FILE" ]; then
  echo "请先执行：cp .env.example .env，然后编辑 .env 填入 API_KEY 等配置"
  exit 1
fi
cat >/etc/systemd/system/kaogongbot.service <<SERVICE
[Unit]
Description=Kaogongbot AI Interview Assistant
After=network.target

[Service]
Type=simple
WorkingDirectory=$APP_DIR
EnvironmentFile=$ENV_FILE
ExecStart=/usr/bin/node $APP_DIR/server.js
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
SERVICE
systemctl daemon-reload
systemctl enable --now kaogongbot
systemctl status kaogongbot --no-pager
