#!/usr/bin/env bash

scriptdir=$(dirname $(realpath $0))
tempdir=$(mktemp -d)

# ============================================================

. $scriptdir/utils.sh

# ============================================================

configure_ssh() {
    [ -f ~/.ssh/authorized_keys ] && return

    if confirm_action "要配置 authorized_keys 吗？" "N"; then
        mkdir -p ~/.ssh
        cp $scriptdir/files/authorized_keys ~/.ssh/authorized_keys
    fi
}

configure_ssh_keygen() {
    [ -f ~/.ssh/id_ed25519.pub ] && return
    [ -f ~/.ssh/id_rsa.pub ] && return

    if confirm_action "要生成 sshkey 吗？" "N"; then
        mkdir -p ~/.ssh
        read -p "sshkey 的名字: " sshkeyname
        ssh-keygen -t ed25519 -C $sshkeyname
    fi
}

configure_git() {
    [ -f ~/.gitconfig ] && return

    if confirm_action "要配置 git 吗？" "Y"; then
        cp $scriptdir/files/.gitconfig ~/.gitconfig
    fi
}

configure_gpg() {
    [ -d ~/.gnupg ] && [ -f ~/.gnupg/gpg.conf ] && return

    if confirm_action "要配置 gpg 吗？（注意，此时还尚未导入 gpg 私钥）" "Y"; then
        mkdir -p ~/.gnupg
        chmod 700 ~/.gnupg
        cp $scriptdir/files/gpg.conf ~/.gnupg/gpg.conf
        if [ "$(uname)" = "Darwin" ]; then
            if ! command_exists gpg; then
                brew install gpg
            fi
        fi
    fi
}

configure_uv() {
    # version: 0.9.8
    [ -f ~/.local/bin/uv ] && return

    if confirm_action "要安装 uv 吗？" "Y"; then
        UV_INSTALLER_GHE_BASE_URL="$github" sh $scriptdir/files/install-uv.sh
        if [ $abroad -ne 1 ]; then
            cp $scriptdir/files/uv.toml ~/.config/uv/uv.toml
        fi
    fi
}

configure_miniconda() {
    [ -d ~/.miniconda3 ] && return
    [ "$offline" -eq 1 ] && return

    OS_TYPE=$(uname -s)
    ARCH=$(uname -m)

    # # Skip aarch64 as it is handled by Miniforge
    # if [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then
    #     echo "Skipping miniconda for aarch64/arm64"
    #     return
    # fi

    if [[ "$OS_TYPE" == "Linux" ]]; then
        INSTALLER="Miniconda3-latest-Linux-${ARCH}.sh"
    elif [[ "$OS_TYPE" == "Darwin" ]]; then
        INSTALLER="Miniconda3-latest-MacOSX-${ARCH}.sh"
    else
        echo "Unsupported OS: $OS_TYPE"
        return
    fi

    if confirm_action "要安装 miniconda 吗？" "N"; then
        if [ ! -d ~/.oh-my-zsh ] && ! confirm_action "检测到未安装 oh-my-zsh，推荐稍后安装。是否继续安装 miniconda？" "N"; then
            return
        fi
        wget "https://repo.anaconda.com/miniconda/$INSTALLER" -O "$tempdir/miniconda.sh"
        bash "$tempdir/miniconda.sh" -b -p "$HOME/.miniconda3"
        rm "$tempdir/miniconda.sh"
        ~/.miniconda3/bin/conda init zsh
        ~/.miniconda3/bin/conda config --set changeps1 False
        if [[ "$OS_TYPE" == "Linux" ]]; then
            ~/.miniconda3/bin/conda install -c conda-forge mamba
            # ~/.miniconda3/bin/mamba init
        fi
    fi
}

configure_miniforge() {
    [ -d ~/.miniforge3 ] && return
    [ "$offline" -eq 1 ] && return
    [[ $(uname -m) != "aarch64" ]] && return

    if confirm_action "要安装 miniforge 吗？" "N"; then
        if [ -d ~/.oh-my-zsh ] && ! confirm_action "检测到未安装 oh-my-zsh，推荐稍后安装。是否继续安装 miniforge？" "N"; then
            return
        fi
        wget "$github/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-$(uname -m).sh" -O $tempdir/miniforge.sh
        bash $tempdir/miniforge.sh -b -p ~/.miniforge3
        rm $tempdir/miniforge.sh
        ~/.miniforge3/bin/conda config --set changeps1 False
        ~/.miniforge3/bin/mamba init zsh
    fi
}

configure_nvm() {
    [ -d ~/.nvm/.git ] && return
    [ "$offline" -eq 1 ] && return

    if confirm_action "要安装 nvm / nodejs 吗？" "N"; then
        git_clone nvm-sh/nvm ~/.nvm --depth=1
        \. $scriptdir/files/install-nvm.sh
        \. ~/.nvm/nvm.sh
        \. ~/.nvm/bash_completion
        nvm install lts/iron
        nvm alias default lts/iron
        nvm install-latest-npm
        if [ $abroad -ne 1 ]; then
            echo "registry=https://registry.npmmirror.com" >~/.npmrc
        fi
    fi
}

# ============================================================

if [ $# -ne 0 ]; then
    for func in $@; do
        declare -F configure_$func >/dev/null || continue
        echo "Configuring $func..."
        eval "configure_$func"
        unset -f configure_$func
    done
    return
fi

configure_ssh
configure_ssh_keygen
configure_git
configure_gpg
configure_uv
configure_miniconda
configure_miniforge
configure_nvm
