# shellcheck shell=dash

___x_cmd_solo___preparepath(){
    local srcfile="$1"

    local x_;   ___x_cmd_solo___absfp_ "$srcfile" || return $?
    absfp="$x_"

    if ___x_cmd_solo___isinsolofolder "$absfp"; then
        N=solo M="The folder of the target script file is a solo workspace. Please clean the folder by yourself -> $absfp" log:ret:64
    fi

    wsfn="${absfp##*/}"
    wsdp="${___X_CMD_SOLO_WORKSPACE_ROOT}/${x_#/}"
    wsfp="${wsdp%/}/${wsfn}"

    ___x_cmd mkdirp "$wsdp"

    # wsfp="${___X_CMD_SOLO_WORKSPACE_ROOT}/${x_#/}"
    # wsdp="${wsfp%/*}"
}

alias solo:prepare:path='local absfp; local wsfp; local wsdp; local wsfn; ___x_cmd_solo___preparepath'

___x_cmd_solo___absfp_(){
    local srcfile="$1"
    [ -f "$srcfile" ] || N=solo M="Cannot find the target script file -> $srcfile" log:ret:1
    ___x_cmd abspath_ "$srcfile" \
        || N=solo M="Cannot calcuate abspath of $srcfile" log:ret:1
}

___x_cmd_solo___isinsolofolder(){
    case "$1" in
        "${___X_CMD_SOLO_WORKSPACE_ROOT}"/*)    return 0 ;;
        *)                                      return 1 ;;
    esac
}


___x_cmd_solo___which(){
    case "$1" in
        "")             N=solo M="Please provide <srcfile> after 'which'"   log:ret:64 ;;
        -h|--help)      ___x_cmd help -m solo which "$@";            return ;;
    esac

    local x_="";
    ___x_cmd_solo___which_ "$@"       || return $?
    printf "%s\n" "$x_"
}

___x_cmd_solo___which_(){
    case "$1" in
        "")             N=solo M="Please provide <srcfile> after 'which_'"   log:ret:64 ;;
        -h|--help)      ___x_cmd help -m solo which_ "$@";            return ;;
    esac

    local srcfile="$1";     shift
    x_=""
    solo:prepare:path "$srcfile"    || return $?
    x_="${wsfp}"
}

___x_cmd_solo___cd(){
    case "$1" in
        "")             N=solo M="Please provide <srcfile> after 'cd'"   log:ret:64 ;;
        -h|--help)      ___x_cmd help -m solo cd "$@";            return ;;
    esac


    local srcfile="$1";     shift
    solo:prepare:path "$srcfile"    || return $?
    ___x_cmd_cmds_cd "${wsdp}"
}


___x_cmd_solo_runexe(){
    local exefile="${1}"

    if   [ -f "${exefile}"     ]; then      "${exefile}"        "$@"
    elif [ -f "${exefile}.exe" ]; then      "${exefile}.exe"    "$@"
    else                                    N=solo M="Cannot find the execuatable file -> $exefile" log:ret:1
    fi
}
