#!/bin/ksh

# This script is to stop all the whining about
# "setting up asppp is too hard!!! whine whine..."
#
# written by phil@bolthole.com

# FUTURE NOTES:
# should test the line
# should TRY to selfdiagnose. For example: /etc/log/asppp.log
#  check for Call Failed: CAN'T ACCESS DEVICE
#


version='v1.4.1'

echo "configppp.sh, by phil@bolthole.com: http://www.bolthole.com/solaris/"

echo version $version
OSREV=`uname -r`

CONFIGFILE=/etc/configppp.cf

#because older bundled PPP versions sucked, we dont try
#to run on older systems.

case $OSREV in 
	5.2)
		echo "OS too old. sorry"
		exit
	;;
	5.3)
		echo "OS too old. sorry"
		exit
	;;
	5.4)
		echo "OS too old. sorry"
		exit
	;;
esac


#val=ROOT
val=`id | sed 's/uid=0(.*/ROOT/'`
if [ "$val" != "ROOT" ] ; then
	echo ""
	echo "Sorry, you are expected to be root to run this script"
	exit 1
fi
#
#Skip down to next set of ####, if you care about main stuff.
############################################################
# we 'echo' our return value. So we have to prompt out to stderr
getvalue()
{
	echo $1 >/dev/fd/2
	echo "Enter new value, or press return to keep default" >/dev/fd/2
	read line
	echo $line
}

#specialized version of getvalue()
# [we 'echo' our return value. So we have to prompt out to stderr]
# give it the message, and default value of a variable
# It will prompt the user
# It will either
#  - print out a new value for the param and return 0, or
# - return 1 (which means, stop here)
# It will return 1 **if**
#     - the user types "end"
#     - the user enters nothing, and there is no default
getchat()
{
	msg="$1"
	default="$2"

	echo $msg "[$default]">/dev/fd/2
	read line
	if [ "$line" = "end" ] ; then
		return 1
	fi
	if [ "$line" = "" ] ; then
		if [ "$default" = "" ] ; then
			return 1
		fi
		echo $default
		return 0
	fi
	
	echo $line
	return 0
}


############################################################


if [ -f $CONFIGFILE ] ; then
	. $CONFIGFILE
fi
MODEMDEV=${MODEMDEV:="cua/a"}

echo Starting automated setup for single ppp connection to an ISP
echo "We assume the following:"
echo "*** I am not to be held resposible for any errors in this script that result"
echo "** in damage to your system in any way"
echo ""
echo "* You haven't messed around with the /etc/uucp, or /etc/asppp.cf stuff"
echo "* You already know what the login strings look like for your ISP,"
echo "    not to mention the appropriate dialin number"
echo ""

echo checking for components

if [ ! -f /etc/asppp.cf ] ; then
	echo You do not have all the ppp packages installed. Cannot continue
	exit
fi
if [ ! -f /etc/uucp/Systems ] ; then
	echo 'You do not have all the uucp packages installed.["Basic Networking"]'
	echo 'Cannot continue.'
	exit
fi

echo 'Checking for backup files. Do not ever remove any files named *pre_ppp'

for file in /etc/asppp.cf /etc/uucp/Systems /etc/uucp/Devices /etc/uucp/Dialers ; do
	if [ ! -f ${file}-pre_ppp ] ; then
		echo making backup of $file
		cp $file ${file}-pre_ppp
	fi
done
echo ""


LOCALNAME=${LOCALNAME:-mypppside}
#we assume dynamic ppp in this day and age
# 1.2.3.4 is the sun-given "dummy" IP suggestion
LOCALIP=${LOCALIP:-1.2.3.4}

echo "####################"
echo NOTICE:
echo 'We are calling the remote side gateway/router address "ispside". '
echo We will override any other entry you may have for host '"ispside"'
echo ""
REMOTENAME=${REMOTENAME:-ispside}


# This needs to be non-wierd, so we can ping it if need be
REMOTEIP=${REMOTEIP:-5.6.7.8}


ans=`getvalue "Current ISP-side gateway/router IP addr is $REMOTEIP"`
REMOTEIP=${ans:=$REMOTEIP}

