--- netenv.old	2006-11-12 13:12:48.000000000 +0100
+++ netenv	2006-11-12 13:13:59.000000000 +0100
@@ -12,7 +12,7 @@
 # After the comment section has become quite large, I swapped it to a
 # netenv.html file. 
 #
-# netenv will fail when there is no dialog(1) or gdialog found !
+# netenv will fail when dialog(1) is not 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
@@ -20,6 +20,30 @@
 #-------------------------------------------------------------------
 NETENV_VERSION="0.94"
 
+# Are there any command line arguments?
+if [ $# != 0 ]; then
+  case $1 in
+    -v|--version)
+      echo "netenv version 0.94, adapted for Debian GNU/Linux"
+      echo "Copyright 1997-2003 Gerd Bavendiek <bav@epost.de>"
+      ;;
+    -h|--help|*)
+      cat <<EOF
+netenv is an interactive utility to switch between different 
+network configurations. It does not accept any command line 
+arguments, but you can specify the configuration to use by
+setting the variable NETENV.
+
+For creation of new configurations and further environment variables,
+please refer to the man page and html documentation.
+
+EOF
+      ;;
+  esac
+  exit 0
+fi
+
+
 # Security check
 NO_NOT_BY_ROOT_OWNED_FILES=`find /etc/netenv ! -user 0 | wc -l | awk '{print $1}'`
 NO_GROUP_WRITABLE_FILES=`find /etc/netenv -type f -perm -060 | wc -l | awk '{print $1}'`
@@ -33,32 +57,77 @@
 ###set -x
 # When located in /tmp, script must be called
 # AFTER wiping out /tmp has been done ...
-NETENV_FIL=`tempfile -n /tmp/netenv 2>/dev/null`
-# 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
+# Since this is not possible in Debian (bootmisc.sh called after
+# networking), we put the file in /var/tmp, which is preserved upon boot.
+NETENV_FIL="/var/tmp/netenv"
+# Create the netenv file if it doesn't exist yet (only if called by root).
+[ `id -u` = "0" ] && touch -a $NETENV_FIL
+# Security check
+if [ "X`stat -c %u $NETENV_FIL`" != "X0" -o ! -f $NETENV_FIL ]; then
+  echo "netenv: $NETENV_FIL must be a regular file owned by root"
+  exit 1
+fi
+if [ "X$((0`stat -c %a $NETENV_FIL` & 022))" != "X0" ]; then
+  echo "netenv: Security check failed, fix $NETENV_FIL permissions"
+  exit 1
+fi
 
 NETENV_BASE=/etc/netenv
 COLS=68
-
-TMPFIL=`tempfile -d /tmp -p dialo > /dev/null 2>&1`
+TMPFIL=`tempfile -d /tmp -p dialo  2>/dev/null`
+# Set up a trap to remove TMPFIL
+trap "rm -f $TMPFIL; trap 0" 0 1 2 3 13 15
 if [ $? -ne 0 ]; then TMPFIL=/tmp/netenv.tmp.$$; fi
-
-MATH=/usr/bin/trpnc # Needed only when setting up a new environment, see
+# trpnc is no longer used in Debian
+#MATH=/usr/bin/trpnc # Needed only when setting up a new environment, see
 	            # function get_netenvdata()
