#!/bin/sh
# 
# /etc/ppp/ppp-up
# 
# Script to establish a PPP link via modem
# 
# ToDo :
# - check if the modem is ready


# Configuration has moved in the file /etc/ppp/ppp-config, so import it
. /etc/ppp/ppp-config


# Seems funny, that the stop part comes before the start part ;), but 
# otherwise the lockfilecheckings are in the way. BTW : calling
# /etc/ppp/ppp stop
# Will remove any lockfile regarding the modem, whetver the file belongs
# to this script or not.

if [ "$1" = "stop" ] ; then
  /usr/bin/killall pppd
    /bin/rm -f /var/lock/LCK..$DEVICE
fi



# First let's see, if the modem is already in use
if test -e /var/lock/LCK..$DEVICE ; then
  /bin/echo $LOCKED
  exit 1
fi

# In case, your COM-Port isn't initialised
#/sbin/setserial /dev/$DEVICE spd_vhi

# Just a small check to see, if we have to deal with a internet junkie, who
# changed the contents of his/her $LOGNAME, to gain internetaccess, even
# though a /var/lock/internet/block-ppp.this_user exists. Might look a bit
# like a dirty hack, but since sudo's the only program, a non root user
# may start this script with (or at least it's the only one, I recomend it
# with) ...

if test -z $SUDO_USER ; then 
# This is a helper function, in case root wants to start this script,
# it'll get confused because of a missing $SUDO_USER. I know there must be a 
# better way, than running a bogus  command, but test -n somehow doesn't
# work (maybe someoe can fix this and send me a patch)
CALLED_BY_ROOT=yes;
else
  if test $LOGNAME != $SUDO_USER ; then
    /bin/echo $JUNKI
    /bin/echo $JUNKI_LOG >> /var/log/internet-log
    /bin/date >> /var/log/internet-log
    exit 1
  fi 
fi



# Maybe we aren't allowed to connect right now, let's check, if there 
# is a lock file and if so, then print it's contents

if test -e /var/lock/internet/block-ppp ; then
  /bin/echo
  /bin/cat /var/lock/internet/block-ppp 
  /bin/echo
  exit 1
fi

# Maybe just a single user is not allowed to connect to the internet
# right now, let's check, if someone's trying, even if s/he is not allowed
# too
 
if test -e /var/lock/internet/block-ppp.$LOGNAME ; then
  /bin/cat /var/lock/internet/block-ppp.$LOGNAME
  exit 1
fi

if [ "$1" = "start" ] ; then
  /usr/sbin/pppd lock connect \
    '/usr/sbin/chat -f /etc/ppp/dialer' /dev/$DEVICE $PPPFLAGS $STATIC
  /sbin/pidof /etc/ppp/ppp > /var/lock/LCK..$DEVICE  
fi