if [[ "$REMOTEIP" = "5.6.7.8" ]] ; then
echo " YOU MUST HAVE THIS VALUE CORRECT, for things to fully work."
echo " However, if all you know is the phone number and login/password,"
echo " just continue this script anyway, and afterwards, ping ispside."
echo " Eventually, the PPP link will come up. You must note the output from "
echo "ifconfig ipdptp0"
echo " You should then see a line like"
echo "  1.1.1.1 -> 204.123.321.222"
echo "In this case, '204.123.321.222' is the TRUE VALUE for ispside."
echo "Write it down as the value for ispside, and run this script again."
echo ""
echo " but for now, to continue..."
fi 


echo ""

echo "If you do not know the DNS IP address for your ISP, just use the same as"
echo "the remote-side router IP address"
DNS=${DNS:=$REMOTEIP}
ans=`getvalue "What is your ISP's DNS server IP address? ($DNS)"`
DNS=${ans:=$DNS}

echo ""
echo "Do not bother to change the following value unless you KNOW you have a"
echo "static IP address. You probably do NOT have a static IP nowadays"
ans=`getvalue "Current local-side IP addr is $LOCALIP"`
LOCALIP=${ans:=$LOCALIP}

ISPNUMBER=${ISPNUMBER:="No default"}

echo ""

echo "You should generally try 'cua/a' or 'cua/b' for modem device"
ans=`getvalue "What modem device would you like to use? ($MODEMDEV)"`
MODEMDEV=${ans:=$MODEMDEV}

echo "What speed would you like to use?"
cpu=`uname -p`
case $cpu in
	'sparc')
		echo "you're on a sparc. You probably don't want to use"
		echo "higher than 38400, unless you have a new serial port, and/or"
		echo "a special modem"
	;;
	'i86pc'|'i386')
		echo "You're on a PC. You PROBABLY have a serial port that can"
		echo "handle 115200 bps"
	;;
esac

choice=0
while [ $choice -eq 0 ] ; do
	BAUDLIST="9600 19200 38400 57600 76800 115200"
	ans=`getvalue "pick from $BAUDLIST ($BAUDRATE)"`
	if [ "$ans" = "" ] ; then
		ans=$BAUDRATE
	fi
	for bl in $BAUDLIST ; do
		if [ "$ans" = "$bl" ] ; then
			choice=$ans
			BAUDRATE="$ans"
		fi
	done
done

while [ "$ISPNUMBER" = "No default" ] ; do
  ans=`getvalue "What's the telephone dialin number of your ISP? ($ISPNUMBER)"`
  ISPNUMBER=${ans:=$ISPNUMBER}
done



chatdirections()
{
echo "____________________________________________________________"
echo ""
echo "Now we get to the part you'll probably have to play with the most."
echo "It will be easiest if you have dialed in to your ISP's number by hand"
echo "so you know exactly what text to expend and send"
echo ""
echo "EXAMPLE: Lets say your ISP prompts you with"
echo "
       Annex server xxxxxxxx
     Welcome to uber-kontrol ISP

  please login:  [ you type your login here]
  what's your password? [you type your password here]"


echo ""
echo "You would set the first thing we look for as 'gin:'"
echo "since that is the last thing we look for before actually "
echo "sending something back"
echo "Then, set [yourloginname] as what we reply with."
echo "Next, the second thing we look for would be 'ord?'"
echo "which is the last four letter of 'password?'"
echo "After that, set what we reply with, to be your password"
echo ""
echo "Just press return when you don't have anything more, and we'll move"
echo "on to actually setting things up"
echo ""
echo "____________________________________________________________"
}