+DIALOG=dialog
+NODE=`uname -n`
+# dialog is in /usr, which might be on a separate partition. If this is not 
+# mounted, we are in trouble. However, the user can specify a NETENV
+# assignment at the boot prompt.
+nodialog () {
+	echo "dialog binary not found."
+	echo
+	echo "Probably the partition containing /usr could not be mounted"
+	echo "To get a valid network setup, specify it at the boot prompt"
+	echo "like this: linux NETENV=someplace"
+	echo
+	echo "For \"someplace\", put in one of the names after the dash"
+	echo "in the files from the following listing (only the ones starting"
+	echo "with $NODE)."
+	echo
+	ls $NETENV_BASE
+	echo
+	echo "Press enter to continue"
+	read -t 20
+	exit 1
+}
+
+which dialog >/dev/null 2>&1 || DIALOG=nodialog
+
+NETENV_TIMEOUT=0 # as a default, don't timeout at all
+NETENV_RUN_INIT_SCRIPTS=""
+ALLOW_EXPERT=NO
+NETENV_DO_RESTART=""
+NETENV_REMEMBER_LAST=""
+
+# You can change the variables in the configuration file:
+CONFFILE=/etc/netenv/netenv.conf
+
+
+if [ -r $CONFFILE ]; then
+  . $CONFFILE
+fi
+
+# In the configuration file, also NETENV_RUN_INIT_SCRIPTS may be set.
+DEBIAN_RUN_INIT_SCRIPTS="networking $NETENV_RUN_INIT_SCRIPTS"
+
 
 ###TERM=linux; export TERM # We are called from init, so we need to know
 
 # Some functions ...
-DIALOG=gdialog
-if ! type gdialog > /dev/null 2>&1; then
-   if ! type dialog > /dev/null 2>&1; then
-      echo Neither gdialog nor dialog found, aborting ...
-      echo Install packages gnome-utils or dialog to fix !
-      exit 1
-   fi
-   DIALOG=dialog
-fi
+
 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 \
@@ -92,6 +161,8 @@
    # Executable assigned by $MATH is being used. If it's not available,
    # don't panic, just a loss of convenience ...
 
+	 # Debian uses bash's arithmetic evaluation instead of trpnc.
+
    while true
    do
       
@@ -132,9 +203,9 @@
       N2=`second $netm`
       N3=`third $netm`
       N4=`fourth $netm`
-      R1=`$MATH $I1 $N1 and`
-      R2=`$MATH $I2 $N2 and`
-      R3=`$MATH $I3 $N3 and`
+      let R1=$(($I1 & $N1))
+      let R2=$(($I2 & $N2))
+      let R3=$(($I3 & $N3))
       R4="0"
       NETWORK="$R1.$R2.$R3.$R4"
       
@@ -144,10 +215,10 @@
       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`
+      let R1=$((~N1 & 255 | I1))
+      let R2=$((~N2 & 255 | I2))
+      let R3=$((~N3 & 255 | I3))
+      let R4=$((~N4 & 255 | I4))
       BROADCAST="$R1.$R2.$R3.$R4"
       
       $DIALOG --inputbox "Broadcast\nEnter the broadcast-Address of the current network ..." \
@@ -155,10 +226,10 @@
       CheckReturnVal
       BROADCAST=`cat $TMPFIL`
       CheckNoInput $BROADCAST
-
-      R1=`$MATH $I1 $N1 and`
-      R2=`$MATH $I2 $N2 and`
-      R3=`$MATH $I3 $N3 and`
+			
+      let R1=$(($I1 & $N1))
+      let R2=$(($I2 & $N2))
+      let R3=$(($I3 & $N3))
       GATEWAY="$R1.$R2.$R3.1"
       
       $DIALOG --inputbox "Gateway\nEnter the Gateway-Address of the current network ..." \
@@ -227,7 +298,6 @@
  	    fi
          fi
 	 ) > $NETENV_FIL
-	 rm -f $TMPFIL
          break
       else	
 	 $DIALOG --yesno "Do you want to repeat the setup process ? " 5 72
@@ -244,19 +314,34 @@
        9 $COLS  2>$TMPFIL
       CheckReturnVal
       NETENV=`cat $TMPFIL`
-      CheckNoInput $NETENV
-      NETCONF_FIL="$NETENV_BASE/"$NODE"-"$NETENV
+# The scripts requires that there be a file named /etc/netenv/$NODE. 
+# If it does not yet exist, we have to create it.
+      if [ -e /etc/netenv/$NODE ]; then
+				CheckNoInput $NETENV
+				NETCONF_FIL="$NETENV_BASE/"$NODE"-"$NETENV
+      else
+# The file does not yet exist.
+# If the user has let the name field empty, use "default". This is necessary
+# in order to be able to bypass DIALOG by specifying a non-empty NETENV at the
+# boot prompt (in case /usr/ has not been mounted. 
+				if [ X$NETENV = X ]; then
+					NETENV=default
+				fi
+				NETCONF_FIL="$NETENV_BASE/"$NODE"-"$NETENV
+# the target of the link doesn't exit yet, but it will be created soon.
+				(cd $NETENV_BASE; ln -s ${NODE}-${NETENV} $NODE)
+      fi
       cp $NETENV_FIL $NETCONF_FIL # As of 0.92 omit -p (scripts clean /tmp)  
    fi
 } # End Function get_confdata
 
 choose_network_environment()
 {
-   ITEM_LIST="" # have to be reentrant as of 0.93 ...
+   ITEM_LIST=( ) # have to be reentrant as of 0.93 ...
    # 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/$NODE $NETENV_BASE/$NODE-*[0-9a-zA-Z]`
