#!/usr/bin/bash
#
##
## Copyright (c) 2018-2019 Benoit DOLEZ - License MIT
##
## Permission is hereby granted, free of charge, to any person obtaining
## a copy of this software and associated documentation files (the
## "Software"), to deal in the Software without restriction, including
## without limitation the rights to use, copy, modify, merge, publish,
## distribute, sublicense, and/or sell copies of the Software, and to
## permit persons to whom the Software is furnished to do so, subject to
## the following conditions:
##
## The above copyright notice and this permission notice shall be
## included in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
## LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
## OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
## WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##
## Author: Benoit DOLEZ <bdolez@ant-computing.com>
## Author: Benoit DOLEZ <bdolez@zenetys.com>
## Version: 1.0
## Description: add autotrack customvar as notif command
##
#

function usage() {
  echo "Usage: ${0##*/} -H HOST [-S SERVICE] ..."
  echo "  -h, --help: this help"
}

function onError() {
  echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]} in ${FUNCNAME[1]}: ERROR" >&2
}

shopt -s extglob
set -e
declare -a ARGS=( )
declare VERBOSE=0

while (( $# > 0 )); do
  case "$1" in
    ## -h, --help: this help
    -h|--help) usage >&2 && exit 0 ;;
    ## --x-debug: enable bash debug mode
    --x-debug) XDEBUG=1 ;;
    ## -v, --verbose: verbose mode
    -v|--verbose) VERBOSE=1 ;;
    ## -H, --host: define hostname
    -H|--host) HOST=$2; shift;;
    ## -S, --service: define service
    -S|--service) SVC=$2; shift;;
    ## --clear 0|...: need to clear
    --current) CURRENT=$2; shift;;
    ## ignore others arguments
    --) break ;;
    ## store unknown args
    -*) fatal "unknown parameter '$1'" && usage >&2 && exit 1 ;;
    *)  ARGS[${#ARGS[@]}]=$1 ;;
  esac
  shift
done

ARGS+=( "$@" )

trap onError ERR

[[ $XDEBUG ]] && set -x

if [[ $NAGIOS_EXTERNAL_COMMAND_FILE ]]; then
  V=( ${CURRENT//:/$IFS} )
  if (( ${V[0]} )); then
    # Increment counter
    NEW="$((${V[0]}+1)):${V[1]}"
  else
    # First call after reinit
    NEW="1:$(date +%s)"
  fi
  # store new value
  if [[ $SVC ]]; then
    echo "[$(date +%s)] CHANGE_CUSTOM_SVC_VAR;$HOST;$SVC;_AUTOTRACK;$NEW" \
      >> $NAGIOS_EXTERNAL_COMMAND_FILE
  else
    echo "[$(date +%s)] CHANGE_CUSTOM_HOST_VAR;$HOST;_AUTOTRACK;$NEW" \
      >> $NAGIOS_EXTERNAL_COMMAND_FILE
  fi
fi