# This reads in the chat script answers. The trick here is
# if they enter a blank line, and there is no default, we
# then have to null out all the later answers
setchatscript()
{
	echo "Most ISPs require a basic chat script along the following lines"
	echo "(look for):  login"
	echo "(reply with): your-dial-up-name"
	echo "(look for): word"
	echo "(reply with): your-dial-up-password"
	echo ""
	echo "To end the script, or take the default, press return."
	echo "If you want to override a default and quit, enter 'end'"
	echo ""
	EXPECT1=`getchat "What's the first thing we should look for?" $EXPECT1`
	if [ $? -ne 0 ] ; then
	    echo "I sure hope you know what you're doing"
	    EXPECT2="";	    EXPECT3=""
	    EXPECT4="";	    SEND1=""
	    SEND2="";	    SEND3=""
	    SEND4=""
	    return;
	fi

	SEND1=`getchat "What should we reply with?" $SEND1`
	if [ $? -ne 0 ] ; then
	    echo "I sure hope you know what you're doing"
	    EXPECT2="";	    EXPECT3=""
	    EXPECT4="";	    SEND2=""
	    SEND3="";	    SEND4=""
	    return;
	fi

	EXPECT2=`getchat "What's the second thing we should look for?" $EXPECT2`
	if [ $? -ne 0 ] ; then
	    EXPECT3="";	    EXPECT4=""
	    SEND2="";	    SEND3=""
	    SEND4=""
	    return;
	fi
	SEND2=`getchat "What should we reply with?" $SEND2`
	if [ $? -ne 0 ] ; then
	    EXPECT3="";	    EXPECT4=""
	    SEND3="";	    SEND4=""
	    return;
	fi


	EXPECT3=`getchat "What's the third thing we should look for?" $EXPECT3`
	if [ $? -ne 0 ] ; then
	    EXPECT4="";	    SEND3="";	    SEND4=""
	    return;
	fi
	SEND3=`getchat "What should we reply with?" $SEND3`
	if [ $? -ne 0 ] ; then
	    EXPECT4="";	    SEND4=""
	    return;
	fi


	EXPECT4=`getchat "What's the last thing we should look for?" $EXPECT4`
	if [ $? -ne 0 ] ; then
	    SEND4=""
	    return;
	fi
	SEND4=`getchat "What should we reply with? [$SEND4]"`
}

export EXPECT1 EXPECT2 EXPECT3 EXPECT4
export SEND1 SEND2 SEND3 SEND4

USEPAP=${USEPAP:-y}
USECHAP=${USECHAP:-n}
USECHAT=${USECHAT:-n}

echo "Do you want to use 'PAP' or 'CHAP' for login authentication?"
echo "(I think many ISPs support PAP these days, which makes things easy)"

ans=`getvalue "Use PAP [$USEPAP]?"`
if [ "$ans" = "y" -o "$ans" = "Y" -o "$ans" = "yes" ] ; then
	USEPAP="y"
else
	USEPAP=${ans:=$USEPAP}
fi

if [ $USEPAP != "y" ] ; then
	ans=`getvalue "Use CHAP[$USECHAP]?"`
	if [ "$ans" = "y" -o "$ans" = "Y" -o "$ans" = "yes" ] ; then
		USECHAP="y"
	else
		USECHAP=${ans:=$USECHAP}
	fi
fi

if [ $USECHAP = "y" -o $USEPAP = "y" ] ; then
	echo USECHAP=$USECHAP
	echo USEPAP=$USEPAP
	
	ans=`getvalue "What's your PPP login name? [$LOGIN]"`
	LOGIN=${ans:=$LOGIN}
	ans=`getvalue "And your PPP password? [$PASSWORD]"`
	PASSWORD=${ans:=$PASSWORD}

	EXPECT1=""
	EXPECT2=""
	EXPECT3=""
	EXPECT4=""
	SEND1=""
	SEND2=""
	SEND3=""
	SEND4=""
fi

echo "Do you want to make a chat script (y/n)?"
if [ $USECHAP = "y" -o $USEPAP = "y" ] ; then
	USECHAT=${USECHAT:-"n"}
	echo "(Since you are using PAP/CHAP, you probably dont need one)"
fi
ans=`getvalue "default is [$USECHAT]"`
USECHAT=${ans:=$USECHAT}

if [ $USECHAT = "y" -o $USECHAT = "Y" ] ; then
	chatdirections | more

	setchatscript
fi


echo "Happy with your answers? enter 'y' to continue"
read ans
if [ "$ans" != "y" -a "$ans" != "Y" ] ; then
	echo "Okay, quitting now."
	exit
