#!/usr/bin/env zsh

__import "print/print"

local    repository
local    tag_use tag_from tag_at tag_depth
local    depth_option
local    url_format
local -i ret

while (( $# > 0 ))
do
    case "$1" in
        --use)
            tag_use="$2"; shift
            ;;
        --from)
            tag_from="$2"; shift
            ;;
        --at)
            tag_at="$2"; shift
            ;;
        --depth)
            tag_depth="$2"; shift
            case $tag_depth in
                0)
                    case "$ZPLUG_CLONE_DEPTH" in
                        0)
                            depth_option=""
                            ;;
                        <->)
                            depth_option="--depth=$ZPLUG_CLONE_DEPTH"
                            ;;
                        *)
                            __zplug::print::print::die "[zplug] $fg[red]ERROR$reset_color: ZPLUG_CLONE_DEPTH must be a positive number.\n"
                            return 1
                            ;;
                    esac
                    ;;
                <->)
                    depth_option="--depth=$tag_depth"
                    ;;
                *)
                    # not integer
                    __zplug::print::print::die "[zplug] $fg[red]ERROR$reset_color: depth tag must be a positive number\n"
                    __zplug::print::print::die "        but, if zero, no shallow clone.\n"
                    return 1
                    ;;
            esac
            ;;
        -*|--*)
            __zplug::print::print::die "[zplug] $1: Unknown option\n"
            return 1
            ;;
        *)
            repository="$1"
            ;;
    esac
    shift
done

# If an 'at' tag has been specified, do a deep clone to allow any commit to be
# checked out.
[[ -n "$tag_at" ]] && depth_option=""

# Initialize
{
    [[ -d $ZPLUG_REPOS ]] || mkdir -p "$ZPLUG_REPOS"
    builtin cd -q "$ZPLUG_REPOS"

    case "$tag_from" in
        github)
            if [[ $ZPLUG_PROTOCOL =~ ^(HTTPS|https)$ ]]; then
                # Create the format of URL used to git clone
                # When vim-plug clones a repository, it injects git::@ into the URL
                # It's a little hack to avoid username/password prompt
                # from git when the repository doesn't exist.
                # Such thing can happen when there's a typo in the argument,
                # or when the repository is removed from GitHub
                # For more information, see also vim-plug wiki.
                # HTTPS: "https://git::@github.com/%s.git"
                url_format="https://git::@github.com/${repository}.git"

                # However, Git 2.3.0 introduced $GIT_TERMINAL_PROMPT
                # which can be used to suppress user prompt
                if __zplug::core::core::git_version 2.3; then
                    # HTTPS (git 2.3+): "https://github.com/%s.git"
                    export GIT_TERMINAL_PROMPT=0
                    url_format="https://github.com/${repository}.git"
                fi
            elif [[ $ZPLUG_PROTOCOL =~ ^(SSH|ssh)$ ]]; then
                # SSH: "git@github.com:%s.git"
                url_format="git@github.com:${repository}.git"
            fi
            ;;

        bitbucket)
            if [[ $ZPLUG_PROTOCOL =~ ^(HTTPS|https)$ ]]; then
                # HTTPS: "https://git::@bitbucket.org/%s.git"
                url_format="https://git::@bitbucket.org/${repository}.git"
            elif [[ $ZPLUG_PROTOCOL =~ ^(SSH|ssh)$ ]]; then
                # SSH: "git@bitbucket.org:%s.git"
                url_format="git@bitbucket.org:${repository}.git"
            fi
            ;;

        gist)
            if [[ $ZPLUG_PROTOCOL =~ ^(HTTPS|https)$ ]]; then
                # the same as github
                #
                # HTTPS: "https://git::@github.com/%s.git"
                url_format="https://git::@gist.github.com/${repository}.git"

                if __zplug::core::core::git_version 2.3; then
                    # HTTPS (git 2.3+): "https://gist.github.com/%s.git"
                    export GIT_TERMINAL_PROMPT=0
                    url_format="https://gist.github.com/${repository}.git"
                fi
            elif [[ $ZPLUG_PROTOCOL =~ ^(SSH|ssh)$ ]]; then
                # SSH: "git@github.com:%s.git"
                url_format="git@gist.github.com:${repository}.git"
            fi
            ;;

        gh-r)
            __releases__ \
                --use "${tag_use:-}" \
                --at  "${tag_at:#master}" \
                "$repository" &>/dev/null
            return $status
            ;;

        *)
            __zplug::print::print::die "[zplug] $tag_from: Unknown tag\n"
            return 1
            ;;
    esac

    if [[ ! $ZPLUG_PROTOCOL =~ ^(HTTPS|https|SSH|ssh)$ ]]; then
        __zplug::print::print::die "[zplug] $fg[red]ERROR$reset_color: $ZPLUG_PROTOCOL is an invalid protocol.\n"
        return 1
    fi

    if [[ -z $url_format ]]; then
        __zplug::print::print::die "[zplug] $fg[red]ERROR$reset_color: $repository is an invalid 'user/repo' format.\n"
        return 1
    fi
}

# git clone
(( $ret == 0 )) &&
    git clone \
    $depth_option \
    --recursive \
    --quiet \
    "$url_format" "$repository" &>/dev/null
ret=$status

if (( $ret == 0 )); then
    (
        # revision/branch/tag lock
        builtin cd -q "$ZPLUG_REPOS/$repository"
        git checkout -q "$tag_at" &>/dev/null
        if (( $status != 0 )); then
            __zplug::print::print::die "[zplug] $fg[red]ERROR$reset_color: pathspec '$tag_at' (at tag) did not match ($repository)\n"
            ret=1
        fi
    )
fi

return $ret
