#!/bin/bash
#
# 创建新用户脚本 - NAS sanguo_vnpy 部署
# 用法: ./create-user.sh <username> <user-id>
# 示例: ./create-user.sh jiangwei 7
#

set -e

# 配置
BASE_DIR="/mnt/nas-volume/sanguo_vnpy"
TEMPLATE_DIR="/mnt/nas-volume/sanguo_vnpy/jiangwei-platform/deploy/nas/templates"
PORT_ALLOCATION="/mnt/nas-volume/sanguo_vnpy/jiangwei-platform/deploy/nas/port-allocation.md"

# 参数检查
if [ $# -ne 2 ]; then
    echo "用法: $0 <username> <user-id>"
    echo "示例: $0 jiangwei 7"
    exit 1
fi

USERNAME=$1
USER_ID=$2

# 计算端口
RPC_REQUEST_PORT=$((2000 + USER_ID * 10))
RPC_SUBSCRIBE_PORT=$((RPC_REQUEST_PORT + 1))
WEB_PORT=$((8000 + USER_ID))
NGINX_PATH="/$USERNAME/"

echo "=========================================="
echo "创建用户: $USERNAME"
echo "用户编号: $USER_ID"
echo "RPC请求端口: $RPC_REQUEST_PORT"
echo "RPC订阅端口: $RPC_SUBSCRIBE_PORT"
echo "Web端口: $WEB_PORT"
echo "Nginx路径: $NGINX_PATH"
echo "=========================================="

# 创建用户目录
USER_DIR="$BASE_DIR/users/$USERNAME"
mkdir -p "$USER_DIR"/{data,logs,strategies}
echo "目录创建完成: $USER_DIR"

# 生成启动脚本
sed \
  -e "s/{{username}}/$USERNAME/g" \
  -e "s/{{rpc_request_port}}/$RPC_REQUEST_PORT/g" \
  -e "s/{{rpc_subscribe_port}}/$RPC_SUBSCRIBE_PORT/g" \
  "$TEMPLATE_DIR/start_trade.py.template" > "$USER_DIR/start_trade.py"

sed \
  -e "s/{{username}}/$USERNAME/g" \
  -e "s/{{rpc_request_port}}/$RPC_REQUEST_PORT/g" \
  -e "s/{{rpc_subscribe_port}}/$RPC_SUBSCRIBE_PORT/g" \
  -e "s/{{web_port}}/$WEB_PORT/g" \
  "$TEMPLATE_DIR/start_web.py.template" > "$USER_DIR/start_web.py"

chmod +x "$USER_DIR/start_trade.py" "$USER_DIR/start_web.py"
echo "启动脚本生成完成"

# 生成systemd服务文件
SYSTEMD_DIR="$BASE_DIR/config/systemd"
mkdir -p "$SYSTEMD_DIR"

sed \
  -e "s/{{username}}/$USERNAME/g" \
  -e "s|/mnt/nas-volume/sanguo_vnpy|$BASE_DIR|g" \
  "$TEMPLATE_DIR/systemd/trade.service.template" > "$SYSTEMD_DIR/sanguo-trade-$USERNAME.service"

sed \
  -e "s/{{username}}/$USERNAME/g" \
  -e "s|/mnt/nas-volume/sanguo_vnpy|$BASE_DIR|g" \
  "$TEMPLATE_DIR/systemd/web.service.template" > "$SYSTEMD_DIR/sanguo-web-$USERNAME.service"

echo "systemd服务文件生成完成: $SYSTEMD_DIR"

# 生成nginx location配置
NGINX_DIR="$BASE_DIR/config/nginx"
mkdir -p "$NGINX_DIR"

sed \
  -e "s/{{username}}/$USERNAME/g" \
  -e "s/{{nginx_path}}/$NGINX_PATH/g" \
  -e "s/{{web_port}}/$WEB_PORT/g" \
  "$TEMPLATE_DIR/nginx/location.conf.template" > "$NGINX_DIR/$USERNAME.conf"

echo "Nginx配置生成完成: $NGINX_DIR/$USERNAME.conf"

# 更新端口分配表
echo "" >> "$PORT_ALLOCATION"
echo "| $USERNAME | $USER_ID | $RPC_REQUEST_PORT | $RPC_SUBSCRIBE_PORT | $WEB_PORT | $NGINX_PATH | 已创建 |" >> "$PORT_ALLOCATION"

echo "=========================================="
echo "用户 $USERNAME 创建完成!"
echo ""
echo "后续步骤:"
echo "1. 将 $SYSTEMD_DIR/sanguo-trade-$USERNAME.service 复制到 /etc/systemd/system/"
echo "2. 将 $SYSTEMD_DIR/sanguo-web-$USERNAME.service 复制到 /etc/systemd/system/"
echo "3. 执行: systemctl daemon-reload"
echo "4. 执行: systemctl enable --now sanguo-trade-$USERNAME.service"
echo "5. 执行: systemctl enable --now sanguo-web-$USERNAME.service"
echo "6. 将 $NGINX_DIR/$USERNAME.conf 包含到Nginx主配置中"
echo "7. 重载Nginx配置"
echo "=========================================="
