#!/bin/bash

# luci-app-openlistui 编译脚本

# set -e  # 注释掉严格模式，避免单个架构失败导致整个脚本退出

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

# 项目信息
PKG_NAME="luci-app-openlistui"
DEFAULT_ARCH="x86_64"
GITHUB_REPO="drfccv/luci-app-openlistui"
GITHUB_TOKEN=""  # GitHub API Token，用于避免rate limit

# 支持的架构（使用清华大学镜像源）
declare -A ARCH_CONFIG=(
    ["x86_64"]="https://mirrors.tuna.tsinghua.edu.cn/openwrt/releases/23.05.0/targets/x86/64/openwrt-sdk-23.05.0-x86-64_gcc-12.3.0_musl.Linux-x86_64.tar.xz"
    ["aarch64"]="https://mirrors.tuna.tsinghua.edu.cn/openwrt/releases/23.05.0/targets/armsr/armv8/openwrt-sdk-23.05.0-armsr-armv8_gcc-12.3.0_musl.Linux-x86_64.tar.xz"
    ["mips"]="https://mirrors.tuna.tsinghua.edu.cn/openwrt/releases/23.05.0/targets/ath79/generic/openwrt-sdk-23.05.0-ath79-generic_gcc-12.3.0_musl.Linux-x86_64.tar.xz"
    ["mipsel"]="https://mirrors.tuna.tsinghua.edu.cn/openwrt/releases/23.05.0/targets/ramips/mt76x8/openwrt-sdk-23.05.0-ramips-mt76x8_gcc-12.3.0_musl.Linux-x86_64.tar.xz"
    ["arm"]="https://mirrors.tuna.tsinghua.edu.cn/openwrt/releases/23.05.0/targets/bcm27xx/bcm2708/openwrt-sdk-23.05.0-bcm27xx-bcm2708_gcc-12.3.0_musl_eabi.Linux-x86_64.tar.xz"
    ["armv7"]="https://mirrors.tuna.tsinghua.edu.cn/openwrt/releases/23.05.0/targets/bcm27xx/bcm2709/openwrt-sdk-23.05.0-bcm27xx-bcm2709_gcc-12.3.0_musl_eabi.Linux-x86_64.tar.xz"
)

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

