#!/bin/sh
# @vasm : vinetstart
# @level: root
# @description: start a network
# 
# (c) Eko M. Budi, 2003
# (c) VLocity Linux, 2003
#
# Released under GNU GPL

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

check_root

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

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 file in $inet_files; do
     if [ -x $file ]; then
       get_inet $file
       inet=${file##${rc_prefix}}
       if [ $DHCP = "yes" ]; then
	  echo "$inet \"DEVICE=$DEVICE, DHCP\" \\"
       else
	  echo "$inet \"DEVICE=$DEVICE, IPADDR=$IPADDR\" \\"
       fi    	
       let count++
     fi
   done
   if [ $count == 0 ]; then
     errorbox "No network connection!"
     clean_exit 1 
   fi
}

##################################################
function menuA() {

  DIMENSION="14 60 4"
  TITLE="START NETWORK"
  TEXT="\n
This is the list of the active network connections.\n
Select the one you want to start 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

  reply=$(cat $freply)
  
  if [ "$reply" ]; then
    infobox "Starting $reply ..."
    ${rc_prefix}${reply} start
    if [ $? != 0 ]; then
	errorbox "Starting $reply FAILED" "ERROR"
	return 1
    fi
    sleep 2
  fi
}

###############################################################
# MAIN 
menuA
clean_exit $?

