#!/bin/sh -e

THIS=`basename $0`

# include common stuff
. /usr/share/synce/synce-serial-common
if [ -n "$1" ]
then
  SYNCE_DEVNAME="$1"
elif [ -z "$SYNCE_DEVNAME" ]
then
  SYNCE_DEVNAME="synce-device"
fi

exit_if_not_root

if [ -z "$INFO" ]; then INFO=echo; fi

PIDFILE="$VAR_PPP/ppp-$SYNCE_DEVNAME.pid"

if ! [ -f "$PIDFILE" ]; then
  echo "
${THIS} was unable find a running SynCE connection. Will do nothing.
" >&2
  exit 0
fi

while [ -f "$PIDFILE" ]; do
  PID="`head -1 $VAR_PPP/ppp-$SYNCE_DEVNAME.pid`"

  if [ -z "${PID}" ]; then
  	echo "
${THIS} found invalid PID in ppp-$SYNCE_DEVNAME; giving up.
" >&2
	exit 1
  fi

  ${INFO} "Killing process with PID ${PID}"

  if kill ${PID}; then
  	  echo "
${THIS} has requested shutdown of $SYNCE_DEVNAME.
"
  else
	  echo "
${THIS} was unable stop the SynCE connection with PID ${PID}.
" >&2
	  exit 1
  fi
  sleep 3
done

