#!/bin/sh
echo
set -a
dcon=`/bin/sh -c "type dcon"|cut -d" " -f3`
if [ ! -x $dcon ]; then
  echo "You must install dcon in a directoy pointed by PATH first."
  exit 1
fi
if [ ! -f ppp-ex.scr ]; then
  echo "This script must be used in the directory where ppp-ex.scr resides."
  exit 1
fi
for a in /usr/lib/ppp /usr/bin /usr/sbin /sbin /usr/lib /bin; do
  if [ -x $a/pppd -a -x $a/ppp-off ]; then
    p8=$a/pppd
    p9=$a/ppp-off
    break
  fi
done
if [ "$p8" = "" ]; then
  echo "Could not find the pppd daemon!  Arrgh!  Have you PPP installed?"
  exit 1
fi
if [ "$p9" = "" ]; then
  echo "Could not find the ppp-off command!  Arrgh!  Have you PPP installed?"
  exit 1
fi
echo "This utility creates a simple dcon script from ppp-ex.scr for"
echo "connecting to simple PPP Internet Service Providers (ISP).  It does not"
echo "provide for complicated cases."
echo
echo "Prior to using this script, you must try logging in to your ISP"
echo "manually, noting all prompts you get as you go along.  For example,"
echo "Your ISP may have a login that looks something like:"
echo
echo "-----------------------------------------------------------------------"
echo "   This access server is restricted. All calls are registered and"
echo "   the calling numbers are recorded. Unauthorized access is strictly"
echo "   forbidden. If you are not authorized, disconnect now !"
echo
echo "   User Access Verification"
echo
echo "   Username: "
echo
echo "   Password: "
echo "-----------------------------------------------------------------------"
echo
echo "The prompts you should note here are \"Username:\" and \"Password:\""
echo
/bin/echo "Type ENTER to continue: \c"
read b
echo
echo "In most cases, after having entered your username and password, you"
echo "should get some prompt from your ISP where you type \"ppp\" or something"
echo "like that to enter PPP mode.  You must know what the prompt is and what"
echo "you must type to start ppp."
echo
echo "Provided you have all that information..."
echo
echo
b="$IFS"
IFS=""
echo "You must enter a device name for your modem.  Ex.: for COM2 on a PC, that"
echo "would be \"/dev/cua1\".  If you have the /dev/modem link made, go ahead and"
echo "use that."
/bin/echo "Please enter the modem device :\c"
read device
echo
/bin/echo "Please enter the modem telphone number of your ISP   :\c"
read number
echo
/bin/echo "Please enter the prompt for entering your username   :\c"
read usernameprompt
echo
/bin/echo "Please enter you username :\c"
read username
echo
/bin/echo "Please enter the prompt for entering your password   :\c"
read passwordprompt
echo
echo
echo "Attention!  If you enter a password here, it'll be written in clear"
echo "in the script.  If you do not enter one here, you will be prompted"
echo "for your password every time you login to your ISP."
/bin/echo "Please enter your password   :\c"
read password
echo
/bin/echo "Please enter the prompt for entering the ppp command :\c"
read pppprompt
echo
/bin/echo "Please enter the string to send to start PPP :\c"
read ppp
echo
echo "You now have to select a name for your custom script."
echo "A good idea is to select something representative of your ISP name."
echo "For example, if your ISP is called \"San-Diego-Communications\", \"sdc\""
echo "would be a good name."
echo
/bin/echo "Please enter script name (no extension please) :\c"
read name
while [ -f $name ]; do
  echo "That already exists!"
  /bin/echo "Please enter script name :\c"
  read name
  if [ "$name" = "" ]; then
    echo "Oh heck!"
    exit 1
  fi
done
echo
echo "Making script..."
if [ ! -x /tmp/ssp.dcon ]; then
  echo "Making /tmp/ssp.dcon..."
  cat <<!EOF! >/tmp/ssp.dcon.c
#include <stdio.h>
main() {
  char line[2048];
  int len;
  short int le=0;
  while(gets(line)) {
    len=strlen(line);
    if(len!=0 || le==0) {
      if(len==0) le=1;
      else le=0;
      printf("%s\n",line);
    }
  }
}
!EOF!
  cc /tmp/ssp.dcon.c -o /tmp/ssp.dcon
fi
if [ -x /tmp/ssp.dcon ]; then
  ssp=/tmp/ssp.dcon
else
  ssp=cat
fi
(
  echo "#!$dcon"
  echo "#$name dcon script made on `date`."
  echo "let \$c=\"$device\""
  echo "let \$n=\"$number\""
  echo "let \$u1=\"$usernameprompt\""
  echo "let \$u=\"$username\""
  echo "let \$p1=\"$passwordprompt\""
  echo "let \$p=\"$password\""
  echo "let \$s1=\"$pppprompt\""
  echo "let \$c1=\"$ppp\""
  echo "let \$p8=\"$p8\""
  echo "let \$p9=\"$p9\""
  tail +12 ppp-ex.scr |sed 's/ *#.*//' | eval $ssp  #Cleanup
) >$name
chmod u=rwx,go= $name
echo
echo "$name created.  You should take a look at it."
echo "To try it out, execute \"$name\""
echo "If it doesn't work, try \"dcon -v $name 2>/tmp/debug\" and"
echo "read the /tmp/debug file for hints.  Good luck!"
