#!/bin/sh
# $Id: dppp,v 1.9 1998/03/01 00:49:02 john Exp john $
#
# set -x

[ $# -eq 0 ] && {
exec /usr/bin/pon
exit
	}

umask 022
BACKTITLE="              Starting PPP Connection                  "

readonly TempFile="/tmp/`echo $0|sed -e 's/^.*\///'`.$$"

readonly BASEDIR=${DUNC_DIR:-$HOME/.dunc}
readonly RCFILE=${DUNC_RC:-$HOME/.dunc2rc}
# Define DUNC_UIFILE as Bruce Peren's GPL's shell interface to dialog
readonly DUNC_UIFILE=${DUNC_UIFILE:-/usr/lib/dunc/shint2dialog.sh}

Exit () {
# clean up and exit script
local return=$1
rm -rf $TempFile* $BASEDIR/tmp.$$
clear
exit ${return:-0}
}

NOGUI=0
NOPWD=0

# include Bruce Perens' GPL'd shell interface to dialog
. ${DUNC_UIFILE}

selectList () {
# This looks in $BASEDIR for .ctn files and builds a selection
# menu based on its findings.  It also builds a corresponding 
# custom case block in ${TempFile}1.
# global variable here is $selection

cat >$TempFile <<EOF
menu \\
"Please select from the list below or select \\"Exit\\"
to exit." "Select A Connection" \\
EOF

cat >${TempFile}1 <<EOF
case \$value in
EOF

j=1

# The list returned may not be read correctly if there are funky characters
# in the filename (i.e. spaces), so this bit of code should work around
# that.
# the global variable $selection is set here
local list="`ls $BASEDIR/*.ctn 2>/dev/null`"
[ -n "$list" ] || {
msgBox "No Connections Found!" "Error"
return 99
        }
for i in $list
do
  if [ `echo $i|grep \.ctn` ]; then
  name="$name $i"
  connection=`basename "$name" .ctn`
  echo -n "\"$j\" \"$connection\"" >>$TempFile
  echo ' \'>>$TempFile

  cat >>${TempFile}1 <<EOF
  $j) selection="$connection" ;;
EOF

  j=`expr $j + 1`
  name=""
  else
  name="$name $i"
  fi
done

cat >>$TempFile <<EOF
" " " " \\
"Exit" "Quit without starting a connection"
EOF

cat >>${TempFile}1 <<EOF
Exit) Exit;;
esac
EOF

local value=`. $TempFile`
# sets the global $selection variable
. ${TempFile}1 

return
}

successMsg () {
# report success via a message box
msgBox "Connection \"$selection\" started!" "Status"
return
	}

errorMsg () {
# report an error starting pppd
msgBox "There may have been a problem starting pppd.  \
Please check with the system administrator" "Error"
return
	}

case "$1" in
'-s')	shift
	selectList
	[ $? -eq 99 ] && {
	errorMsg
	Exit 1
		}
	;;
'-c')	shift
	selection="$1"
	shift
	;;
'-x')	shift
	selection="$1"
	shift
	NOGUI=1
	;;
'-h'|'--help'|*) cat <<EOF
Usage:	`basename $0` [-c connection] [options]
	`basename $0` [-s] [options]
	`basename $0` [-x connection] [[password] options]
	`basename $0`

Synopsis: 
	This is intended to be a drop in replacement for the "pon"
	command.  With no options, it is just that.  But with
	either the -c or -s options it works with the dunc script
	to start a dunc managed connection using dialog.  If the
	-x option is used, dialog is not called at all and the named
	dunc managed connection is executed immediately.  If the 
	password is not stored for the connection, it must be
	supplied on the command line as the second argument when -x
	is used.  It will be prompted for otherwise (if no -x).

	In all dunc managed cases, the trailing options are appended
	to the pppd command line.

EOF
	rm -rf $TempFile*
	exit
	;;
esac

method=`grep METHOD "$BASEDIR/$selection.ctn" 2>/dev/null |awk '{print $3}'`
case $method in
pap|chap|none)
PPPCMD="/usr/sbin/pppd file $BASEDIR/$selection.ctn"
  ;;
chat)
grep QUERY_PWD "$BASEDIR/$selection.cht">/dev/null 2>&1
if [ $? -eq 0 ]; then
    NOPWD=1
fi
if [ $NOPWD -eq 1 ];then
  if [ $NOGUI -eq 0 ];then
    pwd="`inputBox "Please enter your password for the connection" \\
\\"$selection\\". "Enter Password"`"
  else 	# $NOGUI -eq 1
    pwd="$1"
    shift
  fi
  cat "$BASEDIR/$selection.cht"|grep -v "^#"|sed -e "s/QUERY_PWD/$pwd/" > $BASEDIR/tmp.$$
  PPPCMD="/usr/sbin/pppd  connect '/usr/sbin/chat -v -f $BASEDIR/tmp.$$; rm -f $BASEDIR/tmp.$$' `cat "$BASEDIR/$selection.ctn"|grep -v ^# | xargs` $@"
else 	# $NOPWD -eq 0
  PPPCMD="/usr/sbin/pppd file $BASEDIR/$selection.ctn"
fi
  ;;
esac
echo $PPPCMD
eval "$PPPCMD"
return=$?

[ $NOGUI -eq 0 ] && {
if [ $return -eq 0 ]; then
  successMsg
else
  errorMsg
fi
	}
Exit
