#!/bin/bash
# LiteMall 一键部署脚本

echo "======================================"
echo " 欢迎使用 LiteMall 轻量级商城部署工具 "
echo "======================================"

# 1. 检查环境
php -v | grep "PHP 8" > /dev/null
if [ $? -ne 0 ]; then
 echo "错误: 需要 PHP 8.0 或以上版本，请先安装。"
 exit 1
fi

# 2. 创建运行目录并赋权
echo "正在配置目录权限..."
mkdir -p server/runtime/cache
mkdir -p server/runtime/logs
chmod -R 755 server/runtime
chown -R www:www server/runtime 2>/dev/null

# 3. 导入数据库
read -p "请输入MySQL数据库名: " DB_NAME
read -p "请输入MySQL用户名: " DB_USER
read -s -p "请输入MySQL密码: " DB_PASS
echo ""

echo "正在导入基础数据表..."
mysql -u$DB_USER -p$DB_PASS $DB_NAME < server/install/litemall_base.sql
if [ $? -eq 0 ]; then
 echo "数据库导入成功！"
else
 echo "数据库导入失败，请检查连接信息。"
 exit 1
fi

# 4. 生成数据库配置文件
cat > server/config/database.php <<PHPEOF
<?php
return [
 'host' => '127.0.0.1',
 'dbname' => '$DB_NAME',
 'username' => '$DB_USER',
 'password' => '$DB_PASS',
 'charset' => 'utf8mb4'
];
PHPEOF

echo "======================================"
echo " 部署完成！"
echo " 后台访问地址: http://您的域名/admin/"
echo " 默认账号: admin 默认密码: 123456"
echo "======================================"
