#!/bin/sh
#******************************************************************
#ident /etc/init.d/netenv Time-stamp: <98/03/27 08:21:39 bav>
#******************************************************************
#                           Gerd Bavendiek  bav@rw.sni.de  97-05-00
#
# This script is used to set up a file containing information about
# the actual network environment. This can be esp. useful for a
# laptop being used in multiple environment: at home, in the office,
# at a customer site.
#
# Additionally /etc/resolv.conf will be manipulated according to
# entries in the current description file.
#
# After the comment section has become quite large, I swapped it to a
# README file. 
#
# netenv will fail when there is no dialog(1) found !
#-------------------------------------------------------------------
NETENV_FIL=/tmp/netenv	# When located in /tmp, script must be called
                        # AFTER wiping out /tmp has been done ...
NETENV_BASE=/etc/netenv
COLS=68
TMPFIL=/tmp/dialog.$$

TERM=linux; export TERM # We are called from init, so we need to know

# Some functions ...

ExitOnCancel() { echo "You have canceled the setup routine. No Network \
configuration will be done !"; exit 1; }
ExitOnError() { echo "An unknown error ocurred during setup. No Network \
configuration will be done !"; exit 1; }

CheckReturnVal()
{
   case $? in
      0) ;;
      1) ExitOnCancel;;
      *) ExitOnError;;
   esac
}
CheckNoInput()
{
   if [ "$1" = "" ]; then 
      echo "You did not respond with input. No Network \
       configuration will be done !"
      exit 1
   fi
}

get_netenvdata()
{   


   while true
   do
      
      dialog --inputbox "IP-Address\nEnter the current IP-address ..." \
       9 $COLS $IPADDR 2>$TMPFIL
      CheckReturnVal
      IPADDR=`cat $TMPFIL`
      CheckNoInput $IPADDR
      
      dialog --inputbox "Netmask\nEnter the netmask of the current subnet..." \
       9 $COLS $NETMASK 2>$TMPFIL
      CheckReturnVal
      NETMASK=`cat $TMPFIL`
      CheckNoInput $NETMASK
      
      dialog --inputbox "Network\nEnter the IP-Address of the current network ..." \
       9 $COLS $NETWORK 2>$TMPFIL
      CheckReturnVal
      NETWORK=`cat $TMPFIL`
      CheckNoInput $NETWORK
      
      dialog --inputbox "Broadcast\nEnter the broadcast-Address of the current network ..." \
       9 $COLS $BROADCAST 2>$TMPFIL
      CheckReturnVal
      BROADCAST=`cat $TMPFIL`
      CheckNoInput $BROADCAST
      
      dialog --inputbox "Gateway\nEnter the Gateway-Address of the current network ..." \
       9 $COLS $GATEWAY 2>$TMPFIL
      CheckReturnVal
      GATEWAY=`cat $TMPFIL`
      CheckNoInput $GATEWAY

      dialog --inputbox "Profile\nEnter the current Profile's value ..." \
       9 $COLS $PROFILE 2>$TMPFIL
      CheckReturnVal
      PROFILE=`cat $TMPFIL`
      CheckNoInput $PROFILE
      
      (
      echo "The current contents of your /tmp/netenv is as follows:"
      echo
      echo "# Networkenvironment: netask"
      echo export IPADDR=$IPADDR
      echo export NETWORK=$NETWORK
      echo export NETMASK=$NETMASK
      echo export BROADCAST=$BROADCAST
      echo export GATEWAY=$GATEWAY
      echo export PROFILE=$PROFILE
      echo -e "\n\n\n\n\n"
      echo "Answer <Yes> if that looks ok, otherwise <No>"
      echo
      ) > $TMPFIL
	 
      dialog --yesno "`cat $TMPFIL`" 20 72
	 
      if [ $? -eq 0 ]; then
	 (
	 echo "# Networkenvironment: netask"
	 echo export IPADDR=$IPADDR
	 echo export NETWORK=$NETWORK
	 echo export NETMASK=$NETMASK
	 echo export BROADCAST=$BROADCAST
	 echo export GATEWAY=$GATEWAY
	 echo export PROFILE=$PROFILE
	 ) > $NETENV_FIL
	 rm -f $TMPFIL
         break
      else	
	 dialog --yesno "Do you want to repeat the setup process ? " 5 72
	 if [ $? -eq 1 ]; then exit 1; fi		   
      fi
   done

   chmod 644 $NETENV_FIL

   dialog --yesno "Do you want to save this configuration ?" 5 72
   if [ $? -eq 0 ]; then
      dialog --inputbox "Configuration name\nEnter the name under which\
 the configuration should be stored ..." \
       9 $COLS  2>$TMPFIL
      CheckReturnVal
      NETENV=`cat $TMPFIL`
      CheckNoInput $NETENV
      NETCONF_FIL="$NETENV_BASE/"$NODE"-"$NETENV
      cp -p $NETENV_FIL $NETCONF_FIL  
   fi
} # End Function get_confdata

