#!/bin/sh

########################################################################
# DO NOT modify this file directly as it will be overwritten the next
# time the VMware Tools are installed. 
########################################################################

echo `date` ": Executing '$0'"
echo

find_initdir() {
    for dir in "/etc/init.d" "/sbin/init.d" "/etc" "/etc/rc.d" ; do
        if [ -d "$dir/rc0.d" ] &&
	    [ -d "$dir/rc1.d" ] &&
	    [ -d "$dir/rc2.d" ] &&
	    [ -d "$dir/rc3.d" ] &&
	    [ -d "$dir/rc4.d" ] &&
	    [ -d "$dir/rc5.d" ] &&
	    [ -d "$dir/rc6.d" ]; then
	    if [ -d "$dir/init.d" ]; then
		echo "$dir/init.d"
	    else
		echo "$dir"
	    fi
	    return
        fi
    done

    echo "error"
}    

rescue_NIC() {
   niclist="/var/run/vmware-active-nics"

   ifup_path=`which ifup 2>/dev/null`;
   if [ $? -ne 0 ]; then
      return 1;
   fi

   ifconfig_path=`which ifconfig 2>/dev/null`;
   if [ $? -ne 0 ]; then
      return 1;
   fi

   if [ -f "$niclist" ]; then
      while read nic; do
         if $ifconfig_path $nic | egrep '^ +UP ' >/dev/null 2>&1; then
            echo `date` "[resume-vm-default::rescue_nic] $nic is already active."
         else
            echo `date` "[rescue_nic] activating $nic ..."

            $ifup_path $nic
         fi
      done < $niclist

      rm -f $niclist
   fi
}

#
# main
#
initdir=`find_initdir`
if [ "$initdir" != "error" ]; then
   if [ -x "$initdir/network" ]; then
      network="$initdir/network"
   elif [ -x "$initdir/networking" ]; then
      network="$initdir/networking"
   else
      echo \"$initdir/network\" or \"$initdir/networking\" not found!
      status=1
   fi
   if [ -n "$network" ]; then
      "$network" restart
      # Continue even if the networking init script wasn't successful.
      status=0
   fi
else
   echo initdir not found
   status=1
fi

if [ $status -eq 0 ]; then
    rescue_NIC
fi

exit "$status"
