#!/bin/sh

#
# Things to do after bringing up network
#

#
# The main script
#

PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Get device for which we are being called
DEVICE=$1

# Check arg
[ "${DEVICE}" = "" ] && exit 1

# Get device type, real device name and if is an alias
DEVICETYPE=`echo ${DEVICE} | sed -e 's/[0-9]*$//'`
REALDEVICE=`echo $DEVICE | sed 's/:.*//g'`
if echo $DEVICE | grep -q ':' ; then
    ISALIAS=yes
else
    ISALIAS=no
fi

# We only do stuff for real devices, not aliases
[ "${ISALIAS}" = "yes" ] && exit 0

# For ethernet, start link status checking to avoid having blocking
# connections if link is down (runs as daemon).
if [ "${DEVICETYPE}" = "eth" ]; then
    iflinkmon ${DEVICE}
fi

# This should be in the ifup-post script... (bug in initscripts package)
# If nscd is running and /etc/resolv.conf was modified after startup, restart
# Actually, this does not seem to work correctly => always restart nscd
#if [ -f /var/lock/subsys/nscd -a \
#	/etc/resolv.conf -nt /var/lock/subsys/nscd ]; then
    # Make nscd aware of new name resolver settings
    [ -x /etc/init.d/nscd ] && /etc/init.d/nscd condrestart
    # Fixup for incorrectly failing initscripts (not needed with wrapper)
    /sbin/service nscd status > /dev/null 2>&1 && \
    	touch /var/lock/subsys/nscd
#fi

# Sendmail does not use nscd, so we need to restart it so that it picks up
# the updated /etc/resolv.conf file (if it changed). Can restart with SIGHUP.
# If not restarting sendmail, make sure to send all accumulated e-mail
if [ -f /var/lock/subsys/sendmail ]; then
    if [ /etc/resolv.conf -nt /var/lock/subsys/sendmail ]; then
	if [ -f /var/run/sendmail.pid ]; then
	    kill -HUP `sed -e '/^[0-9]\+$/ p; d' /var/run/sendmail.pid`
	    touch /var/lock/subsys/sendmail
	fi
    elif [ "${DEVICE}" != "lo" ]; then
	# Detach from input and output so that parent processes never
	# waits on this one with a zombie shell.
	/usr/sbin/sendmail -q < /dev/null > /dev/null 2>&1 &
    fi
fi

# If lo make an alias that is dummy of this compter's IP address
# so that the address works, even when the network interface is disconnected
# We should make this as a real alias, but the problem is that a route
# will be added for that address, which we don't want since that can
# interfere with the real device in some cases.
# The same result can also be achieved with a file
# /etc/sysconfig/network-scripts/ifcfg-lo:0
# with the following contents:
#NAME=loopback-alias
#DEVICE=lo:0
#IPADDR=<your IP address>
#NETMASK=255.255.255.255
#BROADCAST=<your IP address>
if [ "${DEVICE}" = "lo" ]; then
    # use eval to get rid of trailing space
    eval HOSTIPADDR=`/bin/hostname -i`
    if [ -n "${HOSTIPADDR}" ] ; then
	ip addr add ${HOSTIPADDR}/32 dev lo label lo:0
    fi
fi

# Done
exit 0