# 获取GitHub releases的最新版本号
get_latest_github_version() {
    local repo="$1"
    local token="$2"
    local latest_version=""
    local auth_header=""
    
    # 如果提供了token，添加认证头
    if [ -n "$token" ]; then
        auth_header="Authorization: token $token"
    fi
    
    # 尝试使用curl获取最新release信息
    if command -v curl &> /dev/null; then
        if [ -n "$auth_header" ]; then
            latest_version=$(curl -s -H "$auth_header" "https://api.github.com/repos/${repo}/releases/latest" 2>/dev/null | \
                grep '"tag_name":' | \
                sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/' | \
                sed 's/^v//')
        else
            latest_version=$(curl -s "https://api.github.com/repos/${repo}/releases/latest" 2>/dev/null | \
                grep '"tag_name":' | \
                sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/' | \
                sed 's/^v//')
        fi
    elif command -v wget &> /dev/null; then
        if [ -n "$auth_header" ]; then
            latest_version=$(wget --header="$auth_header" -qO- "https://api.github.com/repos/${repo}/releases/latest" 2>/dev/null | \
                grep '"tag_name":' | \
                sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/' | \
                sed 's/^v//')
        else
            latest_version=$(wget -qO- "https://api.github.com/repos/${repo}/releases/latest" 2>/dev/null | \
                grep '"tag_name":' | \
                sed -E 's/.*"tag_name":\s*"([^"]+)".*/\1/' | \
                sed 's/^v//')
        fi
    fi
    
    if [ -n "$latest_version" ] && [ "$latest_version" != "null" ]; then
        echo "$latest_version"
    else
        echo ""
    fi
}

# 生成下一个版本号
generate_next_version() {
    local base_version="$1"
    local latest_github_version="$2"
    local verbose="${3:-false}"
    
    if [ -z "$latest_github_version" ]; then
        # 如果无法获取GitHub版本，使用git commit计数
        local patch_version="1"
        if [ -d ".git" ]; then
            patch_version=$(git rev-list --count HEAD 2>/dev/null || echo "1")
        fi
        [ "$verbose" = "true" ] && log_info "无法获取GitHub版本，使用git计数: ${base_version}.${patch_version}" >&2
        echo "${base_version}.${patch_version}"
        return
    fi
    
    # 解析版本号
    local github_base=$(echo "$latest_github_version" | cut -d'.' -f1-2)
    local github_patch=$(echo "$latest_github_version" | cut -d'.' -f3)
    
    # 如果base version相同，生成下一个小版本号
    if [ "$github_base" = "$base_version" ]; then
        local next_patch=$((github_patch + 1))
        [ "$verbose" = "true" ] && log_info "检测到相同base version，生成下一个小版本: ${base_version}.${next_patch}" >&2
        echo "${base_version}.${next_patch}"
    else
        # 如果base version不同，使用git commit计数
        local patch_version="1"
        if [ -d ".git" ]; then
            patch_version=$(git rev-list --count HEAD 2>/dev/null || echo "1")
        fi
        [ "$verbose" = "true" ] && log_info "base version不同，使用git计数: ${base_version}.${patch_version}" >&2
        echo "${base_version}.${patch_version}"
    fi
}

show_help() {
    echo "OpenList LuCI App 简化编译脚本"
    echo ""
    echo "用法: $0 [选项|架构]"
    echo ""
    echo "支持的架构:"
    echo "  x86_64   - x86 64位 (默认)"
    echo "  aarch64  - ARM 64位"
    echo "  mips     - MIPS 大端序 (ath79)"
    echo "  mipsel   - MIPS 小端序 (ramips)"
    echo "  arm      - ARM 32位 (bcm2708)"
    echo "  armv7    - ARMv7 (bcm2709)"
    echo "  all      - 编译所有支持的架构"
    echo ""
    echo "构建选项:"
    echo "  -f, --fast       - 跳过feeds更新 (适用于重复构建)"
    echo "  --dev            - 开发模式，为包文件名添加时间戳"
    echo "  -v, --version VERSION - 指定版本号 (例如: 1.2.3)"
    echo "  -t, --token TOKEN     - GitHub API token (避免rate limit)"
    echo "  -j, --jobs JOBS       - 指定编译线程数 (默认: 自动检测CPU核心数)"
    echo ""
    echo "其他选项:"
    echo "  version  - 显示版本信息 (包含GitHub releases检测)"
    echo "  clean    - 清理构建文件"
    echo "  help     - 显示此帮助信息"
    echo ""
    echo "版本管理:"
    echo "  脚本会自动检测 GitHub releases 的最新版本号"
    echo "  如果 base version 相同，会自动生成下一个小版本号"
    echo "  可以使用 -v 参数指定自定义版本号"
    echo "  使用 -t 参数提供 GitHub token 避免 API rate limit"
    echo "  GitHub仓库: https://github.com/$GITHUB_REPO/releases"
    echo ""
    echo "示例:"
    echo "  $0                          # 编译默认架构 (x86_64)"
    echo "  $0 -v 1.2.3 x86_64         # 使用指定版本号编译"
    echo "  $0 -t ghp_xxx... all       # 使用GitHub token编译所有架构"
    echo "  $0 -j 4 x86_64             # 使用4线程编译 x86_64"
    echo "  $0 aarch64      # 编译 ARM64"
    echo "  $0 all          # 编译所有支持的架构"
    echo "  $0 -f x86_64    # 快速编译 x86_64 (跳过feeds更新)"
    echo "  $0 --fast       # 快速编译默认架构"
    echo "  $0 --dev x86_64 # 开发模式编译 x86_64 (包含时间戳)"
    echo "  $0 version      # 显示版本信息"
    echo "  $0 clean        # 清理构建文件"
}

# 编译所有架构
build_all_packages() {
    local fast_mode="${1:-false}"
    local dev_mode="${2:-false}"
    local custom_version="${3:-}"
    local jobs="${4:-1}"
    local all_archs=("x86_64" "aarch64" "mips" "mipsel" "arm" "armv7")
    local success_count=0
    local failed_archs=()
    
    log_info "开始编译所有支持的架构 $([ "$fast_mode" = "true" ] && echo "(快速模式)" || echo "")"
    log_info "总共需要编译 ${#all_archs[@]} 个架构: ${all_archs[*]}"
    echo ""
    
    for arch in "${all_archs[@]}"; do
        log_info "[${success_count}/${#all_archs[@]}] 开始编译架构: $arch"
        echo "========================================"
        
        if build_package "$arch" "$fast_mode" "$dev_mode" "$custom_version" "$jobs"; then
            ((success_count++))
            log_success "架构 $arch 编译成功"
        else
            failed_archs+=("$arch")
            log_error "架构 $arch 编译失败"
        fi
        
        echo ""
    done
    
    # 显示编译结果汇总
    echo "========================================"
    log_info "编译完成汇总:"
    log_success "成功: $success_count/${#all_archs[@]} 个架构"
    
    if [ ${#failed_archs[@]} -gt 0 ]; then
        log_error "失败: ${#failed_archs[@]} 个架构 (${failed_archs[*]})"
        return 1
    else
        log_success "所有架构编译成功！"
        return 0
    fi
}

# 简化的构建函数
build_package() {
    local target_arch="${1:-$DEFAULT_ARCH}"
    local fast_mode="${2:-false}"
    local dev_mode="${3:-false}"
    local custom_version="${4:-}"
    local jobs="${5:-1}"
    
    if [[ -z "${ARCH_CONFIG[$target_arch]}" ]]; then
        log_error "不支持的架构: $target_arch"
        log_info "支持的架构: ${!ARCH_CONFIG[*]}"
        return 1
    fi
    
    log_info "编译 OpenList LuCI App [$target_arch] $([ "$fast_mode" = "true" ] && echo "(快速模式)" || echo "") [${jobs}线程]"
    
    # 显示版本信息
    if [ -f "Makefile" ]; then
        log_info "检查版本信息..."
        if command -v make &> /dev/null; then
            make version 2>/dev/null || log_warning "无法显示版本信息"
        fi
    fi
    
    local SDK_URL="${ARCH_CONFIG[$target_arch]}"
    local BUILD_DIR="build-${target_arch}"
    
    # 创建并进入构建目录
    mkdir -p "$BUILD_DIR"
    cd "$BUILD_DIR"
    
    # 下载SDK（如果还没有）
    local SDK_FILE="sdk.tar.xz"
    if [ ! -f "$SDK_FILE" ]; then
        log_info "下载 OpenWrt SDK..."
        log_info "URL: $SDK_URL"
        if ! wget --timeout=30 --tries=3 --progress=bar:force "$SDK_URL" -O "$SDK_FILE"; then
            log_error "SDK下载失败"
            cd ..
            return 1
        fi
    fi
    
    # 解压SDK（如果还没有）
    if [ ! -d "sdk" ]; then
        log_info "解压 SDK..."
        mkdir -p sdk
        tar -xf "$SDK_FILE" --strip-components=1 -C sdk
    fi
    
    cd sdk
    
    # 复制包文件
    log_info "复制包文件..."
    rm -rf package/luci-app-openlistui
    mkdir -p package/luci-app-openlistui
    
    # 复制必要的文件，包括 root 目录（包含 init.d 脚本）
    cp -r ../../Makefile ../../luasrc ../../root package/luci-app-openlistui/
    
    # 复制 po 目录（如果存在）
    if [ -d "../../po" ]; then
        cp -r ../../po package/luci-app-openlistui/
    else
        log_warning "po 目录不存在，跳过翻译文件复制"
    fi
    
    # 如果存在 htdocs 目录，也复制
    if [ -d "../../htdocs" ]; then
        cp -r ../../htdocs package/luci-app-openlistui/
    fi
    
    # 更新feeds并安装必要依赖
    if [ "$fast_mode" = "true" ]; then
        log_info "快速模式: 跳过feeds更新"
    else
        log_info "配置GitHub镜像源..."
        # 备份原始feeds配置
        [ -f feeds.conf.default.bak ] || cp feeds.conf.default feeds.conf.default.bak
        
        # 替换为GitHub镜像源（清华镜像不提供git仓库，只提供包镜像）
        sed -i 's|git.openwrt.org/feed|github.com/openwrt|g' feeds.conf.default
        sed -i 's|git.openwrt.org/project|github.com/openwrt|g' feeds.conf.default
        
        log_info "更新 feeds (这可能需要几分钟，请耐心等待)..."
        log_info "提示: 使用 -f 或 --fast 选项可跳过此步骤"
        
        # 显示进度的feeds更新
        timeout 300 ./scripts/feeds update -a 2>&1 | while IFS= read -r line; do
            echo "  $line"
        done || {
            log_warning "Feeds更新超时或失败，尝试继续..."
        }
    fi
    
    log_info "安装LuCI依赖包..."
    ./scripts/feeds install luci-base luci-lib-jsonc luci-lib-nixio >/dev/null 2>&1 || true
    
    # 安装构建依赖
    log_info "安装构建依赖..."
    ./scripts/feeds install liblua lua curl wget unzip >/dev/null 2>&1 || true
    
    # 简单配置
    echo "CONFIG_PACKAGE_luci-app-openlistui=y" > .config
    make defconfig >/dev/null 2>&1 || true
    
    # 编译包
    log_info "编译包..."
    
    # 创建构建日志目录
    mkdir -p ../../build-logs
    local build_log="../../build-logs/build-${target_arch}-$(date +%Y%m%d-%H%M%S).log"
    
    # 设置自定义版本号环境变量
    local make_env=""
    if [ -n "$custom_version" ]; then
        make_env="CUSTOM_VERSION=$custom_version"
        log_info "设置自定义版本号: $custom_version"
    fi
    
    if eval "$make_env make package/luci-app-openlistui/compile V=s -j$jobs" 2>&1 | tee "$build_log"; then
        # 查找并复制IPK文件
        mkdir -p ../../output
        local ipk_file=$(find bin -name "*luci-app-openlistui*.ipk" | head -1)
        if [ -n "$ipk_file" ]; then
            # 版本号处理
            local version="unknown"
            if [ -n "$custom_version" ]; then
                # 使用自定义版本号
                version="$custom_version"
                log_info "使用自定义版本号: $version"
            elif [ -f "../../Makefile" ]; then
                local base_version=$(grep "^PKG_VERSION_BASE:=" ../../Makefile | head -1 | cut -d'=' -f2 | tr -d ' \n\r')
                if [ -n "$base_version" ]; then
                    # 获取GitHub最新版本
                    log_info "检测GitHub releases最新版本..."
                    local latest_github_version=$(get_latest_github_version "$GITHUB_REPO" "$GITHUB_TOKEN")
                    # 生成新版本号
                    version=$(generate_next_version "$base_version" "$latest_github_version" "true")
                fi
            fi
            
            if [ "$dev_mode" = "true" ]; then
                local output_name="${PKG_NAME}_v${version}_${target_arch}_$(date +%Y%m%d-%H%M%S).ipk"
            else
                local output_name="${PKG_NAME}_v${version}_${target_arch}.ipk"
            fi
            cp "$ipk_file" "../../output/$output_name"
            
            # 生成构建信息文件
            local info_file="../../output/${output_name%.ipk}.info"
            {
                echo "# OpenList UI Build Information"
                echo "Build Date: $(date '+%Y-%m-%d %H:%M:%S')"
                echo "Target Architecture: $target_arch"
                echo "Package Name: $PKG_NAME"
                echo "IPK File: $output_name"
                echo "Build Host: $(whoami)@$(hostname)"
                echo "Build Directory: $PWD"
                echo "Build Log: $build_log"
                if [ -d ../../.git ]; then
                    echo "Git Hash: $(git -C ../.. rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
                    echo "Git Branch: $(git -C ../.. branch --show-current 2>/dev/null || echo 'unknown')"
                fi
                echo ""
                echo "File Information:"
                ls -lh "../../output/$output_name"
            } > "$info_file"
            
            log_success "编译完成！"
            log_info "IPK文件: output/$output_name"
            log_info "构建信息: output/${output_name%.ipk}.info"
            log_info "构建日志: $build_log"
            ls -lh "../../output/$output_name"
        else
            log_error "未找到编译后的IPK文件"
            log_info "构建日志已保存: $build_log"
            cd ../..
            return 1
        fi
    else
        log_error "编译失败"
        log_info "构建日志已保存: $build_log"
        cd ../..
        return 1
    fi
    
    cd ../..
}

# 清理构建文件
clean_build() {
    log_info "清理构建文件..."
    rm -rf build-*
    rm -rf output
    rm -rf build-logs
    rm -rf build-info
    log_success "清理完成"
}

# 显示版本信息
show_version() {
    log_info "OpenList LuCI App 版本信息"
    echo ""
    
    if [ -f "Makefile" ]; then
        # 从Makefile提取版本信息
        if command -v make &> /dev/null; then
            make version 2>/dev/null
            
            # 即使make成功，也显示GitHub版本信息
            echo ""
            echo "GitHub Release Information:"
            local base_version=$(grep "^PKG_VERSION_BASE:=" Makefile | head -1 | cut -d'=' -f2 | tr -d ' \n\r')
            echo "正在检测GitHub releases..."
            local latest_github_version=$(get_latest_github_version "$GITHUB_REPO" "$GITHUB_TOKEN")
            if [ -n "$latest_github_version" ]; then
                echo "Latest GitHub Release: $latest_github_version"
                local next_version=$(generate_next_version "$base_version" "$latest_github_version")
                echo "Next Build Version: $next_version"
                
                # 显示版本策略说明
                local github_base=$(echo "$latest_github_version" | cut -d'.' -f1-2)
                if [ "$github_base" = "$base_version" ]; then
                    echo "Strategy: Same base version detected, incrementing patch number"
                else
                    echo "Strategy: Different base version, using git commit count"
                fi
            else
                log_warning "无法获取GitHub releases信息"
                local next_version=$(generate_next_version "$base_version" "")
                echo "Next Build Version: $next_version"
            fi
        else
            log_warning "make 命令不可用，从Makefile手动提取版本信息"
            
            # 手动解析Makefile中的版本信息
            echo "正在解析Makefile..."
            
            local base_version=$(grep "^PKG_VERSION_BASE:=" Makefile | head -1 | cut -d'=' -f2 | tr -d ' \n\r')
            local build_date=$(date '+%Y-%m-%d %H:%M:%S')
            local build_host="$(whoami 2>/dev/null || echo 'unknown')@$(hostname 2>/dev/null || echo 'unknown')"
            
            echo "Base Version: $base_version"
            echo "Build Date: $build_date"
            echo "Build Host: $build_host"
            
            if [ -d ".git" ]; then
                local git_hash=$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')
                local git_branch=$(git branch --show-current 2>/dev/null || echo 'unknown')
                local patch_version=$(git rev-list --count HEAD 2>/dev/null || echo '1')
                
                echo "Git Hash: $git_hash"
                echo "Git Branch: $git_branch"
                echo "Patch Version: $patch_version"
                echo "Traditional Version: ${base_version}.${patch_version}"
            else
                echo "Git Hash: unknown (not a git repository)"
                echo "Git Branch: unknown"
                echo "Patch Version: 1"
                echo "Traditional Version: ${base_version}.1"
            fi
            
            # 显示GitHub版本信息
            echo ""
            echo "GitHub Release Information:"
            echo "正在检测GitHub releases..."
             local latest_github_version=$(get_latest_github_version "$GITHUB_REPO" "$GITHUB_TOKEN")
             if [ -n "$latest_github_version" ]; then
                 echo "Latest GitHub Release: $latest_github_version"
                 local next_version=$(generate_next_version "$base_version" "$latest_github_version")
                 echo "Next Build Version: $next_version"
                 
                 # 显示版本策略说明
                 local github_base=$(echo "$latest_github_version" | cut -d'.' -f1-2)
                 if [ "$github_base" = "$base_version" ]; then
                     echo "Strategy: Same base version detected, incrementing patch number"
                 else
                     echo "Strategy: Different base version, using git commit count"
                 fi
             else
                log_warning "无法获取GitHub releases信息"
                local next_version=$(generate_next_version "$base_version" "")
                echo "Next Build Version: $next_version"
            fi
        fi
    else
        log_error "Makefile 不存在"
        exit 1
    fi
    
    echo ""
    log_info "版本信息显示完毕"
}
main() {
    local fast_mode=false
    local arch="$DEFAULT_ARCH"
    local build_all=false
    local dev_mode=false
    local custom_version=""
    local jobs="$(nproc 2>/dev/null || echo '1')"
    
    # 解析参数
    while [[ $# -gt 0 ]]; do
        case $1 in
            "-f"|"--fast")
                fast_mode=true
                shift
                ;;
            "--dev")
                dev_mode=true
                shift
                ;;
            "-v"|"--version")
                if [[ $# -lt 2 ]] || [[ $2 == -* ]]; then
                    show_version
                    exit 0
                else
                    custom_version="$2"
                    shift 2
                fi
                ;;
            "-t"|"--token")
                if [[ $# -lt 2 ]] || [[ $2 == -* ]]; then
                    log_error "--token 参数需要提供 GitHub token"
                    exit 1
                else
                    GITHUB_TOKEN="$2"
                    shift 2
                fi
                ;;
            "-j"|"--jobs")
                if [[ $# -lt 2 ]] || [[ $2 == -* ]]; then
                    log_error "--jobs 参数需要提供线程数"
                    exit 1
                else
                    jobs="$2"
                    shift 2
                fi
                ;;
            "help"|"-h"|"--help")
                show_help
                exit 0
                ;;
            "version")
                show_version
                exit 0
                ;;
            "clean")
                clean_build
                exit 0
                ;;
            "all")
                build_all=true
                shift
                ;;
            *)
                arch="$1"
                shift
                ;;
        esac
    done
    
    # 构建包
    if [ "$build_all" = "true" ]; then
        build_all_packages "$fast_mode" "$dev_mode" "$custom_version" "$jobs"
    else
        build_package "$arch" "$fast_mode" "$dev_mode" "$custom_version" "$jobs"
    fi
}

# 检查基本环境
if [[ "$OSTYPE" != "linux-gnu"* ]]; then
    log_error "此脚本仅支持 Linux 系统"
    exit 1
fi

if ! command -v wget &> /dev/null || ! command -v make &> /dev/null; then
    log_error "请安装必要工具: wget make gcc"
    log_info "Ubuntu/Debian: sudo apt-get install build-essential wget"
    exit 1
fi

main "$@"