fi



# Save config answers. Then do actual setup.
echo "Saving new defaults"

echo REMOTENAME=$REMOTENAME | tee $CONFIGFILE
#security, security...
chmod 0600 $CONFIGFILE

echo REMOTEIP=$REMOTEIP | tee $CONFIGFILE
echo DNS=$DNS | tee -a $CONFIGFILE
echo LOCALNAME=$LOCALNAME | tee -a $CONFIGFILE
echo LOCALIP=$LOCALIP | tee -a $CONFIGFILE
echo ISPNUMBER=$ISPNUMBER | tee -a $CONFIGFILE

echo LOGIN=$LOGIN  | tee -a $CONFIGFILE
echo PASSWORD=$PASSWORD  | tee -a $CONFIGFILE

echo USEPAP=$USEPAP  | tee -a $CONFIGFILE
echo USECHAP=$USECHAP  | tee -a $CONFIGFILE
echo USECHAT=$USECHAT  | tee -a $CONFIGFILE


echo EXPECT1=$EXPECT1 | tee -a $CONFIGFILE
echo EXPECT2=$EXPECT2 | tee -a $CONFIGFILE
echo EXPECT3=$EXPECT3 | tee -a $CONFIGFILE
echo EXPECT4=$EXPECT4 | tee -a $CONFIGFILE
echo SEND1=$SEND1 | tee -a $CONFIGFILE
echo SEND2=$SEND2 | tee -a $CONFIGFILE
echo SEND3=$SEND3 | tee -a $CONFIGFILE
echo SEND4=$SEND4 | tee -a $CONFIGFILE
echo MODEMDEV=$MODEMDEV | tee -a $CONFIGFILE
echo BAUDRATE=$BAUDRATE | tee -a $CONFIGFILE

if [ "$MODEMDEV" != "ACU" ] ; then
	DEVTYPE="CONFPPP"
else
	DEVTYPE="ACU"
fi

##################################################

if [ -f /etc/resolv.conf ] ; then
	if [ ! -f /etc/resolv.conf-pre_ppp ] ; then
		echo backing up /etc/resolv.conf
		cp /etc/resolv.conf /etc/resolv.conf-pre_ppp
	fi
fi
echo setting up /etc/resolv.conf
echo "nameserver $DNS" >/etc/resolv.conf

  ########
grep '^hosts:.*dns' /etc/nsswitch.conf
if [ $? -ne 0 ] ; then
	echo adding dns to /etc/nsswitch.conf
	sed 's/\(hosts:.*\)/\1 dns/' /etc/nsswitch.conf >/tmp/ns.$$
	mv /tmp/ns.$$ /etc/nsswitch.conf
fi


  #####

echo setting up /etc/hosts

TMPHOSTS=/tmp/hosts.$$
sed -e "s/^$REMOTEIP.*/$REMOTEIP $REMOTENAME/" -e "s/^$LOCALIP.*/$LOCALIP $LOCALNAME/" /etc/hosts >$TMPHOSTS
if [ $? -ne 0 ] ; then
  echo error copying /etc/hosts: quitting
  exit 1
fi

grep $REMOTENAME $TMPHOSTS >/dev/null
if [ $? -eq 0 ] ; then
   echo setting IP of $REMOTENAME to $REMOTEIP
   sed -e "s/.*${REMOTENAME}.*/$REMOTEIP $REMOTENAME/" $TMPHOSTS >/tmp/t.$$
   mv /tmp/t.$$ $TMPHOSTS
else
   echo adding remote IP to hosts file
   echo $REMOTEIP $REMOTENAME >>$TMPHOSTS
fi
grep $LOCALNAME $TMPHOSTS >/dev/null
if [ $? -ne 0 ] ; then
   echo adding local IP to hosts file
   echo $LOCALIP $LOCALNAME >>$TMPHOSTS
fi

cp $TMPHOSTS /etc/hosts

if [ $? -ne 0 ] ; then
   echo error copying. leaving tmp file $TMPHOSTS and quitting
   exit 1
fi

rm $TMPHOSTS
  #######

