#!/bin/sh
#
#	$Id: ifdown,v 1.28 2001/05/16 13:12:07 baggins Exp $
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /etc/sysconfig/network
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network-scripts/.functions

# Will be removed in the future
if is_yes "$NETWORKING" ; then
	if [ -z "$IPV4_NETWORKING" ] ; then
		echo "NETWORKING is set to YES, but IPV4_NETWORKING is empty!"
		echo "Please upgrade your config"
		echo "Assuming you want IPv4 networking"
		IPV4_NETWORKING=yes
	fi
fi

DEV=$1

[ -z "$DEV" ] && {
    nls "Usage: %s <device name>" "ifdown" >&2
    exit 1
}

if [ -f "/etc/sysconfig/interfaces/ifcfg-$DEV" ] ; then
	CONFIG="/etc/sysconfig/interfaces/ifcfg-$DEV"
else
	CONFIG="$DEV"
fi

if [ "$(id -u)" != "0" ]; then
    if [ -x /sbin/usernetctl ]; then
        exec /sbin/usernetctl $CONFIG down
    fi
    echo "Users cannot control this device." >&2
    exit 1
fi

source_config

# set all major variables
setup_ip_param

OTHERSCRIPT="/etc/sysconfig/network-scripts/ifdown-${DEVICETYPE}"

# shutdown tleds software
if [ "$TLEDS_DEV" = "$DEVICE" -a -x /usr/bin/tleds ]; then
	/usr/bin/tleds -qk "$DEVICE"
fi
	
if [ -x $OTHERSCRIPT ]; then
    if [ "$HANDLING" = "0" ]; then
    	exec $OTHERSCRIPT $CONFIG $2
    elif [ "$HANDLING" = "1" ]; then
    	$OTHERSCRIPT $CONFIG $2
    fi
fi

# Check to make sure the device is actually up
check_device_down && exit 0

if [ "$BOOTPROTO" = "bootp" -o "$BOOTPROTO" = "pump" ]; then
	/sbin/pump -r -i ${DEVICE}
	RESULT=$?
fi

if [ "$BOOTPROTO" = "dhcp" ]; then
    if [ -x /sbin/dhcpcd ];then
        /sbin/dhcpcd -k ${DEVICE}
	RESULT=$?
    elif [ -x /sbin/dhclient ];then
        if [ -f /var/run/dhclient.pid ];then
            PID=$(cat /var/run/dhclient.pid)
            if ps ax --no-header|grep -q $PID;then
                kill $PID
		RESULT=$?
            fi
        fi
    elif [ -x /sbin/dhcpxd ];then
        /sbin/dhcpxd -k ${DEVICE}
	RESULT=$?
    elif [ -x /sbin/pump ];then
        pump -r -i ${DEVICE}
	RESULT=$?
    fi
fi

ip link set ${DEVICE} down
ip addr flush dev ${DEVICE} 2>&1 | grep -v "Nothing to flush"

if [ "$HANDLING" = "4" ]; then
    exit 0
fi

if [ "$RESULT" -ne "0" ]; then
	return $RESULT
fi

exec /etc/sysconfig/network-scripts/ifdown-post $CONFIG

# This must be last line !
# vi:syntax=sh:tw=78:ts=8:sw=4
