set -e
echo "===== 本程序可以一键安装touchgal，不用提前安装任何东西，新买的服务器就能用，CentOS7专用 ====="
yum install -y wget || { echo "wget安装失败，重试..."; yum install -y wget; }

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo/Cak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

yum clean all && yum makecache fast

DB_NAME="touchgal_db"
DB_USER="touchgal_user"
DB_PASS="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)"
BT_PANEL_URL=""
BT_USER=""
BT_PASS=""

echo "===== 1. 安装宝塔面板 ====="
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh ed8484bec --force

# 提取宝塔登录信息
BT_INFO=$(cat /root/btpanel/login.pl)
BT_PANEL_URL=$(echo "$BT_INFO" | grep -o "http://[0-9.:]\+")
BT_USER=$(echo "$BT_INFO" | grep -o "用户名: .\+" | awk '{print $2}')
BT_PASS=$(echo "$BT_INFO" | grep -o "密码: .\+" | awk '{print $2}')

echo "===== 2. 开放所有必要端口 ====="
firewall-cmd --zone=public --add-port=8888/tcp --permanent
firewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload

echo "===== 3. 安装依赖工具（Redis+PostgreSQL+Git） ====="
yum install -y git postgresql-server postgresql-contrib redis
postgresql-setup initdb
systemctl start postgresql
systemctl enable postgresql
systemctl start redis
systemctl enable redis

echo "===== 4. 安装 nvm + musl 版 Node.js ====="
curl -o- https://gitee.com/mirrors/nvm/raw/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install -s https://unofficial-builds.nodejs.org/download/release/v14.21.3/node-v14.21.3-linux-x64-musl.tar.xz
nvm alias default 14.21.3-musl
npm install -g pnpm@6 --registry=https://registry.npmmirror.com

echo "===== 5. 自动创建 PostgreSQL 数据库 ====="
sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';"
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;"
sed -i "s/ident/md5/g" /var/lib/pgsql/data/pg_hba.conf
systemctl restart postgresql

echo "===== 6. 拉取项目代码 ====="
mkdir -p /www/wwwroot/gal.186.baby
cd /www/wwwroot/gal.186.baby
git clone https://github.com/KUN1007/kun-touchgal-next.git .

echo "===== 7. 自动生成 .env 配置 ====="
cat > .env << EOF
PORT=3000
NODE_ENV=production
DATABASE_URL="postgresql://$DB_USER:$DB_PASS@localhost:5432/$DB_NAME?schema=public"
REDIS_URL="redis://localhost:6379"
NEXT_PUBLIC_SITE_URL="https://gal.186.baby"
EOF

echo "===== 8. 安装项目依赖 + 构建 ====="
pnpm install --registry=https://registry.npmmirror.com
pnpm build

echo "===== 9. 安装 pm2 进程守护 ====="
npm install -g pm2
pm2 start npm --name "touchgal" -- run start
pm2 startup
pm2 save

echo "===== 10. 宝塔配置站点+反向代理 ====="
wget -O /usr/bin/bt http://download.bt.cn/install/update6.sh && chmod +x /usr/bin/bt
bt site add gal.186.baby /www/wwwroot/gal.186.baby 80 root
bt proxy add gal.186.baby http://127.0.0.1:3000 3000

echo "====================================="
echo "========= 部署全部完成！！！ ========="
echo "====================================="
echo "🔑 宝塔面板登录信息："
echo "面板地址: $BT_PANEL_URL"
echo "用户名: $BT_USER"
echo "密码: $BT_PASS"
echo "-------------------------------------"
echo "🗄️  数据库信息（已写入.env）："
echo "数据库名: $DB_NAME"
echo "用户名: $DB_USER"
echo "密码: $DB_PASS"
echo "-------------------------------------"
echo "🟥 Redis 状态：已启动并开机自启"
echo "-------------------------------------"
echo "🌐 项目访问："
echo "请将域名 gal.186.baby 解析到服务器 IP"
echo "访问地址: https://gal.186.baby"
echo "====================================="
echo "脚本调教 by悔念只玩柚子社 脚本编写 by豆包"