#!/bin/bash

# WPS MaxKB 加载项安装脚本
# 适用于 macOS 环境

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 配置信息
DOWNLOAD_URL="https://maxkb-tools-1323865188.cos.ap-guangzhou.myqcloud.com/maxkb-wps_1.0.0.zip"
ZIP_FILE="maxkb-wps_1.0.0.zip"
ADDON_NAME="maxkb-wps_1.0.0"
TARGET_DIR="$HOME/Library/Containers/com.kingsoft.wpsoffice.mac/Data/.kingsoft/wps/jsaddons"
PUBLISH_FILE="$TARGET_DIR/publish.xml"

# 打印带颜色的消息
print_info() {
    echo -e "${CYAN}ℹ ${NC} $1"
}

print_success() {
    echo -e "${GREEN}✓${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_header() {
    echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${BLUE}  $1${NC}"
    echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}

# 错误处理
error_exit() {
    print_error "$1"
    exit 1
}

# 清理临时文件
cleanup() {
    if [ -f "$ZIP_FILE" ]; then
        rm -f "$ZIP_FILE"
        print_info "清理临时文件完成"
    fi
}

# 注册退出时清理
trap cleanup EXIT

# 主函数
main() {
    print_header "WPS MaxKB 加载项安装程序"
    echo ""
    
    # 1. 检查系统环境
    print_info "检查系统环境..."
    if [[ "$OSTYPE" != "darwin"* ]]; then
        error_exit "此脚本仅支持 macOS 系统"
    fi
    print_success "系统环境检查通过"
    echo ""
    
    # 2. 检查必要工具
    print_info "检查必要工具..."
    for cmd in curl unzip; do
        if ! command -v $cmd &> /dev/null; then
            error_exit "未找到 $cmd 命令，请先安装"
        fi
    done
    print_success "必要工具检查完成"
    echo ""
    
    # 3. 创建目标目录
    print_info "准备目标目录..."
    if [ ! -d "$TARGET_DIR" ]; then
        mkdir -p "$TARGET_DIR" || error_exit "创建目录失败: $TARGET_DIR"
        print_success "目标目录创建成功"
    else
        print_success "目标目录已存在"
    fi
    echo ""
    
    # 4. 下载源码包
    print_info "开始下载 WPS 加载项..."
    print_info "下载地址: $DOWNLOAD_URL"
    if curl -fSL --progress-bar -o "$ZIP_FILE" "$DOWNLOAD_URL"; then
        print_success "下载完成"
    else
        error_exit "下载失败，请检查网络连接"
    fi
    echo ""
    
    # 5. 解压文件
    print_info "解压文件到目标目录..."
    if unzip -q -o "$ZIP_FILE" -d "$TARGET_DIR"; then
        print_success "解压完成"
    else
        error_exit "解压失败"
    fi
    echo ""
    
    # 6. 验证解压结果
    print_info "验证安装文件..."
    if [ -d "$TARGET_DIR/$ADDON_NAME" ]; then
        print_success "加载项文件验证成功"
    else
        error_exit "未找到加载项目录: $TARGET_DIR/$ADDON_NAME"
    fi
    echo ""
    
    # 7. 创建 publish.xml
    print_info "创建配置文件 publish.xml..."
    cat > "$PUBLISH_FILE" << 'EOF'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jsplugins>
	<jsplugin name="maxkb-wps" type="wps" url="maxkb-wps_1.0.0" version="1.0.0" enable="enable_dev" install="null" customDomain=""/>
</jsplugins>
EOF
    
    if [ -f "$PUBLISH_FILE" ]; then
        print_success "配置文件创建成功"
    else
        error_exit "配置文件创建失败"
    fi
    echo ""
    
    # 8. 设置权限
    print_info "设置文件权限..."
    chmod -R 755 "$TARGET_DIR/$ADDON_NAME" 2>/dev/null || true
    chmod 644 "$PUBLISH_FILE" 2>/dev/null || true
    print_success "权限设置完成"
    echo ""
    
    # 9. 安装完成
    print_header "安装成功！"
    echo ""
    print_success "WPS MaxKB 加载项已成功安装到:"
    echo -e "  ${CYAN}$TARGET_DIR/$ADDON_NAME${NC}"
    echo ""
    print_info "请重启 WPS Office 以加载新的加载项"
    echo ""
    print_header "感谢使用！"
}

# 执行主函数
main