# End of function defining

# Let's do the work ...

NODE=`uname -n`

# The following block will hopefully get us a valid NETENV. It is
# skipped for those, who still prefer input at the boot prompt 
if [ -z "$NETENV" ]; then 
   for i in `ls $NETENV_BASE/\`uname -n\` $NETENV_BASE/\`uname -n\`-*`
   do
      netenv_id="unknown"
      eval `grep netenv_id $i`
      SUFFIX=`basename $i | cut -d '-' -f2`
      ITEM_LIST=$ITEM_LIST" "$SUFFIX" "$netenv_id
   done
   
   dialog --menu "Debian GNU Linux\n\nChoose your current network-environment !" \
    20 $COLS 12 $ITEM_LIST 2>$TMPFIL
   
   if [ $? -ne 0 ]; then
      echo "You didn't choose a network environment - good luck !"
      exit 0
   else
      NETENV=`cat $TMPFIL` && rm -f $TMPFIL
      # Deal with special cases
      if [ "$NETENV" = ask ]; then
	 :
      elif [ "$NETENV" = $NODE ]; then
	 NETENV=""
	 echo "netenv: File $NETENV_BASE/`uname -n` will be used for setting up the network environment ..."
      else	
	 echo "netenv: File $NETENV_BASE/`uname -n`"-"$NETENV will be used for setting up the network environment ..."
      fi
   fi
else
   echo "netenv: Using provided NETENV=$NETENV ..."
fi

# Define the file holding the current network-environment
# configuration. Directory is $NETENV_BASE, filename is e.g. foo-off
# for the current system named "foo" and the Variable NETENV=off
# (Laptop "foo" used in the office). If NETENV has not been defined,
# look for file <nodename>, e.g. "foo".

# Handle special case: ask for environment
if [ "$NETENV" = ask ]; then
   NETCONF_FIL="$NETENV_BASE/"$NODE"-"$NETENV
   if [ -r $NETCONF_FIL ]; then
      . $NETCONF_FIL
   fi

   get_netenvdata

elif [ -z "$NETENV" ]; then
   # Variable NETENV has not been defined or assigned, probably on
   # home tower or something like that ...
   NETCONF_FIL="$NETENV_BASE/"$NODE
else
   NETCONF_FIL="$NETENV_BASE/"$NODE"-"$NETENV
fi	

cp -p $NETCONF_FIL $NETENV_FIL  
chmod 644 $NETENV_FIL

# Caveat: Take the following only as a suggestion. May be I'll work
# this out in the near future.
#
# Set up /etc/resolv.conf if there is at least DOMAIN and NAMESERVER_1
# defined
# . $NETENV_FIL
# if [ ! -z "$DOMAIN" -a ! -z "$NAMESERVER_1" ]; then 
#     (
#     echo "# resolv.conf autogenerated by /etc/init.d/netenv "`date`
#     echo domain $DOMAIN
#     echo nameserver $NAMESERVER_1
#     ) > /etc/resolv.conf
#     echo netenv: /etc/resolv.conf was set up ...
# fi
