-conf=/etc/NetworkManager/system-connections/ethusb-static.nmconnection
-if ! $force && [[ -s $conf ]] && grep -qFx "$comment" $conf; then
- # we already ran successfully in the past to set things this way, so
- # do nothing.
+con_exists=false
+if nmcli con | grep -q "^$nm_con " &>/dev/null; then
+ con_exists=true
+fi
+
+declare -a args
+if $off; then
+
+ if ! $con_exists; then
+ echo "warning: no existing connection: $nm_con found in output of nmcli con"
+ exit 0
+ fi
+
+
+ tmpstr=$(nmcli con show "$nm_con" 2>/dev/null | sort -r | awk '$1 == "ipv4.method:" || $1 == "ipv4.addresses:" || $1 == "ipv4.gateway:" || $1 == "ipv4.dns:" || $1 == "GENERAL.STATE:" {print $2}' )
+ {
+ read -r ipv4_method
+ read -r ipv4_gateway
+ read -r ipv4_dns
+ read -r ipv4_addresses
+ read -r state
+ }<<<"$tmpstr"
+
+ reup=false
+ if [[ $state == activated ]]; then
+ reup=true
+ fi
+
+ if [[ $ipv4_method != auto ]]; then
+ args+=(ipv4.method auto)
+ fi
+ if [[ $ipv4_addresses != -- ]]; then
+ args+=(-ipv4.addresses "$ipv4_addresses")
+ fi
+ if [[ $ipv4_dns != -- ]]; then
+ args+=(-ipv4.dns "$ipv4_dns")
+ fi
+ if [[ $ipv4_gateway != -- ]]; then
+ # undocumented in t11 man nmcli. guessed randomly
+ args+=(ipv4.gateway 0.0.0.0)
+ fi
+ if (( ${#args[@]} >= 1 )); then
+ m nmcli con mod "$nm_con" "${args[@]}"
+ if $reup; then
+ m nmcli con up "$nm_con"
+ fi
+ else
+ echo "$0: found expected state, nothing to do."
+ fi