#! /bin/bash
#
# Usage: vyatta-link-detect devicename on|off
#
# Copyright (c) 2019, AT&T Intellectual Property.
# Copyright (c) 2014 by Brocade Communications Systems, Inc.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-only

usage() {
    echo "Usage: $0 devicename {on|off}"
    exit 1
}

if [ $# -ne 2 ]; then
    usage
fi

# Note can't use sysctl it is broken for vlan name because of dots
# link_filter values:
#   0 - always receive
#   1 - ignore receive if admin_down
#   2 - ignore receive if admin_down or link down
set-sysctl () {
    (echo "$2" > "/proc/sys/net/ipv4/conf/$1/link_filter") 2> /dev/null
}

case $2 in
on)	set-sysctl "$1" 2
	exec vtysh -c "configure terminal" -c "interface $1" \
	    -c "link-detect" ;;
off)    set-sysctl "$1" 1
	exec vtysh -c "configure terminal" -c "interface $1" \
	    -c "no link-detect" ;;
*)	usage;;
esac

