#!/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

## source the standard functions

. /etc/rc.d/functions-network "$@"

'
}

# 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 '^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