+   for i in `ls $NETENV_BASE/$NODE $NETENV_BASE/$NODE-*[0-9a-zA-Z] 2>/dev/null`
    do
       netenv_id="unknown"
       eval `grep netenv_id $i`
@@ -267,12 +352,28 @@
          LENGTH=$((${#NODE}+1))
          SUFFIX=${SUFFIX:$LENGTH}
       fi
-      ITEM_LIST=$ITEM_LIST" "$SUFFIX" "$netenv_id
+      ITEM_LIST=( "${ITEM_LIST[@]}" "$SUFFIX" "$netenv_id" )
    done
    
-   ITEM_LIST=$ITEM_LIST" "new" "Set_up_new_environment
-   $DIALOG --menu "netenv $NETENV_VERSION on $NODE running `uname -s` `uname -r`\n\nChoose your current network-environment !" \
-    20 $COLS 12 $ITEM_LIST 2>$TMPFIL
+   ITEM_LIST=( "${ITEM_LIST[@]}" "new" "Set up new environment" )
+   CHOOSE_TITLE_TEXT="netenv $NETENV_VERSION on $NODE running `uname -s` `uname -r`\n\nChoose your current network-environment !"
+   NETENV_REMEMBER_LAST=`echo "$NETENV_REMEMBER_LAST" | tr 'A-Z' 'a-z'`
+   if [ \( X"$NETENV_REMEMBER_LAST" = Xyes -o X"$NETENV_REMEMBER_LAST" = Xdefault \) -a -r /var/cache/netenv/lastitem ]; then
+     LAST_ITEM="`cat /var/cache/netenv/lastitem`"
+   else 
+     LAST_ITEM=""
+   fi
+   $DIALOG  --default-item "$LAST_ITEM" --timeout $NETENV_TIMEOUT --menu "$CHOOSE_TITLE_TEXT" 20 $COLS 12 "${ITEM_LIST[@]}" 2>$TMPFIL
+   EXIT_STATUS=$?
+   if [ X`grep -v ^$ $TMPFIL` = Xtimeout ]; then
+     if [ X"$NETENV_REMEMBER_LAST" = Xdefault -a -r /var/cache/netenv/lastitem ]; then
+       cp /var/cache/netenv/lastitem $TMPFIL
+       EXIT_STATUS=0
+     fi
+   else
+     [ $EXIT_STATUS = 0 ] && cp -f $TMPFIL /var/cache/netenv/lastitem
+   fi
+   return "$EXIT_STATUS"
 } # End Function choose_network_environment
 
 # End of function defining
@@ -287,36 +388,45 @@
    ;;
 esac
 
-NODE=`uname -n`
 ###NODE=foo-bar  # TODO
 # 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
    choose_network_environment
-   if [ $? -ne 0 ]; then
-      $DIALOG --yesno "Do you want to enter expert mode ? " 5 72
-      if [ $? -eq 0 ]; then
+   if [ $? -ne 0 -a X`grep -v ^$ $TMPFIL` != Xtimeout ]; then
+     if [ "$ALLOW_EXPERT" = "YES" ]; then
+       $DIALOG --yesno "Do you want to enter expert mode ? " 5 72
+       if [ $? -eq 0 ]; then
          /bin/sh
 	 choose_network_environment
-      else
-         echo "You didn't choose a network environment - good luck !"
-	 rm -f $TMPFIL
-         exit 0
+       else
+	 echo "You didn't choose a network environment - good luck !"
+	 exit 0
+       fi
+     else
+       echo "You didn't choose a network environment - good luck !"
+       exit 0
+     fi
+   else
+#      NETENV=`cat $TMPFIL` && rm -f $TMPFIL
+      NETENV=`grep -v ^$ $TMPFIL` 
+      # Deal with special cases
+      if [ "$NETENV" = new ]; then
+	 :
+      elif [ "$NETENV" = $NODE -o "$NETENV" = timeout ]; then
+	 NETENV=""
+	 echo "netenv: File $NETENV_BASE/$NODE will be used for setting up the network environment ..."
+      else	
+	 echo "netenv: File $NETENV_BASE/$NODE"-"$NETENV will be used for setting up the network environment ..."
       fi
    fi
-   NETENV=`cat $TMPFIL` && rm -f $TMPFIL
-   # Deal with special cases
-   if [ "$NETENV" = new ]; then
-      :
-   elif [ "$NETENV" = $NODE ]; then
-      NETENV=""
-      echo "netenv: File $NETENV_BASE/$NODE will be used for setting up the network environment ..."
-   else	
-      echo "netenv: File $NETENV_BASE/$NODE"-"$NETENV will be used for setting up the network environment ..."
-   fi
 else
    # Continue here, if variable NETENV has been set up as boot argument
    echo "netenv: Using provided NETENV=$NETENV ..."
+   if [ "$NETENV" = $NODE ]; then
+     # user has given the default on the command line.
+     NETENV=""
+   fi
 fi
 
 # Define the file holding the current network-environment
@@ -368,19 +478,41 @@
    . $NETENV_SCRIPT $PROFILE
 fi
 
-rm -f $TMPFIL
 
 # New as of 0.93. If netenv is not run during boot but on the fly, ask the user, if
 # he wants to restart the network
-if [ `tty` != /dev/console ]; then
-   $DIALOG --yesno "Do you want to activate changes by restarting the network ? " 5 72
-   if [ $? -eq 0 ]; then
-      if grep 'Red Hat' /etc/issue > /dev/null; then
-	 /etc/init.d/network restart 
-      elif grep SuSE /etc/issue > /dev/null; then
-         /etc/init.d/network restart
-         # As of SuSE 8.0 route has gone. Still here for 7.3 Users ...
-         if [ -x /etc/init.d/route ]; then /etc/init.d/route restart; fi
+if [ "`tty`" != /dev/console ]; then
+  case $NETENV_DO_RESTART in
+    never|Never|NEVER)
+      DO_RESTART=no;;
+    yes|Yes|YES)
+      DO_RESTART=yes;;
+    *)
+      $DIALOG --yesno "Do you want to activate changes by restarting the network ? " 5 72
+      if [ $? -eq 0 ]; then
+	DO_RESTART=yes
+      else
+	DO_RESTART=no
       fi
-   fi
+      ;;
+  esac
+  if [ $DO_RESTART = "yes" ]; then
+    if grep 'Red Hat' /etc/issue > /dev/null; then
+      /etc/init.d/network restart 
+    elif grep SuSE /etc/issue > /dev/null; then
+      /etc/init.d/network restart
+         # As of SuSE 8.0 route has gone. Still here for 7.3 Users ...
+      if [ -x /etc/init.d/route ]; then /etc/init.d/route restart; fi
+    elif grep Debian /etc/issue > /dev/null; then
+      for script in $DEBIAN_RUN_INIT_SCRIPTS; do
+	/etc/init.d/$script restart;
+      done
+      for script in $NETENV_START_STOP_SCRIPTS; do
+	/etc/init.d/$script stop;
+	/etc/init.d/$script start;
+      done
+    fi
+  fi
 fi
+
+exit 0
