#!/bin/bash
# 重新部署脚本 - 只执行构建和启动步骤

NAS_USER="fnvtk"
NAS_IP="192.168.2.201"
NAS_PASSWORD="Zhiqun1984"
SUDO_PASSWORD="Zhiqun1984"
DOCKER_CMD="/volume1/@appstore/ContainerManager/usr/bin/docker"
DOCKER_COMPOSE_CMD="/volume1/@appstore/ContainerManager/usr/bin/docker-compose"
PROJECT_DIR="/volume1/docker/soul-book"
LOCAL_PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "=========================================="
echo "重新部署 Soul 项目"
echo "=========================================="

# 步骤1: 确保pnpm-lock.yaml已传输
echo "[步骤1] 传输pnpm-lock.yaml..."
expect << EOF
set timeout 60
spawn scp -o KexAlgorithms=+diffie-hellman-group1-sha1 -o Ciphers=+aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc "$LOCAL_PROJECT_DIR/pnpm-lock.yaml" $NAS_USER@$NAS_IP:${PROJECT_DIR}/
expect {
    "password:" {
        send "$NAS_PASSWORD\r"
    }
    timeout {
        puts "传输超时"
        exit 1
    }
}
expect eof
EOF

# 步骤2: 停止旧容器
echo "[步骤2] 停止旧容器..."
expect << EOF
set timeout 60
spawn ssh -t -o KexAlgorithms=+diffie-hellman-group1-sha1 -o Ciphers=+aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc $NAS_USER@$NAS_IP "cd ${PROJECT_DIR} && sudo $DOCKER_COMPOSE_CMD down 2>/dev/null || true && sudo $DOCKER_CMD rm -f soul_book_app 2>/dev/null || true && echo '清理完成'"
expect {
    "password:" {
        send "$NAS_PASSWORD\r"
        exp_continue
    }
    "Password:" {
        send "$SUDO_PASSWORD\r"
        exp_continue
    }
    "清理完成" {
        puts "清理成功"
    }
    timeout {
        puts "清理超时，继续执行"
    }
}
expect eof
EOF

# 步骤3: 构建镜像
echo "[步骤3] 构建Docker镜像（这可能需要5-10分钟）..."
expect << EOF
set timeout 1200
spawn ssh -t -o KexAlgorithms=+diffie-hellman-group1-sha1 -o Ciphers=+aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc $NAS_USER@$NAS_IP "cd ${PROJECT_DIR} && sudo $DOCKER_COMPOSE_CMD build --no-cache 2>&1 | tail -50"
expect {
    "password:" {
        send "$NAS_PASSWORD\r"
        exp_continue
    }
    "Password:" {
        send "$SUDO_PASSWORD\r"
        exp_continue
    }
    timeout {
        puts "构建超时，但继续执行"
    }
}
expect eof
EOF

# 步骤4: 启动容器
echo "[步骤4] 启动容器..."
expect << EOF
set timeout 120
spawn ssh -t -o KexAlgorithms=+diffie-hellman-group1-sha1 -o Ciphers=+aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc $NAS_USER@$NAS_IP "cd ${PROJECT_DIR} && sudo $DOCKER_COMPOSE_CMD up -d && echo '容器启动完成'"
expect {
    "password:" {
        send "$NAS_PASSWORD\r"
        exp_continue
    }
    "Password:" {
        send "$SUDO_PASSWORD\r"
        exp_continue
    }
    "容器启动完成" {
        puts "容器启动成功"
    }
    timeout {
        puts "启动超时"
    }
}
expect eof
EOF

# 等待服务启动
echo "等待服务启动（30秒）..."
sleep 30

# 步骤5: 检查状态
echo "[步骤5] 检查容器状态..."
expect << EOF
set timeout 30
spawn ssh -t -o KexAlgorithms=+diffie-hellman-group1-sha1 -o Ciphers=+aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc $NAS_USER@$NAS_IP "sudo $DOCKER_CMD ps --filter name=soul_book_app --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' && echo '---' && sudo $DOCKER_CMD logs --tail 30 soul_book_app 2>&1 | tail -20"
expect {
    "password:" {
        send "$NAS_PASSWORD\r"
        exp_continue
    }
    "Password:" {
        send "$SUDO_PASSWORD\r"
        exp_continue
    }
}
expect eof
EOF

echo ""
echo "=========================================="
echo "部署完成！访问地址: http://${NAS_IP}:3000"
echo "=========================================="
