#!/bin/sh
#
#  This is the boot/shutdown script for the driver for the
#  Conexant AccessRunner ADSL modem.
#

# load the standard bootscript functions
. /etc/rc.d/init.d/functions

# for debugging
# set -o xtrace

# initialize the return value
RETVAL=0

# determine if we are loading or unloading
case "$1" in
   start)
      # if the driver is not already loaded then
      # Load the module
      if [ ! -f /var/lock/subsys/cnxadsllock ]; then
         echo -n "Starting Conexant AccessRunner"
         insmod CnxADSL                     \
             CnxtDslVendorId=0x14F1         \
             CnxtDslArmDeviceId=0x1610      \
             CnxtDslAdslDeviceId=0x1611     \
             CnxtDslPhysicalDriverType=1

         # Initialize the firmware and start training
         /etc/Conexant/cnxadslload /etc/Conexant &
         if ! grep -q br2684_init /proc/ksyms; then
            echo -n "Loading br2684 module: "
            insmod br2684
         fi
         RETVAL=$?
         if [ $RETVAL -eq 0 ] ; then
            echo

            # Create the tty device
            number=`awk '/ttyCX/ { print $1 }' /proc/devices`
            echo $number
            number=${number:?"Could not find device"}

            rm -f /dev/ttyCX
            mknod --mode=660 /dev/ttyCX c $number 0

            sleep 30;

			gawk '/ATM_V[CP]I/ {print $1"="$2}' /etc/Conexant/cnxadsl.conf >tempvpivci.sh
			. tempvpivci.sh
			rm -f tempvpivci.sh

            br2684ctl -c1 -a ${ATM_VPI}.${ATM_VCI} -b
            # create a file for the init scripts to know
            # to shut the driver down
            touch /var/lock/subsys/cnxadsllock
         fi
      else
         echo -n "AccessRunner already loaded"
      fi
      ;;
   stop)
      killproc br2684ctl
      # killall  cnxadslload
      # if the driver is started then stop it
      if [ -f /var/lock/subsys/cnxadsllock ]; then
         echo -n "Stopping Conexant AccessRunner:"
         #unload the module
         rmmod CnxADSL
         rmmod br2684
         # get rid of the tty device
         rm -f /dev/ttyCX
         rm -f /var/lock/subsys/cnxadsllock
      fi
      ;;
   status)
      status cnxadslload
      RETVAL=$?
      ;;
   stat)
      cat /proc/net/atm/CnxAdsl:0 |more
      ;;
esac

# return the results
exit $RETVAL
