#!/bin/sh
# @vasm : vnetsimple
# @level: root
# @description: Set up a simple 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"
host_file="/etc/HOSTNAME"
dns_file="/etc/resolv.conf"
dnsmasq_file="/etc/resolv.conf.dnsmasq"
inet_prefix="${rc_prefix}.inet"
inet_file="${inet_prefix}1"
inet_files="${inet_prefix}*"

HOST_NAME="vlocity.linux.vnet"

if [ "$1" == "--no-start" ]; then
    NO_START="1"
    shift
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 careful ;-)
#
# GNU GPL  (c) Eko M. Budi, 2004
#          (c) VLocity Linux, 2004
#

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

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

## You may make customized script here
## If not, source the standard network 
. /etc/rc.d/functions-network "$@"

'
}

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

set_hostname()
{
   if skill -n X &> /dev/null; then
      echo $HOST_NAME > $host_file
   else
      hostname $HOSTNAME && echo $HOST_NAME > $host_file
   fi
}

get_dns()
{
   DNS_SERVER=""
   if [ -r $dnsmasq_file ]; then
     line=$(grep -e '^nameserver .*' $dnsmasq_file) 
   elif [ -r $dns_file ]; then
     line=$(grep -e '^nameserver .*' $dns_file)
   fi
   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
   # 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
     service -s dnsmasq &> /dev/null
  fi
  # reload dnsmasq
  if [ -z "$NO_START" ] && [ "`skill -n dnsmasq 2> /dev/null`" ]; then
       service dnsmasq reload > /dev/null 2>&1
  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"}
}

# Set inet values 
function set_inet()
{
  # I don't want a troube here
  # So ... let's always make a new script
  # Old script will be overwritten !!!!
  new_inet | 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
  
  ## Restart inet if the interface is running
  if [ -z "$NO_START" ]; then
       $inet_file start > /dev/null 2>&1
  fi
}

# add_host 
# add host entry to etc/hosts
# IPADDR and HOST_NAME must be set
function set_hosts()
{
    if [ ! -f /etc/hosts ]; then
	dbug "Creating a new /etc/hosts"
cat << EOF > /etc/hosts
#
# hosts	This file describes a number of hostname-to-address
#	mappings for the TCP/IP subsystem.  It is used as:
#	1. A static mapping without a DNS server for localhost
#       2. A table for dnsmasq
#
# By the way, Arnt Gulbrandsen <agulbra@nvg.unit.no> says that 127.0.0.1
# should NEVER be named with the name of the machine.  It causes problems
# for some (stupid) programs, irc and reputedly talk. :^)

# localhost loopback, must present
127.0.0.1       localhost

# hostname loopback, only if absolutely no network
#127.0.0.1 $HOST_NAME ${HOST_NAME%%.*}

EOF

    fi

    dbug "Modifying /etc/hosts"
    mv /etc/hosts /etc/hosts.bak 
    IPADDR=${IPADDR:-127.0.0.1}
    cat /etc/hosts.bak | while read line; do
	if echo $line | grep -qe '^127\.0\.0\.1.*localhost'; then
    	    # the must presents loopback
	    echo $line >> /etc/hosts
	elif echo $line | grep -qE '^#*127\.0\.0\.1 '; then
    	    # presents for NO NETWORK only
	    if [ "$IPADDR" = "127.0.0.1" ]; then
		echo "127.0.0.1   $HOST_NAME ${HOST_NAME%%.*}" >> /etc/hosts
	    else
	        echo "#127.0.0.1   $HOST_NAME ${HOST_NAME%%.*}" >> /etc/hosts
	    fi
	elif echo $line | grep -qe "^$IPADDR "; then
	    # skip our IPADDR, since we are going to add a new one
	    continue
	else
	    # write the rest
	    echo "$line" >> /etc/hosts
	fi
    done
    # add the new one if not NO NETWORK
    if [ "$IPADDR" != "127.0.0.1" ]; then
	echo "$IPADDR   $HOST_NAME ${HOST_NAME%%.*}" >> /etc/hosts
    fi
}

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

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

DIMENSION="17 66"
TITLE="SET NETWORK HOSTNAME"
TEXT="\n
This configurator quickly sets a simple networking system.
On an official network like the Internet or an office, 
you need to ask the TCP/IP settings from your ISP or your
network 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
  
  dbug "hostname=$HOST_NAME"

  return 0
done
}


########################################################
# Ask Mode DHCP, STATIC, PROBE
function menuB() {

# Check the device
modprobe $DEVICE &> /dev/null
 
if ifconfig -a | grep -qe "^$DEVICE"; then
DIMENSION="20 70 4"
TEXT="\n
Now we need to know how this network can get its TCP/IP settings.\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 TCP/IP settings right now, then\n
select the STATIC method. PROBE is also a STATIC method but\n
with network detection, useful for a portable computer that\n
may connect to different networks."
else
TEXT="\n
Network device $DEVICE is currently not present. If you are going
to add it later using PCMCIA, hotplug or wireless devices, then 
you may continue to choose DHCP, STATIC or PROBE connection method.
Otherwise, you should choose NONE.\n.
Please make your selection:"
DIMENSION="19 70 4"
fi
TITLE="SET NETWORK METHOD"

$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"
      IPADDR=127.0.0.1
      infobox "Configuring network $inet with DHCP ..."
      set_hostname
      set_hosts
      set_inet
      clean_exit 0
      ;;
    STATIC)
      PROBE="no"
      DHCP="no"
      ;;
    PROBE)
      PROBE="yes"
      DHCP="no"
      ;;
    NONE)
      infobox "No network ..."
      IPADDR=127.0.0.1
      DNS_SERVER=""
      set_hostname
      set_hosts
      set_dns
      disable_inet
      sleep 2
      clean_exit 0
      ;;
esac
return 0
}

########################################################
# Ask an IP Address
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 address 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 address's, 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  (recommended 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:"
## HEY, 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
  if ipmask 0.0.0.0 $reply &> /dev/null; then
    if ! echo $reply | grep -qe "^127\."; then
       IPADDR=$reply
       return 0  
    fi
  fi
  retrybox "Invalid IP address"
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() {

  if [ -z "$GATEWAY" ]; then
    GATEWAY="${IPADDR%.*}.254"
  fi

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() {

  if [ -z "$DNS_SERVER" ]; then
    DNS_SERVER="$GATEWAY"
  fi

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_hosts
  set_inet
  set_dns
  sleep 2
  ## Start it
  if [ -z "$NO_START" ]; then
    $inet_file start > /dev/null 2>&1  
  fi
  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 $?

