# shellcheck shell=dash disable=2034

xrc arg
xrc:mod:lib     fsiter      ls util other

___x_cmd_fsiter___main(){
    arg:init fsiter 'x fsiter -h'
    [ "$#" -gt 0 ] || {
        ___x_cmd_fsiter___ls_main
        return
    }

    local op="$1";  shift
    case "$op" in
        --help|-h)      M='fsiter' help:ret:0                   ;;
        --dfs)          ___x_cmd_fsiter___dfs           "$@"    ;;

        --filecount)    ___x_cmd_fsiter___filecount     "$@"    ;;
        --dirempty)     ___x_cmd_fsiter___dirempty      "$@"    ;;
        --exist)        ___x_cmd_fsiter___exist         "$@"    ;;
        --count_)       ___x_cmd_fsiter___count_        "$@"    ;;

        --file_|--file1_|--file01_|--dir_|--dir1_|--dir01_)
                        ___x_cmd_fsiter___"${op#--}"    "$@"    ;;
        --ls)           ___x_cmd_fsiter___ls_main       "$@"    ;;
        *)              ___x_cmd_fsiter___ls_main "$op" "$@"    ;;
    esac
}


# TODO: We will add file path in the future.
if [ zsh != "$___X_CMD_SHELL" ]; then
___x_cmd_fsiter___dfs(){
    local ___x_cmd_fsiter_path="$1"
    local ___x_cmd_fsiter_depth="${2:-0}"
    local ___x_cmd_fsiter_maxdepth="${3:-0}"
    local ___x_cmd_fsiter_callback="$4"
    [ -d "$___x_cmd_fsiter_path" ]     || M="Please provide directory path" arg:ret:64
    [ -n "$___x_cmd_fsiter_callback" ] || M="Please provide callback" arg:ret:64

    [ "$___x_cmd_fsiter_depth" -le "$___x_cmd_fsiter_maxdepth" ] || return 0

    local i
    for i in "$___x_cmd_fsiter_path"/*; do
        [ -e "$i" ] || continue
        "$___x_cmd_fsiter_callback" "$i" || return $?
        [ -d "$i" ] || continue
        ___x_cmd_fsiter___dfs \
            "$i" $((___x_cmd_fsiter_depth + 1)) \
            "$___x_cmd_fsiter_maxdepth" \
            "$___x_cmd_fsiter_callback" || return $?
    done
}

else
# Section: zsh fsiter dfs
___x_cmd_fsiter___dfs(){
    local ___x_cmd_fsiter_path="$1"
    local ___x_cmd_fsiter_depth="${2:-0}"
    local ___x_cmd_fsiter_maxdepth="${3:-0}"
    local ___x_cmd_fsiter_callback="$4"
    [ -d "$___x_cmd_fsiter_path" ]     || M="Please provide directory path" arg:ret:64
    [ -n "$___x_cmd_fsiter_callback" ] || M="Please provide callback" arg:ret:64

    [ "$___x_cmd_fsiter_depth" -le "$___x_cmd_fsiter_maxdepth" ] || return 0

    local i
    ___x_cmd_fsiter___dfs_print \
        "$___x_cmd_fsiter_path" \
        "$___x_cmd_fsiter_depth" \
        "$___x_cmd_fsiter_maxdepth" | while read -r i ; do
        "$___x_cmd_fsiter_callback" "$i" || return $?
    done
}

___x_cmd_fsiter___dfs_print()(
    local ___x_cmd_fsiter_path="$1"
    local ___x_cmd_fsiter_depth="${2:-0}"
    local ___x_cmd_fsiter_maxdepth="${3:-0}"
    [ -d "$___x_cmd_fsiter_path" ]     || M="Please provide directory path" arg:ret:64
    [ "$___x_cmd_fsiter_depth" -le "$___x_cmd_fsiter_maxdepth" ] || return 0

    set +o nomatch
    local i
    for i in "$___x_cmd_fsiter_path"/*; do
        [ -e "$i" ] || continue
        printf "%s\n" "$i"
        [ -d "$i" ] || continue
        ___x_cmd_fsiter___dfs_print \
            "$i" $((___x_cmd_fsiter_depth + 1)) \
            "$___x_cmd_fsiter_maxdepth" || return $?
    done
)

# EndSection

fi
