#!/bin/sh
# @vasm : vnetadd
# @level: root
# @description: Add a new network connection
# 
# (c) Eko M. Budi, 2003
# (c) Vector Linux, 2003
#
# Released under GNU GPL

vdir=$(dirname $0)
source $vdir/vasm-functions

dbug "Starting $0"

check_root


###############################################################
### Create a new configuration file
function new_inet()
{

echo '#!/bin/sh
# This file is supposed to be created by vinetadd
# and modified by vinetset.
# You can modify it but be cerefull
#
# GNU GPL  (c) Eko M. Budi, 2004
#          (c) Vector Linux, 2004
#

###########################################################
## The settings
DEVICE=eth0
DHCP="yes"
IPADDR=""
NETMASK=""
GATEWAY=""
PROBE="no"

###########################################################
## The script

NETNAME=$(hostname)
PATH="/sbin:/usr/sbin:/bin:/usr/bin"

start()
{
if [ "$DHCP" = "yes" ]; then
   ## DYNAMIC IP
   echo "Starting network ${DEVICE} by contacting a DHCP server..."
   if [ -z "$NETNAME" ]; then
     dhcpcd -t 10 -d $DEVICE
   else
     dhcpcd -t 10 -h ${NETNAME} -d $DEVICE
   fi
   if [ $? != 0 ]; then
      echo "FAILED"
      return 1
   else
      ifconfig $DEVICE
      return 1
   fi
else
   ## STATIC
   # Test IPADDR, if invalid, skip
   IPMASK=`ipmask $NETMASK $IPADDR`
   if [ ! $? = 0 ]; then
     echo "Invalid IP/NETMASK = $IPADDR/$NETMASK"
     return 1
   fi     
   BROADCAST=`echo $IPMASK | cut -d " " -f 1`
   NETWORK=`echo $IPMASK | cut -d " " -f 2`
 
   # Going up ..
   echo "Starting network ${DEVICE} as ${IPADDR}/${NETMASK}..."
   ifconfig ${DEVICE} ${IPADDR} broadcast ${BROADCAST} netmask ${NETMASK}
   if [ $? != 0 ]; then
      return 1
   fi
   
   # if PROBE is defined, try the gateway 3 times
   if [ "$GATEWAY" ]; then
     if [ "$PROBE" = "yes" ]; then
        echo "Probing the gateway $GATEWAY "
        for ii in 1 2 3; do
          sleep 1
          ping -c 1 -w $ii $GATEWAY > /dev/null
    	  if [ $? = 0 ]; then
            break;
          fi
          if [ $ii = 3 ]; then
            echo " FAILED !!!"
	    echo Canceling network $DEVICE ...
	    ifconfig $DEVICE down
	    return 1	
	  fi
        done
     fi

     # set the default gateway if it is not defined yet
     route | grep -e "^default" &> /dev/null
     if [ $? != 0 ]; then
       echo "Setting up gateway $GATEWAY ..."
       route add default gw ${GATEWAY} netmask 0.0.0.0 $DEVICE
     fi
  fi
  return 0
fi

}

stop() {
   # stop the default gateway if it is ours
   current_gw=$(route -n | grep -e "^0.0.0.0" | cut -f 2 -d " ")
   if [ "$current_gw" = "$GATEWAY" ]; then
      echo "Deleting gateway $GATEWAY..."
      route del default
   fi 

   # stop the interface
   echo "Stopping network $DEVICE ..."
   ifconfig $DEVICE down 
}

status() {
   ifconfig $DEVICE
}

case $1 in
start)
   start
   ;;
stop)
   stop
   ;;
restart)
   $0 stop
   sleep 3
   $0 start
   ;;
reload)
   ;;
status)
   status 
   ;;
*)
   echo "Usage: $0 {start|stop|restart|reload|status}"
esac

'
}



# Properties
rc_prefix="/etc/rc.d/rc."
inet_prefix="${rc_prefix}inet"
inet_files="${inet_prefix}*"

# Get the inet values from file
function get_inet()
{
  DEVICE=""
  NETNAME=""
  IPADDR=""
  NETMASK=""
  GATEWAY=""
  PROBE="no"
  inet_file=$1
  eval `grep -e '^DEVICE=' $inet_file`
  eval `grep -e '^IPADDR=' $inet_file`
  eval `grep -e '^NETMASK=' $inet_file`
  eval `grep -e '^GATEWAY=' $inet_file`
  eval `grep -e '^NETNAME=' $inet_file`
  eval `grep -e '^DHCP=' $inet_file`
  eval `grep -e '^PROBE=' $inet_file`
}

###########################################################################################
#
function build_inet_menu()
{
   count=0
   for num in 1 2 3 4 5 6 7 8 9; do
      inet_file="${inet_prefix}$num"
      if [ -x $inet_file ]; then
         continue
      elif [ -r $inet_file ]; then
          get_inet $inet_file
	  echo "inet$num \"RECYCLE DEVICE=$DEVICE\" \\"
      else
	  echo "inet$num \"NEW inet\" \\"
      fi    	
      let count++
   done
   if [ $count == 0 ]; then
     errorbox "Wow, all 9 connections have been used. I don't want to help more ;-)"
     clean_exit 1 
   fi
}

##################################################
# List active network connection, ask one to set
function menuA() {

  DIMENSION="20 56 8"
  TITLE="ADD NETWORK"
  TEXT="\n
Vector Linux uses a flexible network connections.\n
This configurator can create a new connection\n
then let you change the settings.\n\n
Just select a connection, then press OK."

  echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
  build_inet_menu >> $fmenu
  echo '2> $freply' >> $fmenu

  cat $fmenu
  source $fmenu
  
  status=$?
  [ $status != 0 ] && return $status

  inet=$(cat $freply)
  inet_file=${rc_prefix}$inet
  new_inet > $inet_file
  chmod +x $inet_file
  exec $vdir/vinetset $inet
}

###############################################################
# MAIN 
wizard menuA 
clean_exit

