
# x ip iter 192.168.0.0/25


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

    case "$1" in
        -h|--help)      ___x_cmd help -m ip iter "$@"; return 0 ;;
    esac

    local cidr="$1"
    local ip
    local mask

    case "$cidr" in
        */*)        ip="${cidr%/*}"; mask=${cidr##*/} ;;
        *)          N=ip M="Unsuported -> $cidr" log:ret:64
    esac

    local x_
    local count;    ___x_cmd_ip_count_ "$mask";  count=$x_

    if ___x_cmd_is_stdout2tty && [ "$count" -gt 32 ]; then
        ___x_cmd_ip_iter___run | ___x_cmd_cmds head -n 16
        ip:info \
            --tip       "Command to view all combination using less -> x ip iter $cidr | less" \
            --tip-fzf   "Command to fzf all combination -> x ip iter $cidr | x fzf" \
            "TTY output limited to 16 results to prevent screen flush. Total IP in [cidr=$cidr] -> $count"
    else
        ___x_cmd_ip_iter___run
    fi
}

___x_cmd_ip_iter___run(){
    ___x_cmd_ip_toint_ "$ip"
    local end="$(( x_ + count ))"

    if [ "$count" -gt 256 ]; then
        ___x_cmd_ip_fromint_awk "$x_" "$end"
        return
    fi

    local i=$x_
    while [ "$i" -lt $end ]; do
        ___x_cmd_ip_fromint "$i"
        i="$((i+1))"
    done
}

___x_cmd_ip_count(){
    local x_
    ___x_cmd_ip_count_ "$@" || return
    printf "%s\n" "$x_"
}

___x_cmd_ip_count_(){
    local count="$1"

    case "$count" in
        */*)        count=${count##*/} ;;
    esac

    x_=$(( 1 << ( 32 - count ) ))
}
