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

vdir=$(dirname $0)

. $vdir/vasm-functions

check_root

HOST_FILE="/etc/HOSTNAME"
HOST_NAME="vlocity.linux.vnet"
DNS_FILE="/etc/resolv.conf"
DNS_SERVER="127.0.0.1"
DNSMASQ_FILE="/etc/resolv.conf.dnsmasq"

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

set_hostname()
{
   if pidof 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()
{
   # if dnsmasq is running, set dnsmasq file instead of the standard one
   if pidof dnsmasq &>/dev/null; then
      echo "search ${HOST_NAME#*.}" > $DNSMASQ_FILE
      if [ "$DNS_SERVER" ]; then
        echo "nameserver $DNS_SERVER" >> $DNSMASQ_FILE
      fi
      echo "search ${HOST_NAME#*.}" > $DNS_FILE
      if [ "$DNS_SERVER" ]; then
         echo "nameserver 127.0.0.1" >> $DNS_FILE
      fi
      service dnsmasq reload &> /dev/null
   else
      echo "search ${HOST_NAME#*.}" > $DNS_FILE
      if [ "$DNS_SERVER" ]; then
         echo "nameserver $DNS_SERVER" >> $DNS_FILE
      fi
   fi
}


############################
# Set HOSTNAME
menuA() {
while [ 1 ]; do
TEXT="\n
This configurator sets the hostname and DNS server.\n
Hostname is a unique internet name to call this computer.\n
It should follow the standard convention 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;
  
  HOST_NAME=$(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

