# shellcheck    shell=sh disable=SC3043     # xrc

___x_cmd log init zuz

xrc:mod:lib zuz     cat ls ll z/_index uz/_index

___x_cmd_zuz___main(){
    # Tempoerary ... Once we erase all side effect, we will remove the subshell.
    (
        if [ -z "$LC_ALL" ]; then
            LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd_zuz___main_ "$@"
        else
            ___x_cmd_zuz___main_ "$@"
        fi
    )
}

___x_cmd_zuz___main_(){
    [ "$#" -gt 0 ] ||           set -- --help

    local op="${1}";            shift
    case "$op" in
        z|compress)             ___x_cmd_zuz_z          "$@" ;;
        uz|decompress)          ___x_cmd_zuz_uz         "$@" ;;
        uzr)                    ___x_cmd_zuz_uzr        "$@" ;;
        ls)                     ___x_cmd_zuz_ls         "$@" ;;
        cat)                    ___x_cmd_zuz_cat        "$@" ;;
        ""|--help|-h)           ___x_cmd help -m zuz    "$@" ;;
        show)                   ___x_cmd_zuz_show       "$@" ;;

        *)                      ___x_cmd_zuz -h
                                N=zuz M="Not found command: x zuz $op" log:ret:64
    esac
}


# NOTICE: tar czf - "$@" | split -b "$size" -a 3 - "$target_file."
# NOTICE: In some environments, split doesnot have -d option. Thus we are using 7zip to compress
___x_cmd_zuz_run(){
    local IFS=' '
    zuz:debug "Command to execute => $*"
    if [ -n "$___X_CMD_ZUZ_QUIET" ]; then
        "$@" 2>/dev/null 1>&2
    elif [ -n "$___X_CMD_ZUZ_NOT_UI" ] || ! ___x_cmd_runmode_allow_manual ; then
        "$@"
    else
        zuz:info --cmd "$*" "start execution"
        ___x_cmd log uirotate "$@"
        # { "$@"; printf "\nEXITCODE: %s\n" "$$"; } | x ui rotate -n 5
    fi

    local exit_code=$?
    [ "$exit_code" = 0 ] || zuz:error "EXIT [code=$exit_code] ==> $*"
    return $exit_code
}

