#!/bin/sh
#
# Copyright 1998 VMware, Inc.  All rights reserved.
#

# Basic support for IRIX style chkconfig 
# chkconfig: 3 1 01
# description: Cleans up the COS environment

# load system helper functions.
. /etc/init.d/functions
. /etc/init.d/vmware-functions

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

LOCK_FILE=/var/lock/subsys/vmware

IsLocalFileSystem()
{
   return $(df -Pl "${1}" > /dev/null 2>&1)
}

RequireInitProcess()
{
   if [ -z "${runlevel}" ] ; then
      echo "This service may not be executed manually." >&2
      exit 1
   fi
}

CreateIPMINodes()
{
   rm -f /dev/ipmi[0-9]*
   awk '/ipmi/ { system("mknod /dev/"$2" c "$1" 0") }' /proc/devices
}

VMKTCP_IPv6_Enabled()
{
    (esxcfg-module -g tcpip3 | grep -q 'ipv6 *= *1')  2>/dev/null 1>/dev/null
}

port_reserve () 
{
   #Work around for bug 62830, 313387 (you still don't want to know).
   perl <<-END &
   use IO::Socket; \$0 = "portResv90X";
   my \$sock = new IO::Socket::INET(LocalPort => "902", Proto => "tcp", Listen => 1, Reuse => 1);
   my \$sock2 = new IO::Socket::INET(LocalPort => "903", Proto => "tcp", Listen => 1, Reuse => 1);
   die "Could not create socket: \$!\n" unless \$sock;
   die "Could not create socket: \$!\n" unless \$sock2;
   sleep (10) while(1);
END
}

case "${1}" in
   start)
      RequireInitProcess

      # Lock for RH's init system
      [ ! -d "$(basename "${LOCK_FILE}")" ] && mkdir -p "$(basename "${LOCK_FILE}")"
      touch "${LOCK_FILE}"

      if isTroubleMode ; then
         ln -sf /etc/issue.troubleshooting-mode /etc/issue
         action "Skipping $productName init services." /bin/true
         break
      else
         ln -sf /etc/issue.normal /etc/issue
      fi

      echo "Starting $productName services:"

      if [ -z "$(cat /proc/swaps)" ]; then
         esxcfg-init --alert 'No COS Swap Configured'
      fi

      if isFirstRun ; then
         # enable sysrq support ; NOP on release builds
         sysctl -q -w kernel.sysrq=1 >/dev/null 2>&1

         setFirstRun
      fi

      # clear stale PID files
      rm -rf /var/run/vmware/*

      # Clear /tmp to create more space
      if IsLocalFileSystem /tmp ; then
         rm -rf /tmp/*
      fi

      CreateIPMINodes

      #
      # before restoring vswifs, make COS IPv6 config match that of VMkernel
      #
      if VMKTCP_IPv6_Enabled; then
          esxcfg-vswif --enable-ipv6=true > /dev/null
      else
          esxcfg-vswif --enable-ipv6=false > /dev/null
      fi

      # restore vswifs and vmknics, mount vmfs temporarily
      # so dvsdev chardev is available when vswif is on DVS.
      /bin/mount -t vmfs vmfs /vmfs
      esxcfg-vswif -r
      esxcfg-vmknic -r
      # if we run vmknic we have to run route restore again
      esxcfg-route -r
      /bin/umount /vmfs

      # select a manamgement interface
      esxcfg-init -c 

      # Stop progress display on vmkernel status terminal
      esxcfg-init --set-boot-progress done

      port_reserve

      #force an update of the management interface since hostd isn't up yet we will lose the vswif up event
      esxcfg-init -c
   ;;

   stop)
      RequireInitProcess

      # Switch to COS VT
      chvt 1

      # Save any configuration changes
      esxcfg-boot --rebuild

      # Remove the RH lock
      rm -f "${LOCK_FILE}"
   ;;

   *)
      echo "Usage: $(basename "$0") {start|stop|status}"
      exit 1
   ;;
esac
