#!/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"

echo "快速部署到 NAS..."

# 使用rsync传输pnpm-lock.yaml（如果存在）
if [ -f "pnpm-lock.yaml" ]; then
    echo "传输 pnpm-lock.yaml..."
    expect << EOF
set timeout 60
spawn rsync -avz -e "ssh -o KexAlgorithms=+diffie-hellman-group1-sha1 -o Ciphers=+aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc" pnpm-lock.yaml $NAS_USER@$NAS_IP:${PROJECT_DIR}/
expect {
    "password:" {
        send "$NAS_PASSWORD\r"
    }
}
expect eof
EOF
fi

# 停止、构建、启动
expect << 'DEPLOY_SCRIPT'
set timeout 1800
spawn ssh -t -o KexAlgorithms=+diffie-hellman-group1-sha1 -o Ciphers=+aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc fnvtk@192.168.2.201 bash -c "cd /volume1/docker/soul-book && pwd && ls -la docker-compose.yml && sudo /volume1/@appstore/ContainerManager/usr/bin/docker-compose -f /volume1/docker/soul-book/docker-compose.yml down 2>/dev/null; sudo /volume1/@appstore/ContainerManager/usr/bin/docker-compose -f /volume1/docker/soul-book/docker-compose.yml build --no-cache && sudo /volume1/@appstore/ContainerManager/usr/bin/docker-compose -f /volume1/docker/soul-book/docker-compose.yml up -d && sleep 10 && sudo /volume1/@appstore/ContainerManager/usr/bin/docker ps | grep soul"
expect {
    "password:" {
        send "Zhiqun1984\r"
        exp_continue
    }
    "Password:" {
        send "Zhiqun1984\r"
        exp_continue
    }
    timeout {
        puts "部署超时，但可能仍在进行中"
    }
}
expect eof
DEPLOY_SCRIPT

echo ""
echo "部署完成！访问: http://192.168.2.201:3000"
