#!/usr/bin/env bash
#============================================================
# File: git-mirror.sh
# Description: 展示代码仓库的镜像地址（支持完美横排）
# Author: Jetsung Chan <i@jetsung.com>
# Version: 0.2.0
# UpdatedAt: 2025-12-10
#============================================================

[[ -n "${DEBUG:-}" ]] && set -eux || set -euo pipefail

# 平台 URL 配置
declare -A PLATFORMS=(
    ["MYCODE"]="https://git.jetsung.com"
    ["FRAMAGIT"]="https://framagit.org"
    ["ATOMGIT"]="https://atomgit.com"
    ["GITHUB"]="https://github.com"
    ["CODEBERG"]="https://codeberg.org"
    ["CODEUP"]="https://codeup.aliyun.com/jetsung"
)

parse_args() {
    local org="" repo=""
    local github="" atomgit="" framagit="" codeup="" codeberg="" horizontal=""

    while [[ $# -gt 0 ]]; do
        case "$1" in
            -o|--org)         org="${2:-}";     shift 2 ;;
            -r|--repo)        repo="${2:-}";    shift 2 ;;
            -gh|--github)     github="YES";     shift ;;
            -ag|--atomgit)    atomgit="YES";    shift ;;
            -fg|--framagit)   framagit="YES";   shift ;;
            -cb|--codeberg)   codeberg="YES";  shift ;;
            -cp|--codeup)     codeup="YES";     shift ;;
            -H|--horizontal)  horizontal="YES"; shift ;;
            -h|--help)
                cat <<'EOF'
Usage: gitmirror -o <org> [-r <repo>] [options]

Options:
  -o, --org         组织名（必填）
  -r, --repo        仓库名（默认当前目录）
  -gh, --github     显示 GitHub
  -ag, --atomgit    显示 AtomGit
  -fg, --framagit   显示 Framagit
  -cb, --codeberg   显示 Codeberg
  -cp, --codeup     显示 Codeup
  -H, --horizontal  横排显示（完美居中 ● 分隔）
  -h, --help        显示帮助
EOF
                exit 0
                ;;
            *)
                echo "未知选项: $1" >&2
                echo "使用 --help 查看帮助" >&2
                exit 1
                ;;
        esac
    done

    ORG="$org"
    REPO="$repo"
    GITHUB="$github"
    ATOMGIT="$atomgit"
    FRAMAGIT="$framagit"
    CODEUP="$codeup"
    CODEBERG="$codeberg"
    HORIZONTAL="$horizontal"
}

main() {
    # 初始化全局变量
    ORG="" REPO="" GITHUB="" ATOMGIT="" FRAMAGIT="" CODEUP="" CODEBERG="" HORIZONTAL=""

    parse_args "$@"

    # 必填校验
    [[ -z "$ORG" ]] && { echo "Error: 缺少 -o/--org 参数" >&2; exit 1; }

    # REPO 默认当前目录名
    [[ -z "$REPO" ]] && REPO=$(basename "$PWD")
    [[ -z "$REPO" ]] && { echo "Error: 无法确定不了仓库名" >&2; exit 1; }

    # 简单格式校验
    if ! [[ "$ORG" =~ ^[a-zA-Z0-9_-]+$ && "$REPO" =~ ^[a-zA-Z0-9_-]+$ ]]; then
        echo "Error: org 和 repo 只能包含字母、数字、- 和 _" >&2
        exit 1
    fi

    # 特殊组织映射
    ORG_FRAMAGIT="$ORG"
    ORG_CODEUP="$ORG"
    ORG_ATOMGIT="$ORG"
    ORG_GITHUB="$ORG"
    ORG_CODEBERG="$ORG"
    case "$ORG" in
        idev) ORG_GITHUB="idevsig"; ORG_ATOMGIT="idev" ;;
        tiny) ORG_GITHUB="tinyzen"; ORG_ATOMGIT="tinyzen" ;;
    esac

    echo
    echo "## 仓库镜像"
    echo

    # 收集所有要显示的链接
    local links=()
    links+=("[MyCode](${PLATFORMS[MYCODE]}/${ORG}/${REPO})")
    [[ -n "$FRAMAGIT" ]] && links+=("[Framagit](${PLATFORMS[FRAMAGIT]}/${ORG_FRAMAGIT}/${REPO})")
    [[ -n "$ATOMGIT" ]]  && links+=("[AtomGit](${PLATFORMS[ATOMGIT]}/${ORG_ATOMGIT}/${REPO})")
    [[ -n "$GITHUB" ]]   && links+=("[GitHub](${PLATFORMS[GITHUB]}/${ORG_GITHUB}/${REPO})")
    [[ -n "$CODEBERG" ]] && links+=("[Codeberg](${PLATFORMS[CODEBERG]}/${ORG_CODEBERG}/${REPO})")
    [[ -n "$CODEUP" ]]   && links+=("[Codeup](${PLATFORMS[CODEUP]}/${ORG_CODEUP}/${REPO})")

    if [[ "$HORIZONTAL" == "YES" ]]; then
        # 完美横排：每个链接之间用「 ● 」分隔（左右各一个空格）
        local first=1
        for link in "${links[@]}"; do
            if (( first )); then
                printf "%s" "$link"
                first=0
            else
                printf " ● %s" "$link"
            fi
        done
        echo
        echo
    # 空一行，保持和竖排一致
    else
        # 竖排（原样）
        for link in "${links[@]}"; do
            echo "- $link"
        done
        echo
    fi
}

main "$@"
