#!/bin/sh

cd /etc/sysconfig/network-scripts
. ./network-functions

[ -f ../network ] && . ../network

unset REALDEVICE
if [ "$1" = --realdevice ] ; then
    REALDEVICE=$2
    shift 2
fi

CONFIG=$1
source_config

[ -z "$REALDEVICE" ] && REALDEVICE=$DEVICE

if [ "$ISALIAS" = no ] ; then
    /etc/sysconfig/network-scripts/ifup-aliases ${DEVICE} ${CONFIG}
fi

/etc/sysconfig/network-scripts/ifup-routes ${REALDEVICE} ${DEVNAME}

if [ -n "$DNSDEV" ]; then
    if [ "$DNSDEV" = "$DEVICE" ] ; then
        PEERDNS="yes"
    else
        PEERDNS="no"
    fi
fi

if [ -n "$PEERDNS" -a "$PEERDNS" != "no" -o -n "$RESOLV_MODS" -a "$RESOLV_MODS" != "no" ]; then

  # write resolv.conf (note: this is done by dhclient if BOOTPROTO=dhcp)
  if [ "$BOOTPROTO" != dhcp ] ; then 
    # backup resolv.conf
    cp -af /etc/resolv.conf /etc/resolv.conf.save
    tr=`mktemp /tmp/XXXXXX`

    # DNS search path
    echo "; generated by $0" > $tr
    if [ -n "$DOMAIN" ] ; then
	echo "search $DOMAIN" >> $tr
    else
	# CA-38371: For most apps it is harmless to have domains in resolv.conf that may be unknown to the DNS servers
	# However, likewise tries each domain in resolv.conf in turn when joining an AD domain and takes
	# a minute to time out if the DNS domain cannot be resolved.
	# This should be fixed in likewise, but for now do not preserve domain/search line if not
	# specified for this interface.
	# egrep '^(domain|search)' /etc/resolv.conf >> $tr
	:
    fi

    # DNS server list
    i=1
    while true ; do
	SERVER=`eval echo -n "\\$MS_DNS$i"` 
	if [ -n "$SERVER" ] ; then
	    echo "nameserver $SERVER" >> $tr
	else
	    SERVER=`eval echo -n "\\$DNS$i"` 
	    if [ -n "$SERVER" ] ; then
		echo "nameserver $SERVER" >> $tr
	    else
		break
	    fi
	fi
	i=$(($i+1))
    done
    
    # maintain permissions
    # but set umask in case it doesn't exist!
    oldumask=`umask`
    umask 022
    change_resolv_conf $tr 
    rm -f $tr
    umask $oldumask
  fi
fi

# don't set hostname on ppp/slip connections
if [ "$2" = "boot" -a \
     "${DEVICE}" != lo -a \
     "${DEVICETYPE}" != "ppp" -a \
     "${DEVICETYPE}" != "slip" ]; then
    if need_hostname; then
	IPADDR=`LANG= LC_ALL= ifconfig ${DEVICE} | grep 'inet addr' | 
		awk -F: '{ print $2 } ' | awk '{ print $1 }'`
	eval `/bin/ipcalc --silent --hostname ${IPADDR}`
	if [ "$?" = "0" ]; then
	    set_hostname $HOSTNAME
	fi
    fi
fi

# Notify programs that have requested notification
do_netreport

if [ -x /sbin/ifup-local ]; then
    /sbin/ifup-local ${DEVICE}
fi

exit 0
