#!/bin/sh
#******************************************************************
#ident /etc/netenv/netenv_setup Time-stamp: <03/03/11 21:33:34 bav>
#******************************************************************
#                            Gerd Bavendiek  bav@epost.de  98-04-17
#
# If there is a variable NETENV_SCRIPT pointing to this executable
# file, netenv will source this script during boot. The following is
# what makes sense for me. Take it as a starting point.
#
# Let op: This will be run as root !
#------------------------------------------------------------------
###set -x
#---   Some functions   -------------------------------------------
print_action() 
{
   echo -e netenv: $* " ... \c"  
}
print_status() 
{
   if [ $? -eq 0 ]; then
      echo done
   else
      echo failed
   fi
}
#---   End of functions   -----------------------------------------

#---   Do everything needed when working at home   ----------------
PROFILE=$1
print_action Using PROFILE $PROFILE
echo

if [ "$PROFILE" = "default" ]; then
   print_action linking /dev/pilot
   rm -f /dev/pilot; ln -s /dev/ttyS0 /dev/pilot
   print_status
   print_action calling ptal-init start
   /usr/sbin/ptal-init start 
   print_status
else
   # When there is no docking station, start irattach appropiately and
   # link /dev/pilot to ircomm0.
   print_action running irattach
   /usr/sbin/irattach /dev/ttyS3 -s
   print_status
   print_status linking /dev/pilot
   rm -f /dev/pilot; ln -s /dev/ircomm0 /dev/pilot
   print_status
fi

#---   Set defaultgateway   ---------------------------------------
if [ ! -z "$GATEWAY" ]; then
   print_action Defaultgateway setting to $GATEWAY, see /etc/sysconfig/network/routes
   (
      echo "# routs autogenerated by netenv "`date`
      echo "default $GATEWAY - -"
   ) > /etc/sysconfig/network/routes
   print_status done
fi

#---   Set up /etc/resolv.conf   ----------------------------------
if [ ! -z "$DNS_1" ]; then 
    rm -f /etc/resolv.conf
    (
    echo "# resolv.conf autogenerated by netenv "`date`
    if [ ! -z "$DOMAIN" ]; then echo domain $DOMAIN; fi
    if [ ! -z "$SEARCH" ]; then echo search $SEARCH; fi
    echo nameserver $DNS_1
    if [ ! -z "$DNS_2" ]; then echo nameserver $DNS_2; fi
    ) > /etc/resolv.conf.netenv
    chmod 644 /etc/resolv.conf.netenv
    print_action setting up new /etc/resolv.conf
    ln -s /etc/resolv.conf.netenv /etc/resolv.conf
    print_status
fi

#---   Edit /etc/hosts   -------------------------------------------

if [ -n "$NETENV_HOST_ENTRIES" ]; then
   if egrep '^# netenv hosts begin$' /etc/hosts > /dev/null 2>&1; then
      (
      awk 'NR==1,/^# netenv hosts begin$/' /etc/hosts
      echo $NETENV_HOST_ENTRIES | awk -F':' '{ for ( j=1; j<=NF; j++ ) print $j }'
      awk '/^# netenv hosts end$/,/*/' /etc/hosts
      ) > /etc/hosts.netenv
      print_action modifying /etc/hosts
      mv -f /etc/hosts.netenv /etc/hosts
      chmod 644 /etc/hosts
      print_status
   fi
else
   if egrep '^# netenv hosts begin$' /etc/hosts > /dev/null 2>&1; then
      (
      awk 'NR==1,/^# netenv hosts begin$/' /etc/hosts
      awk '/^# netenv hosts end$/,/*/' /etc/hosts
      ) > /etc/hosts.netenv
      print_action cleaning up /etc/hosts
      mv -f /etc/hosts.netenv /etc/hosts
      chmod 644 /etc/hosts
      print_status 
   fi
fi
