#! /bin/sh
# Author:       Li Junhao   l@x-cmd.com             # xrc
# shellcheck    shell=sh    disable=SC2006,SC2016,SC1091,SC3043

# license:      AGPLv3

___x_cmd_xbin_downloadeval_(){
    if command -v curl 2>/dev/null 1>&2; then
        x_="eval \"\$(curl https://get.x-cmd.com)\""
    elif command -v minicurl 2>/dev/null 1>&2; then
        x_="eval \"\$(minicurl https://get.x-cmd.com)\""
    elif command -v wget 2>/dev/null 1>&2; then
        x_="eval \"\$(wget -qO- https://get.x-cmd.com)\""
    else
        >&2 printf "%s\n" 'Need wget or curl installed first'
        return 1
    fi
}

___x_cmd_xbin_download(){
    local x_
    ___x_cmd_xbin_downloadeval_ || return
    eval "$x_"
}

___x_cmd_xbin_init_(){
    if [ -f "$HOME/.x-cmd.root/X" ]; then
        x_='. "$HOME/.x-cmd.root/X"'
    elif [ -f "/var/x-cmd/v/global/X" ]; then
        command mkdir -p "$HOME/.x-cmd.root/v"
        command ln -s "/var/x-cmd/v/global" "$HOME/.x-cmd.root/v/global"

        [ ! -d "/var/x-cmd/global" ] || {
            command ln -s "/var/x-cmd/global" "$HOME/.x-cmd.root/global"
        }

        printf "%s\n" '. ${___X_CMD_ROOT:=$HOME/.x-cmd.root}/v/${___X_CMD_VERSION:=global}/X' >"$HOME/.x-cmd.root/X"
        x_='. "$HOME/.x-cmd.root/X"'
    else
        ___x_cmd_xbin_downloadeval_ || return
    fi
}

___x_cmd_xbin_init(){
    local x_=
    ___x_cmd_xbin_init_ || return
    printf "%s\n" "$x_"
}

___x_cmd_xbin___main_run(){
    local x_=
    ___x_cmd_xbin_init_ || return 1
    eval "$x_"
    command -v ___x_cmd_main 1>/dev/null 2>&1 || {
        printf "%s\n" "Setup failure."
        return 1
    }
    ___x_cmd_main "$@"
}

___x_cmd_xbin___main(){
    local op="$1"
    case "$op" in
        -h|--help)                  shift; ___x_cmd_xbin___main_help ;;
        init)                       shift; ___x_cmd_xbin_init "$@" ;;
        zsh|bash|sh|ash|dash)       shift; XXSH="$op" ___x_cmd_xbin_xxsh "$@" ;;
        *)                          ___x_cmd_xbin___main_run "$@" ;;
    esac
}

___x_cmd_xbin_sh(){
    local x_=
    ___x_cmd_xbin_init_ || return 1
    [ -f "$HOME/.x-cmd.root/X" ] || eval "$x_"
    "$SHELL" "$@"
}

___x_cmd_xbin_xxsh(){
    local x_=
    ___x_cmd_xbin_init_ || return 1
    [ -f "$HOME/.x-cmd.root/X" ] || eval "$x_"

    command -v "$XXSH" 1>/dev/null 2>&1 || {
        printf "%s\n" "Need $XXSH installed first."
        return 1
    }
    # setup shell
    # curl install xxsh ...
    command $XXSH "$@"
}

___x_cmd_xbin___main_help(){
    printf "%s\n" '

x <init|zsh|bash|ash|dash|sh>
x <other-subcmd> [args...]

subcmd:
    init
            initialize the environment, usage: eval "$(x init)"
    zsh|bash|ash|dash
            start the shell with x-cmd environment
    sh
            start the shell ($SHELL) with x-cmd environment

    <other-subcmd>
            start a process with x-cmd environment then running `x <other-subcmd>`
' >&2
    return 1
}

___x_cmd_xbin___main "$@"
