#!/usr/bin/bash
# SPDX-License-Identifier: GPL-2.0

# Input: Name of the interface
#
# Output: The script prints the string "Enabled" to stdout to indicate
#	that DHCP is enabled on the interface. If DHCP is not enabled,
#	the script prints the string "Disabled" to stdout.
#
# RHEL specific implementation, use NetworkManager information.

[ ! -z "$1" ] || exit 1

nmcon=$(nmcli -g GENERAL.CONNECTION device show "$1" 2>/dev/null)

if [ -z "$nmcon" ]; then
echo "Unknown"
exit 0
fi

ipv4=$(nmcli --fields ipv4.method connection show "$nmcon" 2>/dev/null | cut -d ':' -f 2 | xargs echo -n)
ipv6=$(nmcli --fields ipv6.method connection show "$nmcon" 2>/dev/null | cut -d ':' -f 2 | xargs echo -n)

if [ "$ipv4" = "auto" ] || [ "$ipv6" = "auto" ]; then
echo "Enabled"
else
echo "Disabled"
fi
