# shellcheck shell=dash disable=SC2016

# If there is a mac address with the more than 1 ip, high light it.

xrc:mod:lib route

___x_cmd_route_ls(){

    local format=app

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)          ___x_cmd help -m arp ls ; return 0 ;;
            -c|--csv)           format=csv; shift        ;;
            -t|--tsv)           format=tsv; shift        ;;
            -r|--raw)           format=raw; shift        ;;
            --auto|--app)       format="${1#--}"; shift  ;;
            -*)                 N=route M="Unknown option -> $1" log:ret:64 ;;
            *)                  break ;;
        esac
    done

    ___x_cmd_route_ls_"$format" "$@"
}


___x_cmd_route_ls_raw(){
    if ___x_cmd_hascmd netstat; then
        if ___x_cmd os is win; then
            "${___X_CMD_ROOT_MOD}/route/lib/ls/netstat.bat" -rn
        else
            ___x_cmd_cmds netstat -rn
        fi
    elif ___x_cmd os is linux; then
        route:info "netstat command not found, use the busybox's netstat command"
        ___x_cmd busybox netstat -rn
    else
        N=route M="Command not found -> netstat" log:ret:1
    fi
}

# unify to title: netif expire/metric flags destiation gateway

___x_cmd_route_ls_xsv(){
    ___x_cmd_route_ls_raw | {
        if ___x_cmd os is win; then
            ___x_cmd_cmds awk -v OFS="$1" \
                -f "$___X_CMD_ROOT_MOD/route/lib/ls/parse_win_xsv.awk"
        else
            ___x_cmd_cmds awk -v OFS="$1" \
                -f "$___X_CMD_ROOT_MOD/route/lib/ls/parse_xsv.awk"
        fi
    }
}

___x_cmd_route_ls_csv(){
    ___x_cmd_route_ls_xsv ","
}

___x_cmd_route_ls_tsv(){
    ___x_cmd_route_ls_xsv "\t"
}

___x_cmd_route_ls_auto(){
    if      ___x_cmd_is_stdout2tty; then        ___x_cmd_route_ls_app
    else                                        ___x_cmd_route_ls_tsv
    fi
}

___x_cmd_route_ls_app(){
    ___x_cmd_route_ls_csv | ___x_cmd csv app
}

___x_cmd_route_ls_app___colorize(){
    ___x_cmd_cmds cat
}
