# shellcheck shell=dash

xrc:mod:lib     dns     set/linux set/darwin set/win

___x_cmd_dns_set(){
    [ $# -gt 0 ] || {
        N=dns M="Usage: x dns set [--file|--dry-run] [target] <ip> [ip ...]" log:ret:64
    }

    local mode="default"
    local no_v4=0 no_v6=0
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)  ___x_cmd help -m dns set; return 0 ;;
            -f|--file)   mode="file";   shift ;;
            -d|--dry-run) ___X_CMD_DNS_DRY_RUN=1; shift ;;
            --no-ipv4)   no_v4=1; shift ;;
            --no-ipv6)   no_v6=1; shift ;;
            --) shift; break ;;
            -*) N=dns M="Unknown option ==> $1" log:ret:64 ;;
            *)  break ;;
        esac
    done

    [ "$no_v4" = 1 ] && ___X_CMD_DNS_NO_V4=1
    [ "$no_v6" = 1 ] && ___X_CMD_DNS_NO_V6=1
    export ___X_CMD_DNS_NO_V4 ___X_CMD_DNS_NO_V6
    [ -n "$___X_CMD_DNS_DRY_RUN" ] && export ___X_CMD_DNS_DRY_RUN

    if [ "$mode" = "file" ]; then
        ___x_cmd os name_
        if [ "$x_" = "linux" ]; then
            ___x_cmd_dns_set___linux_file "$@"
            return $?
        else
            dns:info "(--file is Linux-only; falling through to system-wide set on $x_)"
            mode="default"
        fi
    fi

    local x_
    ___x_cmd os name_
    case "$x_" in
        linux)      ___x_cmd_dns_set___linux   "$@" ;;
        darwin)     ___x_cmd_dns_set___darwin  "$@" ;;
        win)        ___x_cmd_dns_set___win     "$@" ;;
        *)          N=dns M="Unsupported os ==> $x_" log:ret:64 ;;
    esac
}

___x_cmd_dns_set___looks_like_ip(){
    case "$1" in
        *:*)        return 0 ;;
        *.*.*.*)   return 0 ;;
        *)          return 1 ;;
    esac
}
