#!/bin/sh
# @vasm : vnetsimple
# @level: root
# @description: Set up a simple network
# 
# (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

# Properties
rc_prefix="/etc/rc.d/rc"
host_file="/etc/HOSTNAME"
dns_file="/etc/resolv.conf"
inet_prefix="${rc_prefix}.inet"
inet_file="${inet_prefix}1"
inet_files="${inet_prefix}*"

HOST_NAME="vector.linux.vnet"
DNS_SERVER="127.0.0.1"

get_hostname()
{
   HOST_NAME=""
   if [ -r $host_file ]; then
      HOST_NAME=$(cat $host_file)
   fi
   HOST_NAME=${HOST_NAME:-"vector.linux.vnet"}   
}

set_hostname()
{
   hostname $HOST_NAME && echo $HOST_NAME > $host_file
}

get_dns()
{
   DNS_SERVER=""
   if [ -r $dns_file ]; then
     line=$(grep -e '^nameserver .*' $dns_file)
     if [ "$line" ]; then
        value=$(echo $line | cut -d ' ' -f 2)
        ipmask 0.0.0.0 $value &> /dev/null
        if [ $? = 0 ]; then
           DNS_SERVER=$value
        fi
     fi
   fi
   # DNS_SERVER=${DNS_SERVER:-127.0.0.1}
}

set_dns()
{
   echo "search ${HOST_NAME##.*}" > $dns_file
   if [ "$DNS_SERVER" ]; then
     echo "nameserver $DNS_SERVER" >> $dns_file
   fi
}

