#!/bin/sh
#
# $Id: synce-serial-config.in,v 1.9 2005/07/08 19:04:42 twogood Exp $
#
# vim: expandtab
#
# Script for configuring a PPP device for SynCE
#

THIS=`basename $0`

# include common stuff
. /usr/pkg/share/synce/synce-serial-common

exit_if_not_root

SYSTEM=`uname -s`

help()
{
  echo "
Syntax for ${THIS}:

  ${THIS} serial-device [local-ip-address:remote-ip_address [DNS-server-name]]

  The serial-device specifies the name of your serial port device."

case ${SYSTEM} in

  Linux)
    echo "
  These are the port names on Linux:

    ttyS0  The first serial port
    ttyS1  The second serial port
    ..."
    ;;

  FreeBSD)
    echo "
  These are the port names on FreeBSD:

    ttyd0  The first serial port
    ttyd1  The second serial port
    ..."
    ;;

  *)
    echo "
${THIS} does not know the name of serial ports on ${SYSTEM} systems."
    ;;

esac

  echo "
  You can also connect via an infrared connection (IrDA). This requires
  that you already have irattach running.

    ircomm0  The first IrDA communication port

  Please send corrections and updates to the above information to the
  SynCE development mailing list: synce-devel@lists.sourceforge.net

  If you do not provide the local_ip_address:remote_ip_address parameter,
  ${THIS} will use ${DEFAULT_IPS}.

  If you want to specify these addresses yourself, you may want to read
  about the same option on the man page for pppd.
"

}

if [ -z "$1" ]; then
  help
  exit 1
fi

case $1 in
  -h|--help)
    help
    exit 0
    ;;
esac

if [ ! -d "${PPP_PEERS}" ]; then
  echo "
ERROR:

${THIS} did not find the directory ${PPP_PEERS}.
Have you really installed pppd?
" >&2
  exit 1
fi

if [ -c $1 ]; then
  DEVICE=$1;
else 
  if [ -c /dev/$1 ]; then
    DEVICE=/dev/$1
  else
    echo "
ERROR:

${THIS} was unable to find a character device named \"$1\"

Run \"${THIS} --help\" to get help.
" >&2
    exit 1
  fi
fi

if [ "$2" ]; then
  if ! (echo $2 | grep -b '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*:[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$'); then
    echo "
ERROR:

The format of \"$2\" looks incorrect.

Run \"${THIS} --help\" to get help.
" >&2
    exit 1
  fi

  IP_ADDRESSES=$2
else
  IP_ADDRESSES=${DEFAULT_IPS}
fi

DNS_SERVER="$3"
if [ -z "$DNS_SERVER" ]; then
  DNS_SERVER=`echo $IP_ADDRESSES | sed 's/:.*$//'`
fi

echo "# Modifications to this file will be lost next time you run ${THIS}
${DEVICE} 115200
connect '/usr/pkg/bin/synce-serial-chat'
nodefaultroute
noauth
local
${IP_ADDRESSES}
ms-dns ${DNS_SERVER}
crtscts
linkname ${PEER}" >${PEER_FILE}
#lcp-echo-failure 2
#lcp-echo-interval 2

echo "
You can now run synce-serial-start to start a serial connection.
"

exit 0
