#!/bin/bash
# =============================================
# Print Service 启动脚本
# =============================================

set -e

SERVICE_NAME="print-service"
SERVICE_PORT="8089"
APP_DIR="/opt/erp/${SERVICE_NAME}"
JAR_FILE="${APP_DIR}/${SERVICE_NAME}.jar"
LOG_FILE="${APP_DIR}/logs/${SERVICE_NAME}.log"
PID_FILE="${APP_DIR}/${SERVICE_NAME}.pid"

# 环境配置
export DB_HOST=${DB_HOST:-"111.229.80.149"}
export DB_PORT=${DB_PORT:-"3306"}
export DB_NAME=${DB_NAME:-"erp_db"}
export DB_USER=${DB_USER:-"root"}
export DB_PASSWORD=${DB_PASSWORD:-"nihao588+"}
export REDIS_HOST=${REDIS_HOST:-"111.229.80.149"}
export REDIS_PORT=${REDIS_PORT:-"6379"}
export REDIS_PASSWORD=${REDIS_PASSWORD:-"Y(@r5tGk9$Lp2"}
export NACOS_HOST=${NACOS_HOST:-"111.229.80.149"}
export NACOS_PORT=${NACOS_PORT:-"8848"}
export NACOS_NAMESPACE=${NACOS_NAMESPACE:-"public"}
export SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE:-"dev"}
export PRINT_SERVICE_PORT=${PRINT_SERVICE_PORT:-"8089"}

# 创建目录
mkdir -p ${APP_DIR}/logs

# 下载JAR(如果不存在)
if [ ! -f "${JAR_FILE}" ]; then
    echo "正在下载 ${SERVICE_NAME}.jar..."
    # 替换为实际仓库地址
    # wget -O ${JAR_FILE} http://nexus.erp.com/repository/releases/com/erp/${SERVICE_NAME}/1.0.0/${SERVICE_NAME}.jar
    echo "JAR文件不存在，请先构建: mvn clean package -DskipTests"
    exit 1
fi

# 检查端口
check_port() {
    if netstat -tuln | grep -q ":${SERVICE_PORT} "; then
        echo "错误: 端口 ${SERVICE_PORT} 已被占用"
        exit 1
    fi
}

# 启动服务
start() {
    check_port
    echo "启动 ${SERVICE_NAME}..."
    
    nohup java -Xms256m -Xmx512m -XX:+UseG1GC \
        -jar ${JAR_FILE} \
        --spring.profiles.active=${SPRING_PROFILES_ACTIVE} \
        --server.port=${SERVICE_PORT} \
        > ${LOG_FILE} 2>&1 &
    
    echo $! > ${PID_FILE}
    echo "${SERVICE_NAME} 已启动, PID: $(cat ${PID_FILE})"
    
    # 等待启动
    sleep 5
    
    # 检查健康状态
    if curl -sf http://localhost:${SERVICE_PORT}/actuator/health > /dev/null; then
        echo "${SERVICE_NAME} 启动成功!"
    else
        echo "${SERVICE_NAME} 启动中，请检查日志: ${LOG_FILE}"
    fi
}

# 停止服务
stop() {
    if [ -f ${PID_FILE} ]; then
        PID=$(cat ${PID_FILE})
        echo "停止 ${SERVICE_NAME} (PID: ${PID})..."
        kill ${PID} 2>/dev/null || true
        rm -f ${PID_FILE}
        echo "${SERVICE_NAME} 已停止"
    else
        echo "${SERVICE_NAME} 未运行"
    fi
}

# 重启服务
restart() {
    stop
    sleep 2
    start
}

# 查看状态
status() {
    if [ -f ${PID_FILE} ]; then
        PID=$(cat ${PID_FILE})
        if ps -p ${PID} > /dev/null; then
            echo "${SERVICE_NAME} 正在运行 (PID: ${PID})"
        else
            echo "${SERVICE_NAME} 未运行 (PID文件存在但进程已退出)"
        fi
    else
        echo "${SERVICE_NAME} 未运行"
    fi
}

# 查看日志
logs() {
    if [ -f ${LOG_FILE} ]; then
        tail -f ${LOG_FILE}
    else
        echo "日志文件不存在: ${LOG_FILE}"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    logs)
        logs
        ;;
    *)
        echo "用法: $0 {start|stop|restart|status|logs}"
        exit 1
        ;;
esac

exit 0
