#!/bin/bash

# 服务器超售系统 - 一键远程安装脚本
DOWNLOAD_URL="https://mpimg.cn/down.php/649ed2d5f4f7f6be8f7e42435b5f30fd.zip"
INSTALL_DIR="/root"
ZIP_NAME="oversell_system.zip"

echo "--- 开始一键下载安装服务器超售系统 ---"

# 1. 检查 root 权限
if [ "$EUID" -ne 0 ]; then
  echo "请以 root 权限运行此脚本"
  exit 1
fi

# 2. 安装基础工具
echo "检查依赖工具: wget, unzip..."
if command -v apt-get >/dev/null 2>&1; then
    apt-get update && apt-get install -y wget unzip
elif command -v yum >/dev/null 2>&1; then
    yum install -y wget unzip
fi

# 3. 下载文件
echo "正在下载项目文件..."
wget -O "$ZIP_NAME" "$DOWNLOAD_URL"

if [ $? -ne 0 ]; then
    echo "下载失败，请检查网络链接或链接是否失效"
    exit 1
fi

# 4. 解压
echo "正在解压..."
unzip -o "$ZIP_NAME" -d "$INSTALL_DIR"

if [ $? -ne 0 ]; then
    echo "解压失败"
    exit 1
fi

# 5. 执行内部安装脚本
echo "执行系统初始化..."
cd "$INSTALL_DIR/oversell_system" || exit
chmod +x install.sh
bash install.sh

# 6. 清理
rm -f "$INSTALL_DIR/$ZIP_NAME"

echo ""
echo "===================================================="
echo "安装成功！项目已安装至: $INSTALL_DIR/oversell_system"
echo "请根据 $INSTALL_DIR/oversell_system/README_IDCSMART.md 进行魔方财务对接"
echo "===================================================="