#! /bin/sh
#
#  Copyright (C) 1997-2005  Dimitrios P. Bouras
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   For author contact information, look in the README file.
 
# The following variables are adjusted automatically by 'make install'
#----------------------------------------------------------------------
NetGroup=dialer
XispID=//tmp/pkgbuild/comms/xisp/work.vax/.destdir/usr/pkg/bin/xispid

# First setup all variables passed on from pppd
#-----------------------------------------------
interface=$1
device=$2
speed=$3
localIP=$4
remoteIP=$5
ipparam=$6
 
# Bail out if no ipparam
#------------------------
if [ "$ipparam"x = x ]; then
	exit 0
fi

# Then parse the ipparam argument string passed on from xisp
#------------------------------------------------------------
pipefname=`echo $ipparam | sed -e 's/\:.*$//'`
temp=`echo $ipparam | sed -e 's/^[^:]*\://'`
ISPname=`echo $temp | sed -e 's/\:.*$//'`
temp=`echo $temp | sed -e 's/^[^:]*\://'`
if [ "$DNS1"x = x ]; then					# we don't want to clobber
	DNS1=`echo $temp | sed -e 's/\:.*$//'`	# peer-assigned DNS servers
	xispDNS1=1								# keep track of server(s) added
else										# by pppd versus servers added
	xispDNS1=0								# by xisp in order to know what
fi											# to do further down
temp=`echo $temp | sed -e 's/^[^:]*\://'`
if [ "$DNS2"x = x ]; then
	DNS2=`echo $temp | sed -e 's/\:.*$//'`
	xispDNS2=1
else
	xispDNS2=0
fi
domain=`echo $temp | sed -e 's/^.*\://'`

# Make sure that the user who started pppd is indeed a member of $NetGroup
# If not, then this is most probably a system security attack, so bail out
# Also run xispid security checks on the named-pipe file name and modes
#--------------------------------------------------------------------------
userOK=0
for gname in `$XispID -rGn`; do
	if [ "$gname" = "$NetGroup" ]; then
		userOK=1
	fi
done
if [ $userOK -eq 1 ]; then
	if ! `$XispID -c $pipefname`; then
		exit 0
	fi
else
	exit 0
fi

# The following command redirects all stdout/stderr output (from this
# script or from any command run within it) to the xisp browser window
#----------------------------------------------------------------------
exec 1>>$pipefname 2>&1

# The following is guaranteed to work under SuSE Linux 8.2 & 9.0.
# If you modify it for another Linux distribution, please send a
# short message and I'll merge your changes with the official xisp
# release. The fragment checks if the system /etc/ppp/ip-up will
# adjust the DNS servers via the /sbin/modify_resolvconf script.
#------------------------------------------------------------------
SystemDNS=0
RslvStr='MODIFY_RESOLV_CONF_DYNAMICALLY'
BindStr='MODIFY_NAMED_CONF_DYNAMICALLY'
if [ -r /etc/sysconfig/network/config ]; then
	eval `grep "^[[:space:]]*\($RslvStr\|$BindStr\)=" \
		  /etc/sysconfig/network/config 2>/dev/null`
	if [ "$MODIFY_RESOLV_CONF_DYNAMICALLY" = "yes" -o \
		 "$MODIFY_NAMED_CONF_DYNAMICALLY" = "yes" ] ; then
		SystemDNS=1
	fi
fi

# Remove the lines in /etc/resolv.conf for the DNS entries passed from xisp
# or from pppd (via the "usepeerdns" option, as returned from our peer)
#---------------------------------------------------------------------------
umask 022
if [ "$DNS1"x != x ]; then
	if [ $xispDNS1 -eq 1 ]; then
		grep -v "xisp primary DNS" /etc/resolv.conf > /etc/resolv.conf.new
		mv -f /etc/resolv.conf.new /etc/resolv.conf
	elif [ $SystemDNS -eq 0 ] ; then
		grep -v "peer-assigned DNS1" /etc/resolv.conf > /etc/resolv.conf.new
		mv -f /etc/resolv.conf.new /etc/resolv.conf
	fi
	echo "DNS $DNS1 removed"
fi
if [ "$DNS2"x != x ]; then
	if [ "$DNS2" != "$DNS1" ]; then
		if [ $xispDNS2 -eq 1 ]; then
			grep -v "xisp secondary DNS" /etc/resolv.conf > /etc/resolv.conf.new
			mv -f /etc/resolv.conf.new /etc/resolv.conf
		elif [ $SystemDNS -eq 0 ] ; then
			grep -v "peer-assigned DNS2" /etc/resolv.conf > /etc/resolv.conf.new
			mv -f /etc/resolv.conf.new /etc/resolv.conf
		fi
		echo "DNS $DNS2 removed"
	fi
fi

# Remove the line for domain name in /etc/resolv.conf passed from xisp
#----------------------------------------------------------------------
if [ "$domain"x != x ]; then
	grep -v "xisp domain line" /etc/resolv.conf > /etc/resolv.conf.new
	mv -f /etc/resolv.conf.new /etc/resolv.conf
	echo "Removed domain $domain"
fi

# Restart nscd if it was running
#--------------------------------
if [ "$DNS1"x != x -o "$DNS2"x != x ]; then
	if [ -x /etc/init.d/nscd ]; then
		NSCD=/etc/init.d/nscd;
	elif [ -x /etc/rc.d/nscd ]; then
		NSCD=/etc/rc.d/nscd;
	elif [ -x /etc/rc.d/init.d/nscd ]; then
		NSCD=/etc/rc.d/init.d/nscd;
	else
		NSCD=''
	fi
	if [ "$NSCD"x != x ]; then
		if [ -x $NSCD ]; then
			if $NSCD stop > /dev/null 2>&1; then
				sleep 1
				$NSCD start > /dev/null 2>&1
				echo "Restarted nscd"
			fi
		fi
	fi
fi

# The ISPname variable can now be used to carry out global and/or
# ISP-specific tasks like downloading mail and/or news. The folowing
# statements are examples. Modify them to best suit your needs.
#--------------------------------------------------------------------

# echo "Interface $interface is now DOWN"
# echo "On $device at $speed Baud"
# echo "Local IP: $localIP"
# echo "Remote IP: $remoteIP"

case $ISPname in

	'My First Provider')
		echo "Provider 1"
		;;

	'My Second Provider')
		echo "Provider 2"
		;;

	*)
		;;
esac

