# shellcheck shell=sh disable=SC3043

# git facility
___x_cmd_scotty_ws_name(){
    param:void
    local name
    name=$(___x_cmd_scotty_ws_geturl)
    name="${name%.git}"

    case "$#" in
        0)      printf "%s\n" "${name%.git}" ;;
        1)      [ "$1" != "-" ] && eval "$1=\"\${name%/*}\"" ;;
        2)
                [ "$1" != "-" ] && eval "$1=\"\${name%/*}\""
                [ "$2" != "-" ] && eval "$2=\"\${name#*/}\""
    esac
}

# git facility
___x_cmd_scotty_ws_geturl(){
    ___x_cmd git meta path
}

# git facility

___x_cmd_scotty_ws_pull(){
    param:void
    ___x_cmd git pull "git@$(___x_cmd websrc gitdomain):$(___x_cmd git meta path)" "$(___x_cmd git meta branch)"
}

___x_cmd_scotty_ws_push(){
    param:void
    ___x_cmd scotty mod push
}

___x_cmd_scotty_ws(){
    if [ -f "$(___x_cmd wsroot)/.x-cmd/$1" ]; then
        ___x_cmd ws "$@"
        return
    fi

    param:dsl <<A
subcommand:
    pull        "pull module"
    push        "push module"
    name        "get workspace name from git config"
A
    param:run

    if [ -z "$PARAM_SUBCMD" ]; then
        ___x_cmd_scotty_ws help
        return 1
    fi

    "___x_cmd_scotty_ws_$PARAM_SUBCMD" "$@"
}