# Get the inet values from file
function get_inet()
{
  DEVICE=""
  IPADDR=""
  NETMASK=""
  GATEWAY=""
  DHCP="yes"
  PROBE="no"
  dbug inet_file=$inet_file
  if [ -r $inet_file ]; then
     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`
  fi
  DEVICE=${DEVICE:-"eth0"}
  IPADDR=${IPADDR:-"10.0.0.1"}
  NETMASK=${NETMASK:-"255.255.255.0"}
  if [ -z "$GATEWAY" ]; then
    GATEWAY="${IPADDR%.*}.254"
  fi
}

# Set inet values 
function set_inet()
{
  if [ ! -r $inet_file ]; then
    new_inet > $inet_file 
  fi
  cat $inet_file | sed "
  s#^DEVICE=.*#DEVICE=$DEVICE# 
  s#^IPADDR=.*#IPADDR=$IPADDR# 
  s#^NETMASK=.*#NETMASK=$NETMASK# 
  s#^GATEWAY=.*#GATEWAY=$GATEWAY# 
  s#^DHCP=.*#DHCP=$DHCP# 
  s#^PROBE=.*#PROBE=$PROBE#" > $inet_file

  chmod a+x $inet_file
}

# Disable inet file but not delete it
function disable_inet()
{
  if [ -f $inet_file ]; then
     chmod -x $inet_file  
  fi
}


## Create a new inet script
function new_inet()
{
echo '
#!/bin/sh
# This file is supposed to be created by vnetadd
# and modified by vnetset.
# You can modify it by hand, 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"
NETNAME="noname.nodomain.nonet"

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

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
   
   # Set the gateway if defined
   if [ "$GATEWAY" ]; then
     # if PROBE is defined, try the gateway 3 times
     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

' 

}

##################################################
# List active network connection, ask one to set
function menuA() {
while [ 1 ]; do

DIMENSION="17 70"
TITLE="SET NETWORK HOSTNAME"
TEXT="\n
This configurator quickly sets a simple networking for\n
a computer with a single network card. On an official\n
network like the Internet or an office, you need to ask\n
the TCP/IP settings from your ISP or your network\n
administrator. Otherwise, the default value will be fine.\n\n
First, please enter the hostname for this computer.\n
It should be something like: name.domain.net."

  $DCMD --backtitle "$BACKTITLE" --title "$TITLE" --inputbox "$TEXT" $DIMENSION $HOST_NAME 2> $freply
  status=$?
  [ $status != 0 ] && return $status;
  
  reply=$(cat $freply)

  # should check it here
  HOST_NAME=$reply
  set_hostname
  return 0
done
}


########################################################
# Ask Mode DHCP, STATIC, PROBE
function menuB() {
DIMENSION="20 70 4"
TITLE="SET NETWORK METHOD"
TEXT="\n
Now we need to know how this network can get its IP address.\n
IP address is a unique network identification for your computer.\n
It can be obtained automatically via DHCP if your computer is\n
connected to a dynamic network like cable modem or DSL.\n
If not, you should prepare an IP address right now, then\n
select the STATIC method. PROBE is also a STATIC method but\n
with network detection, usefull for a portable computer that\n
may connect to different networks."

$WCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
  "DHCP"   "automatic setup via Dynamic Host Control Protocol" \
  "STATIC" "set manual TCP/IP configuration" \
  "PROBE"  "static method with network detection" \
  "NONE"   "no network card, skip this configuration" \
  2> $freply

status=$?
[ $status != 0 ] && return $status

reply=$(cat $freply)
dbug $reply
case $reply in
    DHCP)
      PROBE="no"
      DHCP="yes"
      infobox "Configuring network $inet with DHCP ..."
      set_inet 
      sleep 2
      clean_exit 0
      ;;
    STATIC)
      PROBE="no"
      DHCP="no"
      ;;
    PROBE)
      PROBE="yes"
      DHCP="no"
      ;;
    NONE)
      infobox "No network ..."
      disable_inet 
      sleep 2
      clean_exit 0
      ;;
esac
return 0
}

########################################################
# Ask an IP Adress
function menuC() {
while [ 0 ]; do
DIMENSION="20 74"
TITLE="SET NETWORK IP ADDRESS"
TEXT="\n
So you want a static IP for $DEVICE. If you are connected to an\n
official network, you must get the IP adress from the administrator.\n
However, if you are on your own network, you may assign an arbitrary\n 
IP address that conforms the standard internal IP adresss, e.g:\n
  10.0.0.1 - 10.0.0.254        (easier to remember)\n
  172.16.0.1  - 172.16.0.254\n
  192.168.0.1 - 192.168.0.254  (recomended for small network)\n
Note that each computer must have a unique IP address.\n\n
Enter the IP address, or use 10.0.0.[1-254] for your own network:"
## HEH, I know that this IP information is not so right.
## But I think it is easier for newbies :)
## as long as it works

  $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
  --inputbox "$TEXT" $DIMENSION $IPADDR 2> $freply

  status=$?
  [ $status != 0 ] && return $status

  reply=$(cat $freply)
  
  # check the IP
  ipmask 0.0.0.0 $reply &> /dev/null
  if [ $? = 0 ]; then
     IPADDR=$reply
     return 0  
  fi
  retrybox "Invalid IP adrress"
done

}

########################################################
# Ask a Netmask
function menuD() {

while [ 0 ]; do

TITLE="SET NETWORK NETMASK"
TEXT="\n
Netmask is the pair of IP Address that looks kinda like these:\n
  255.255.255.0    (medium network, up to 250 computers)\n
  255.255.255.224  (small network, around 30 computers)\n\n
Enter it here or use 255.255.255.0 for your own network:"
DIMENSION="15 68"
  $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
  --inputbox "$TEXT" $DIMENSION $NETMASK 2> $freply

  status=$?
  [ $status != 0 ] && return $status

  reply=$(cat $freply)
  
  # check the IP
  ipmask $reply $IPADDR &> /dev/null
  if [ $? = 0 ]; then
     NETMASK=$reply
     return 0 
  fi
  retrybox "Invalid Netmask"
done
}

########################################################
# Ask a Gateway 
function menuE() {

while [ 0 ]; do

TITLE="SET NETWORK GATEWAY"
if [ "$PROBE" = "yes" ]; then
TEXT="\n
GATEWAY is the computer that forwards the network traffic to the 
bigger network (most likely the internet). You should get this 
information from your administrator. Since you have selected PROBE 
mode, this network will be up only if the gateway is reachable.\n\n
Please enter the IP address of the gateway or leave it empty\n
if you are on an isolated network."
DIMENSION="18 70" 
else
TEXT="\n
GATEWAY is the computer that forwards your network traffic to
the bigger network (most likely the internet). You should get
this information from your ISP or administrator.\n\n
Please enter the IP address of the gateway or leave it empty\n
if you are on an isolated network."
DIMENSION="16 70"
fi

  $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
  --inputbox "$TEXT" $DIMENSION $GATEWAY 2> $freply

  status=$?
  [ $status != 0 ] && return $status

  GATEWAY=$(cat $freply)

  # skip if empty
  [ -z "$GATEWAY" ] && return 0;
    
  # check the IP
  ipmask $NETMASK $GATEWAY &> /dev/null
  [ $? = 0 ] && return 0  

  retrybox "Invalid IP address for gateway"
done
}


############################
# Set DNS SERVER
menuF() {

while [ 1 ]; do
DIMENSION="14 60"
TEXT="\n
DNS (Domain Name Server) is the computer that helps
to find another hosts by their internet name.\n\n
Enter the IP address of the DNS server or\n
leave it empty if there is no DNS server."
TITLE="SET DNS SERVER"

  $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
  --inputbox "$TEXT" $DIMENSION $DNS_SERVER 2> $freply

  status=$?
  [ $status != 0 ] && return $status;
  
  DNS_SERVER=$(cat $freply)
  [ -z "$DNS_SERVER" ] && return 0

  ipmask 0.0.0.0 $DNS_SERVER &> /dev/null
  [ $? = 0 ] && return 0  

  retrybox "Bad IP Address for DNS"
done
}

menuG() {
  infobox "Setting up networking $inet_file ...  "
  set_hostname
  set_dns
  set_inet
  sleep 2
  clean_exit 0
}

###############################################################
# MAIN 

inet=${1:-inet1}
inet_file="${rc_prefix}.$inet"

#set_inet
#cat $inet_file
#exit

get_hostname
dbug hostname=$HOST_NAME
get_inet 
dbug ip=$IPADDR/$NETMASK
get_dns
dbug dns_server=$DNS_SERVER

wizard menuA menuB menuC menuD menuE menuF menuG

clean_exit $?


