#!/bin/bash
#
# Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
#
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: LicenseRef-NVIDIA-Proprietary
#
source "$(dirname "$0")/_lib.sh"
trace "$@"
TARGET="$1"
if [[ "$TARGET" == pci/* ]]; then
    BDF=$(bdf_from_target "$TARGET")
    IFACE=$(iface_from_bdf "$BDF")
else
    IFACE="$TARGET"
fi
[[ -z "$IFACE" ]] && { echo "Error: no interface for $TARGET" >&2; exit 1; }

# YANG admin-state -> ip-link command-arg decode.
# libdms post-conversion: up=1, down=2 (YANG explicit values).
# `ip link set dev <IFACE> <ARG>` accepts only "up"/"down" — the int form
# would be a syntax error. Decode here. Same table shape as
# mlxreg-port-admin-set; see that file's header comment for the rationale.
declare -A YANG_ENUM=(
    ["admin-status:1"]="up"
    ["admin-status:2"]="down"
    ["admin-status:up"]="up"
    ["admin-status:down"]="down"
)
VAL="$2"
DECODED="${YANG_ENUM[admin-status:$VAL]:-}"
if [[ -z "$DECODED" ]]; then
    echo "Error: ip-link-admin-set: bad admin-status value '$VAL' (expected up/down/1/2)" >&2
    exit 1
fi

if ! err=$(ip link set dev "$IFACE" "$DECODED" 2>&1); then
    echo "Error: ip link set dev $IFACE $DECODED failed: $err" >&2
    exit 1
fi
echo "admin-status=${DECODED}"