############################################################
#I think/hope this will work on both sparc and intel
# This is to get 8-n-1, instead of 7-e-1
NO_PARITY='"" P_ZERO'

echo setting up /etc/uucp config files
cd /etc/uucp
cp Systems-pre_ppp Systems

if [ "$MODEMDEV" = "ACU" ] ; then
	# only modify Systems file
	echo $REMOTENAME Any ACU $BAUDRATE $ISPNUMBER $NO_PARITY $EXPECT1 $SEND1 $EXPECT2 $SEND2 $EXPECT3 $SEND3 $EXPECT4 $SEND4 >>Systems
else
	#Fiddle with Systems, Devices, and Dialers
	MODEMDEV=`echo $MODEMDEV |sed 's:/dev/::'`
	echo $REMOTENAME Any CONFPPP $BAUDRATE $ISPNUMBER $NO_PARITY $EXPECT1 $SEND1 $EXPECT2 $SEND2 $EXPECT3 $SEND3 $EXPECT4 $SEND4 >>Systems
	egrep -v CONFPPP Devices >/tmp/dev.$$
	mv /tmp/dev.$$ Devices
	echo CONFPPP $MODEMDEV - Any hayeshf >>Devices
	egrep -v hayeshf Dialers >/tmp/dev.$$
	mv /tmp/dev.$$ Dialers
	HAYES=`grep '^hayes[^a-zA-Z]' Dialers-pre_ppp`
	print -r "$HAYES STTY=crtscts"|sed 's/^hayes/hayeshf/' >>Dialers
fi


echo setting up asppp demon

cd /etc
cp asppp.cf-pre_ppp asppp.cf

INTERFACE=ipdptp0

if [ $USEPAP = "y" ] ; then AUTH="pap" ; fi
if [ $USECHAP = "y" ] ; then AUTH="$AUTH chap" ; fi


echo "#The following is auto-entered by configppp.sh"	 >>asppp.cf
echo ifconfig  $INTERFACE plumb $LOCALNAME $REMOTENAME up >>asppp.cf
echo "" 				>>asppp.cf
echo "path" 				>>asppp.cf
echo "	interface $INTERFACE"		>>asppp.cf
echo "	peer_system_name $REMOTENAME"	>>asppp.cf
if [ "$AUTH" != "" ] ; then
echo "	will_do_authentication $AUTH"	>>asppp.cf
fi
if [ $USEPAP = "y" ] ; then
echo "	pap_id $LOGIN"	>>asppp.cf
echo "	pap_password $PASSWORD"	>>asppp.cf
fi
if [ $USECHAP = "y" ] ; then
echo "	chap_name $LOGIN"	>>asppp.cf
echo "	chap_secret $PASSWORD"	>>asppp.cf
fi
echo "	negotiate_address on"		>>asppp.cf
echo "	inactivity_timeout 300"		>>asppp.cf
echo "" 				>>asppp.cf

echo ""
echo ""
echo "############################################################"
echo "setting default route"
if [ -f /etc/defaultrouter ] ; then
	echo used to be
	cat /etc/defaultrouter
fi

echo ispside >/etc/defaultrouter

base=`basename $MODEMDEV`
if [ -c "/dev/term/$base" ] ; then
	echo Setting port for outbound only
	pmadm -r -p zsmon -s tty$base
fi

echo "Okay, we're done! Starting dialup PPP demon..."

/etc/init.d/asppp stop >/dev/null 2>&1
sleep 1
/etc/init.d/asppp start

#must be AFTER asppp restart
route add default ispside 1

echo "BTW, if you dont want your link to come up at boot-time, change the"
echo "      keep-hot-count          hosts"
echo " line in /etc/nscd from value 20, to 0"
echo ""
echo ""
echo "If something isn't working, run this script again and change your answers"
echo ""
echo ""
echo "PLEASE EMAIL phil@bolthole.com to say whether or not this script works"
echo " for you. I will try to iron out any problems in the script you may"
echo " encounter, and thus help even more people by your feedback."
echo ""
echo "For testing, do  '/usr/sbin/ping ispside'  to try to bring up the connection"
