#!/usr/bin/env bash
#
# Copyright 2015 VMware, Inc.  All rights reserved.
#
# This script manages the VMware View Usbd service
#

### BEGIN INIT INFO
# Provides: vmware-view-USBD
# Required-Start: vmware-USBArbitrator
# Required-Stop: vmware-USBArbitrator
# X-Start-Before: 
# X-Stop-After: 
# Default-Start: 2 3 4
# Default-Stop: 0 6
# Short-Description: This services starts and stops the View USBD.
### END INIT INFO


# Load bootstrapper info
. /etc/vmware/bootstrap

# This defines $rc_done and $rc_failed on S.u.S.E.
if [ -f /etc/rc.config ]; then
   # Don't include the entire file: there could be conflicts
   rc_done=`(. /etc/rc.config; echo "$rc_done")`
   rc_failed=`(. /etc/rc.config; echo "$rc_failed")`
else
   # Make sure the ESC byte is literal: Ash does not support echo -e
   rc_done='[71G done'
   rc_failed='[71Gfailed'
fi

# This defines echo_success() and echo_failure() on RedHat
if [ -r "$INITSCRIPTDIR"'/functions' ]; then
   . "$INITSCRIPTDIR"'/functions'
fi

vmware_failed() {
  if [ "`type -t 'echo_failure' 2>/dev/null`" = 'function' ]; then
    echo_failure
  else
    echo -n "$rc_failed"
  fi
}

vmware_success() {
  if [ "`type -t 'echo_success' 2>/dev/null`" = 'function' ]; then
    echo_success
  else
    echo -n "$rc_done"
  fi
}

# Execute a macro
vmware_exec() {
  local msg="$1"  # IN
  local func="$2" # IN
  shift 2

  # On Caldera 2.2, SIGHUP is sent to all our children when this script exits
  # I wanted to use shopt -u huponexit instead but their bash version
  # 1.14.7(1) is too old
  #
  # Ksh does not recognize the SIG prefix in front of a signal name
  if [ "$VMWARE_DEBUG" = 'yes' ]; then
    (trap '' HUP; "$func" "$@")
  else
    (trap '' HUP; "$func" "$@") >/dev/null 2>&1
  fi
  if [ "$?" -gt 0 ]; then
    vmware_failed
    echo
    return 1
  fi

  vmware_success
  echo
  return 0
}

# Start the view USBD service
vmwareStartViewUSBD() {
    # The executable checks for already running instances, so it
    #  is safe to just run it.
    "$BINDIR"/vmware-view-usbd
}

# Stop the view USBD service
vmwareStopViewUSBD() {
   pid=`pgrep -f /usr/bin/vmware-view-usbd`

   if [ "$pid" = "" ]; then
      return 0
   fi

   # Kill the vmware-view-usbd process
   kill -15 $pid
   # Give it a few seconds to shut down properly
   for f in '1 2 3 4 5 6 7 8 9 10'; do
      if ! ps $pid >/dev/null; then
         # No need to wait if it's already down
         break
      fi
      sleep 1
   done

   # Give it a few seconds to shut down after the kill
   for f in '1 2 3 4 5 6 7 8 9 10'; do
      if ! ps $pid >/dev/null; then
         # No need to wait if it's already down
         break
      fi
      sleep 1
   done

   if ps $pid >/dev/null; then
      # Failed to kill it...
      return 1
   else
      # Success!
      return 0
   fi
}

vmwareService() {
   case "$1" in
      start)
         vmware_exec 'VMware View USBD' vmwareStartViewUSBD
         exitcode=$(($exitcode + $?))
         if [ "$exitcode" -gt 0 ]; then
            exit 1
         fi
         ;;
      stop)
         vmware_exec 'VMware View USBD' vmwareStopViewUSBD
         exitcode=$(($exitcode + $?))
         if [ "$exitcode" -gt 0 ]; then
            exit 1
         fi
         ;;
      restart)
         "$SCRIPTNAME" stop && "$SCRIPTNAME" start
         ;;
      *)
         echo "Usage: $BASENAME {start|stop|restart}"
         exit 1
   esac
}

SCRIPTNAME="$0"
BASENAME=`basename "$SCRIPTNAME"`

# Check permissions
if [ "`id -ur`" != '0' ]; then
   echo 'Error: you must be root.'
   echo
   exit 1
fi

vmwareService "$1"

exit 0
