#!/bin/bash
#
# description: This is the boot/shutdown script for the Conexant AccessRunner
#              ADSL modem.


CNXADSLCONFIG=/usr/bin/cnxadslconfig
CNXADSLLOAD=/etc/Conexant/cnxadslload
CONFIG_DIR=/etc/Conexant
LOCK_FILE=/var/lock/cnxadslctl
VPI=0
VCI=38
NAME=cnxadsl
DESC="Conexant AccessRunner ADSL Modem"

# See how we were called.
case "$1" in
  start)
	# Load pppoatm if needed
	haspppoatm=`lsmod | grep ^pppoatm`
	if [ -z "$haspppoatm" ]; then
	   echo "No PPPoATM driver loaded"
	   modprobe pppoatm
	else
	   echo "Has PPPoATM already loaded"
	fi

    # if the driver is not already loaded then load the module
    if [ ! -f $LOCK_FILE ]; then
      echo -n "Starting $DESC: $NAME"
      insmod -f CnxADSL \
             CnxtDslVendorId=0x14F1         \
             CnxtDslArmDeviceId=0x1610      \
             CnxtDslAdslDeviceId=0x1611     \
             CnxtDslPhysicalDriverType=1 &> /dev/null

      if [ "$?" != "0" ]; then
        echo " FAILED - Could not load module!"
	exit 1
      fi

      # Initialize the firmware and start training
      $CNXADSLLOAD $CONFIG_DIR
    
      # Set the lockfile
      touch  $LOCK_FILE
	
      # Configure the VPI / VCI
      $CNXADSLCONFIG --vpi=$VPI --vci=$VCI

      # 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
      gawk '/ATM_V[CP]I/ {print $1"="$2}' /etc/Conexant/cnxadsl.conf >tempvpivci.sh
      . tempvpivci.sh
      rm -f tempvpivci.sh

      # Start pppd
      echo "Starting pppd"
      pppd &
    else
      echo -n "$DESC already loaded"
    fi
    echo "."
    ;;
  stop)
      if [ -f $LOCK_FILE ]; then
         echo -n "Stopping $DESC: $NAME "

	 # Stop pppd
	 killall pppd
	 
         #unload the module
         rmmod CnxADSL
	 # Remove the lock file
	rm -f $LOCK_FILE
	 echo "."
      fi
      ;;
  restart)
     $0 stop
     sleep 2
     $0 start
     ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0
