#!/bin/sh
#******************************************************************
#$ident: @(#) netenv 0.82 Time-stamp: <98/07/03 12:19:58 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.
#
# 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 !
#
# As of version 0.81 netenv will try to use "math" when setting up a
# new environment. math is work of Bruce Perens <Bruce@Pixar.com>. To
# avoid conflicts with e.g. Mathematica it has been renamed to trpnc.
#-------------------------------------------------------------------

# When located in /tmp, script must be called
# AFTER wiping out /tmp has been done ...
NETENV_FIL=`tempfile -n /tmp/netenv`
# At least older Debian system don't know tempfile; the following
# statement should deal with this situation
if [ $? -ne 0 ]; then NETENV_FIL=/tmp/netenv; fi

NETENV_BASE=/etc/netenv
COLS=68

TMPFIL=`tempfile -d /tmp -p dialo`
if [ $? -ne 0 ]; then TMPFIL=/tmp/netenv.tmp.$$; fi

MATH=/usr/bin/trpnc # Needed only when setting up a new environment, see
	            # function get_netenvdata()

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; }

first() { echo $* | cut -f1 -d" "; }
second() { echo $* | cut -f2 -d" "; }
third() { echo $* | cut -f3 -d" "; }
fourth() { echo $* | cut -f4 -d" "; }

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()
{   
   # Executable assigned by $MATH is being used. If it's nor avail,
   # don't panic, just a loss of convenience ...

   while true
   do
      
      dialog --inputbox "IP-Address\nEnter the current IP-address ..." \
       9 $COLS $IPADDR 2>$TMPFIL
      CheckReturnVal
      IPADDR=`cat $TMPFIL`
      CheckNoInput $IPADDR

      ipa=`echo $IPADDR | sed 's/\./ /g'`
      I1=`first $ipa`
      I2=`second $ipa`
      I3=`third $ipa`
      I4=`fourth $ipa`

      # Just a wild guess ...
      NETMASK=255.255.255.0
      
      dialog --inputbox "Netmask\nEnter the netmask of the current subnet..." \
       9 $COLS $NETMASK 2>$TMPFIL
      CheckReturnVal
      NETMASK=`cat $TMPFIL`
      CheckNoInput $NETMASK

      netm=`echo $NETMASK | sed 's/\./ /g'`
      N1=`first $netm`
      N2=`second $netm`
      N3=`third $netm`
      N4=`fourth $netm`
      R1=`$MATH $I1 $N1 and`
      R2=`$MATH $I2 $N2 and`
      R3=`$MATH $I3 $N3 and`
      R4="0"
      NETWORK="$R1.$R2.$R3.$R4"
      
      dialog --inputbox "Network\nEnter the IP-Address of the current network ..." \
       9 $COLS $NETWORK 2>$TMPFIL
      CheckReturnVal
      NETWORK=`cat $TMPFIL`
      CheckNoInput $NETWORK

      R1=`$MATH $N1 not 255 and $I1 or`
      R2=`$MATH $N2 not 255 and $I2 or`
      R3=`$MATH $N3 not 255 and $I3 or`
      R4=`$MATH $N4 not 255 and $I4 or`
      BROADCAST="$R1.$R2.$R3.$R4"
      
      dialog --inputbox "Broadcast\nEnter the broadcast-Address of the current network ..." \
       9 $COLS $BROADCAST 2>$TMPFIL
      CheckReturnVal
      BROADCAST=`cat $TMPFIL`
      CheckNoInput $BROADCAST

      R1=`$MATH $I1 $N1 and`
      R2=`$MATH $I2 $N2 and`
      R3=`$MATH $I3 $N3 and`
      GATEWAY="$R1.$R2.$R3.1"
      
      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
      if [ ! -z "$GATEWAY" ]; then
	 echo export GATEWAY=$GATEWAY
      fi
      if [ ! -z "$PROFILE" ]; then
	 echo export PROFILE=$PROFILE
      fi
      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
	 if [ ! -z "$GATEWAY" ]; then
	    echo export GATEWAY=$GATEWAY
	 fi
	 if [ ! -z "$PROFILE" ]; then
	    echo export PROFILE=$PROFILE
	 fi
	 ) > $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
   # The ls gives a list of files starting with the current node name,
   # excluding emacs backup files. The list starts with the default
   # and ends with ask.
   for i in `ls $NETENV_BASE/\`uname -n\` $NETENV_BASE/\`uname -n\`-*[0-9a-zA-Z]`
   do
      netenv_id="unknown"
      eval `grep netenv_id $i`
      SUFFIX=`basename $i | cut -d '-' -f2-`
      ITEM_LIST=$ITEM_LIST" "$SUFFIX" "$netenv_id
   done
   
      ITEM_LIST=$ITEM_LIST" "new" "Set_up_new_environment
   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" = new ]; 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
   # Continue here, if variable NETENV has been set up as boot argument
   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" = new ]; then
   NETCONF_FIL="$NETENV_BASE/"$NODE"-"$NETENV
   if [ -r $NETCONF_FIL ]; then
      . $NETCONF_FIL
   fi

   get_netenvdata

   # At this point we assume to have a valid NETENV_FIL, eventually a
   # NETCONF_FIL too. The latter is not a must. If the user decided
   # not to store the data it may a once-and-never-again environment.

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	

if [ -r $NETCONF_FIL ]; then
   cp -p $NETCONF_FIL $NETENV_FIL  
   chmod 644 $NETENV_FIL
else
   echo "netenv: Using temporary configuration data ..."
fi

. $NETENV_FIL

# If NETENV_SCRIPT has been defined, run it
if [ -x "$NETENV_SCRIPT" ]; then
   echo "executing $NETENV_SCRIPT ..."
   . $NETENV_SCRIPT
fi


# 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
