#!/usr/bin/env bash
#
# Copyright (c) Omnissa, LLC. All rights reserved.
# This product is protected by copyright and intellectual property laws in the
# United States and other countries as well as by international treaties.
# -- Omnissa Restricted
#
# This script manages the Horizon USB Arbitrator service
#

# HORIZON_INIT_INFO

BINDIR=/usr/bin

# 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

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

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

# Execute a macro
horizon_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 [ "$HORIZON_DEBUG" = 'yes' ]; then
    (trap '' HUP; "$func" "$@")
  else
    (trap '' HUP; "$func" "$@") >/dev/null 2>&1
  fi
  if [ "$?" -gt 0 ]; then
    horizon_failed
    echo
    return 1
  fi

  horizon_success
  echo
  return 0
}

# Start the virtual machine USB Arbitrator service
horizonStartUSBArbitrator() {
   # The executable checks for already running instances, so it
   #  is safe to just run it.
   "$BINDIR"/horizon-eucusbarbitrator
}

# Stop the virtual machine USB Arbitrator service
horizonStopUSBArbitrator() {
    # The service knows how to stop itself.
    "$BINDIR"/horizon-eucusbarbitrator --kill
}

horizonService() {
   case "$1" in
      start)
         echo -n "Starting $BASENAME ..."
         horizon_exec 'Horizon USB Arbitrator' horizonStartUSBArbitrator
         exitcode=$(($exitcode + $?))
         if [ "$exitcode" -gt 0 ]; then
            exit 1
         fi
         ;;
      stop)
         echo -n "Stopping $BASENAME ..."
         horizon_exec 'Horizon USB Arbitrator' horizonStopUSBArbitrator
         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

horizonService "$1"

exit 0
