#!/bin/sh
# @vasm : vnameset
# @level: root
# @description: change the hostname and dns server
# 
# (c) Eko M. Budi, 2003
# (c) Vector Linux, 2003
#
# Released under GNU GPL

vdir=$(dirname $0)

. $vdir/vasm-functions

check_root

HOST_FILE="/etc/HOSTNAME"
HOST_NAME="vector.linux.vnet"
DNS_FILE="/etc/resolv.conf"
DNS_SERVER="127.0.0.1"

get_hostname()
{
   if [ -r $HOST_FILE ]; then
      HOST_NAME=$(cat $HOST_FILE)
   fi
}

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

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

set_dns()
{
   echo "search ${HOST_NAME##.*}" > $DNS_FILE
   echo "nameserver $DNS_SERVER" >> $DNS_FILE
}


############################
# Set HOSTNAME
menuA() {
while [ 1 ]; do
TEXT="\n
This configurator sets the hostname and DNS server.\n
Hostname is a standard internet name to call your\n
computer. It should look like this:\n
   myhost.mydomain.mynet\n\n
Please enter the hostname now."
TITLE="SET HOSTNAME"
  $DCMD --backtitle "$BACKTITLE" --title "$TITLE" --inputbox "$TEXT" 15 60 $HOST_NAME 2> $freply
  status=$?
  [ $status != 0 ] && return $status;
  
  HOSTNAME=$(cat $freply)

  # should check more carefully it here
  [ "$HOST_NAME" ] && return 0

  retrybox "Please enter a proper hostname"
done
}

############################
# Main
menuB() {

while [ 1 ]; do
TEXT="\n
DNS (Domain Name Server) is the computer that helps to find\n
other hosts by their internet name. Without a DNS server,\n
you will call other computers by their IP address.\n\n
Enter the IP address of the DNS server (e.g. 192.168.1.254)\n
or leave it empty if you don't have one."
TITLE="SET DNS SERVER"
  $WCMD --backtitle "$BACKTITLE" --title "$TITLE" --inputbox "$TEXT" 14 64 $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"
done
}

menuC() {
  infobox "Setting up hostname and DNS server ..."
  set_hostname
  set_dns
  sleep 2
  clean_exit 0
}

############################
# MAIN
get_hostname
get_dns

wizard menuA menuB menuC

