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

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

dbug "Starting $0"

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
     infobox "All connections has been deleted"
     sleep 2
     clean_exit 1 
   fi
}

# Confirm deletion
function menuB() {
inet=$1
inet_file="${rc_prefix}$1"
dbug $inet_file
if [ -f "$inet_file" ]; then
  get_inet $inet_file
  TITLE="DELETE NETWORK"
  if [ "$DHCP" = "yes" ]; then
  DIMENSION="12 50"
  TEXT="\n
We are about to delete network $inet:\n
  DEVICE  = $DEVICE\n
  DHCP    = yes\n\n
Press YES to delete this connection."
       
else
  DIMENSION="15 50"
  TEXT="\n
We are about to delete network $inet:\n
  DEVICE  = $DEVICE\n
  IPADDR  = $IPADDR\n
  NETMASK = $NETMASK\n
  GATEWAY = $GATEWAY\n
  PROBE   = $PROBE\n\n
Press YES to delete this connection."
fi

  $DCMD --backtitle "$BACKTITLE" --title "$TITLE" --yesno "$TEXT" $DIMENSION 2> $freply
  status=$?
  [ $status != 0 ] && return status
  
  infobox "Deleting network $inet"
  # rm -rf $inet_file
  # I guess make it unexecutable is enough
  chmod -x $inet_file
  # Delete is overkill
  # rm -rf $inet_file
  sleep 2
fi

}

##################################################
# Need to ask username
function menuA() {
dbug MenuA

while [ 0 ]; do
  DIMENSION="14 60 4"
  TITLE="DELETE NETWORK"
  TEXT="\n
This is the list of the active network connections.\n
Select the one you want to delete 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
    menuB $reply  
  fi
done
}

###############################################################
# MAIN 
dbug "Main $0"
 
if [ "$1" ]; then
   if [ -x ${rc_prefix}$1 ] ; then
     menuA
   else
     errorbox "Cannot find $1 connection."
   fi
else
   if ls $inet_files &> /dev/null ; then
     menuA
   else
     errorbox "No network connection. Cannot delete"
   fi
fi

clean_exit 0

