#!/bin/sh
#####################################################################
# /sbin/ifup-local
#
# ChanYoon Jung			2002/09/19
# h2y					2004/05/14
#
# DESCIPTION
#	bring up rc.firewall (nat & firewall).
#	log network status
#	send network status notification mail.
#####################################################################

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

CONFIG=$1
[ -f "$CONFIG" ] || CONFIG=ifcfg-$1
source_config

# Do it if eth0/eth1(static/dynamic/pppoe), ppp2(ppp client) 
if [ -x /etc/rc.d/init.d/syipfilter ]; then
	/etc/rc.d/init.d/syipfilter restart >/var/log/syipfilter.log 2>/var/log/syipfilter.err
fi

if [ "$DEVICE" = "eth0" ]; then
#	# Restart nat & firewall
#	/etc/rc.d/init.d/firewall restart >/var/log/firewall.log 2>/var/log/firewall.err

	# Find external ip address
	EXTIF=`/sbin/route -n | grep ^"0.0.0.0" | awk '{print $NF}'`
	EXTIP=
	if [ -n "$EXTIF" ] ; then
		EXTIP="`ifconfig $EXTIF | grep 'inet addr' | \
		awk '{print $2}' | sed -e 's/.*://'`"
	fi

	EXTIP_LOGFILE=/var/log/extip.log
	EXTIP_FILE=/var/run/extip
	if [ -n "$EXTIP" -a "$EXTIF" != "eth0" ] ; then
		# Log external ip address
		echo "External IP Address : $EXTIP" > $EXTIP_LOGFILE
		echo "External Interface : $EXTIF" >> $EXTIP_LOGFILE
		echo >> $EXTIP_LOGFILE
		ifconfig $EXTIF >> $EXTIP_LOGFILE
	
		# Send report mail	
		OLD_EXTIP="`cat $EXTIP_FILE 2>/dev/null`"
		if [ ! -f "$EXTIP_FILE" -o "$EXTIP" != "$OLD_EXTIP" ]; then
			MAIL_SUBJECT="New External IP Address"
			MAIL_CFGFILE=/etc/sysconfig/netreport.conf
			/usr/sbin/smail "$MAIL_SUBJECT" "$EXTIP_LOGFILE" "$MAIL_CFGFILE" &	
		fi
		
		# Mark external ip address		
		echo "$EXTIP" > $EXTIP_FILE
	fi
fi

exit 0
