#!/usr/bin/env bash

APIkeyFile=".nagiosapikey"
[ ! -r "$APIkeyFile" ] && APIkeyFile="${HOME}/.nagiosapikey"
[ ! -r "$APIkeyFile" ] && APIkeyFile=""

APIKEY=""
XI_URL=""
curl="curl -k -s"

# myCmd (for "Commands") are now too big for individual variables.  We'll read them into an associative array instead
declare -A myCmd

# We need a separate thing for myOpts and it's easier to have a couple of them be straight variables
declare -A myOpts
myOpts[API]="objects"
myOpts[APIep]="servicestatus"
#myOpts[TestMode]=""
#myOpts[Create]=""
#myOpts[Apply]=""
#myOpts[Options]=""
verbose="0"
tmpJSON=""
tmpQuick=""
tmpQuicker=""

# Different API commands return different JSON datasets.  So let's make a lookup table that figures out where to start the data extracts
declare -A APIinfo
APIinfo["config/command"]=".[]"
APIinfo["config/contact"]=".[]"
APIinfo["config/contactgroup"]=".[]"
APIinfo["config/host"]=".[]"
APIinfo["config/hostgroup"]=".[]"
APIinfo["config/service"]=".[]"
APIinfo["config/servicegroup"]=".[]"
APIinfo["config/timeperiod"]=".[]"

APIinfo["objects/bpi"]=""
APIinfo["objects/comment"]=""
APIinfo["objects/contact"]=".contact[]"
APIinfo["objects/contactgroup"]=".contactgroup[]"
APIinfo["objects/contactgroupmembers"]=".contactgroup[]"
APIinfo["objects/cpexport"]=""
APIinfo["objects/downtime"]=""
APIinfo["objects/hostavailability"]=".hostavailability[]"
APIinfo["objects/hostgroup"]=".hostgroup[]"
APIinfo["objects/hostgroupmembers"]=".hostgroup[]"
APIinfo["objects/host"]=".host[]"
APIinfo["objects/hoststatus"]=".hoststatus[]"
APIinfo["objects/logentries"]=""
APIinfo["objects/rrdexport"]=""
APIinfo["objects/service"]=".service[]"
APIinfo["objects/serviceavailability"]=".serviceavailability[]"
APIinfo["objects/servicegroup"]=".servicegroup[]"
APIinfo["objects/servicegroupmembers"]=".servicegroup[]"
APIinfo["objects/servicestatus"]=".servicestatus[]"
APIinfo["objects/sla"]=""
APIinfo["objects/statehistory"]=".stateentry[]"
APIinfo["objects/timeperiod"]=""
APIinfo["objects/unconfigured"]=".[]"

APIinfo["system/user"]=".users[]"

do_debug() {
  [ "$verbose" -ge "$1" ] && echo "$2" >&2
}

get_myAPI() {
  case "$1" in
    o*) myOpts[API]="objects";;
    c*) myOpts[API]="config";;
    s*) myOpts[API]="system";;
    *)  myOpts[API]="";;
  esac
}

get_myAPIep() {
  case "$1" in
    c|contact) myOpts[APIep]="contact";;
    cg|contactgroup) myOpts[APIep]="contactgroup";;
    cgm|contactgroupmembers) myOpts[APIep]="contactgroupmembers";;
    co|comment) myOpts[APIep]="comment";;
    com|command) myOpts[APIep]="command";;
    dt|downtime) myOpts[APIep]="downtime";;
    ha|hostavailability) myOpts[API]="objects"; myOpts[APIep]="hostavailability";;
    hg|hostgroup) myOpts[APIep]="hostgroup";;
    hgm|hostgroupmembers) myOpts[API]="objects"; myOpts[APIep]="hostgroupmembers";;
    h|host) myOpts[APIep]="host";;
    hs|hoststatus) myOpts[APIep]="hoststatus";;
    le|logentries) myOpts[APIep]="logentries";;
    sa|serviceavailability) myOpts[API]="objects"; myOpts[APIep]="serviceavailability";;
    sgm|servicegroupmembers) myOpts[API]="objects"; myOpts[APIep]="servicegroupmembers";;
    sg|servicegroup) myOpts[APIep]="servicegroup";;
    sh|statehistory) myOpts[API]="objects"; myOpts[APIep]="statehistory";;
    s|service) myOpts[APIep]="service";;
    ss|servicestatus) myOpts[APIep]="servicestatus";;
    tp|timeperiod) myOpts[APIep]="timeperiod";;
    u|user) myOpts[APIep]="user";;
    uo|unconfigured) myOptiosn[API]="objects"; myOpts[APIep]="unconfigured";;
    *) myOpts[APIep]="";;
  esac
}

print_helpopt() {
  cat << HELPOPT_EOF
    Use these options to add options to the command specified.  To add multiple options, specify -o|--opt multiple times (they will add together)

    Options for different APIs:

      all:
	i	Make all string comparisons case-insentitive

      servicestatus|hoststatus:
        c	Show fields selected by -f as quoted CSV
	C	Same as C but also include the fields
	X	Same as C but use a more human-friendly set of names for the fields when printed

      contactgroupmembers:
	m	Only show contacts (not the complete JSON data)
	o	Only show host_object_id (specifying both options will result in errors)

      hostgroupmembers:
	h	Only show hosts (not the complete JSON data)
	o	Only show host_object_id (specifying both options will result in errors)

      servicegroupmembers:
	h	Only show hosts (not the complete JSON data)
        s	Only show service names (not the complete JSON data)
	o	Only show service_object_id (specifying both options will result in errors)

HELPOPT_EOF
  exit
}

print_helpcreate() {
  cat << HELPCREATE_EOF
  If --create is specified, we can create/update various things.  Note that NONE of this data is verified at this point so USE AT YOUR OWN RISK!!!

  1) if --cname, -s, and -h are specified, then we can update <svc> in config_name <cname> so that the host list is <hosts>:
	$0 --create -cn "NAS Checks" -s "Physical Disks" -h "NAS-1,NAS-2"

  2) if -hg and -h are specified, then we can add/update a hostgroup:
	$0 --create -hg Hostgroup -h Host,Host2

  There are other combinations that are undocumented at this time.
HELPCREATE_EOF
  exit
}

print_help() {
  cat << HELP_EOF
    --api	< o*bjects | c*onfig | s*ystem >
    -t|--object	< hoststatus | servicestatus | logentries | statehistory | uo | ...
    --url	XI_URL=<value>

    --ack	problem_has_been_acknowledged=<0,1>
    --ace	active_checks_enabled=<0,1>
    -c|--command	check_command=<value>
    -cca	current_check_attempt=<value>
    --col	column name=<type>:<value>
		Ex: name=lk:local - Displays any matching name with 'local' anywhere in the string.
		Ex: name=in:localhost,nagios,testhost - Displays any matching name with the comma-separated list.
		Note: Multiple --col selectors are not valid at this time
    -cnn	current_notification_number=<n>
    --cname	command_name=<...>
    --cline	command_line=<...>
    --ctrace	Take the --cname given, get its --cline, and replace arguments to show a valid command (whew!)
    -cg|--contactgroup	contact_groups=<value>
    -cn|--configname	config_name=<value>
		... comment | downtime | contact | host | service | hostgroup | ...
    --create	doCreate="true"
    --apply	If we're creating something, then Apply Configuration
    --delete	If we're creating something, then delete it instead (host and service must be specified)
    --disable	Disable a service in a config (must use -cn and -s; sets the service to active_checks_enabled=0)
    --enable	Enable a service in a config (must use -cn and -s; sets the service to active_checks_enabled=1)
    -D		<Nagios Config Directive>=<value> This will take anything listed as a Nagios configuration directive and search for it
    -f|--fields	JQ-valid list of fields to show=<value>
    --file	load JSON from=<value>
    --helpcr*	Show help for create options
    --helpopt	Show help for command options
    --help	This text
    -hg|--hostgroup	hostgroup=<value>
    -hgm	hostgroup_members (mainly for creating hostgroups)
    -h|--host	host_name=<value>
		... hostgroupmembers | servicegroupmembers >
    -j|--jq	additional valid JQ=<value>
    --key	APIKEY=<value>
    --keyfile	APIkeyFile=<value>
    -o|--opt	cmdOptions=<value> (endpoint specific options.  See --helpopt)
    --order 	orderby	<column>:<a or d>
    --output	output text=<value>
    -Q|--quick	Sets -f to .host_name,.service_description,.current_state,.state_type,.problem_has_been_acknowledged (assumes servicestatus)
    -q		Same as --quick but add -o c
    -qq		Same as --quick but add -o c but also print the first line of the CSV output (fields)
    --records	records	<amount>:<starting at>
    --save	save JSON to=<value>
    -sg|--servicegroup	servicegroup=<value>
    -sgm	servicegroup_members (mainly for creating servicegroups)
    -s|--service	service_description=<value>
    --start	For things that have date selectors, this is a "show me after" selector: YYYYMMDDHHMMSS
    --end	For things that have date selectors, this is a "show me before" selector: YYYYMMDDHHMMSS
    --users	Lists XI users
    --usersa	Lists XI users (advanced)
    --useradd	Switches to add user mode (MUST SPECIFY --users AS WELL!!!)
    --status	Quick select for showing similar things that you would see on main servicestatus page in the GUI
    --bstatus	Same as --status but only show things that are not in an OK state
    --hstatus	Quick select for showing similar things that you would see on a main hoststatus page in the GUI
    --bhstatus	Same as --hstatus but only show things that are not in an OK state
    --state	0, 1, or 2 (or other, I suppose)
    --stype	0, 1 (SOFT or HARD)
    --test	Don't call the API, just show what would happen
    --raw	Just get the raw API JSON, ignore everything else (except --file and host/key info)
    -v|--verbose	verbose=\$((\$verbose + 1))
HELP_EOF
  exit
}

# We're going to do nagiostats instead
do_stats() {
  if [ ! -x "/usr/local/nagios/bin/nagiostats" ]; then
    echo "Cannot find nagiostats program."
    exit 1
  fi
  /usr/local/nagios/bin/nagiostats --mrtg --data=MINACTHSTLAT,MAXACTHSTLAT,AVGACTHSTLAT | xargs | while read min max avg; do
    echo "Active Host Check Latency (min/max/avg in ms): $min / $max / $avg"
  done
  /usr/local/nagios/bin/nagiostats --mrtg --data=MINACTSVCLAT,MAXACTSVCLAT,AVGACTSVCLAT | xargs | while read min max avg; do
    echo "Active Service Check Latency (min/max/avg in ms): $min / $max / $avg"
  done
  exit
}

# Convert YYYYMMDDHHSS to Unix Timestamp - for instance, date -d "20240701 08:05" +"%s"
convert_time() {
  do_debug 2 "CONVERT_TIME input is $1"
  theYear="${1:0:4}"; [ -z "$theYear" ] && echo "" && return		# We need a valid year, at least
  theMonth="${1:4:2}"; [ -z "$theMonth" ] && theMonth="01"
  theDay="${1:6:2}"; [ -z "$theDay" ] && theDay="01"
  theHour="${1:8:2}"; [ -z "$theHour" ] && theHour="00"
  theMinute="${1:10:2}"; [ -z "$theMinute" ] && theMinute="00"
  theSecond="${1:12:2}"; [ -z "$theSecond" ] && theSecond="00"
  theTime="$theYear-$theMonth-$theDay $theHour:$theMinute:$theSecond"
  do_debug 2 "CONVERT_TIME output is $theTime"
  do_debug 2 `date -d "$theTime" +"%s"`
  date -d "$theTime" +"%s"
}

# get_opts
while [ -n "$1" ]; do
  case "$1" in
    --helpcr*) print_helpcreate;;
    --helpopt) print_helpopt;;
    --help) print_help;;
    --key) APIKEY="$2"; shift 2;;
    --keyfile) APIkeyFile="$2"; shift 2;;
    --url) XI_URL="$2"; shift 2;;
    -v|--verbose) verbose=$(($verbose + 1)); shift 1;;
    --api) get_myAPI "$2"; shift 2;;
    -t|--object) get_myAPIep "$2"; shift 2;;
    --stats) do_stats; shift 2;;
    --ack) myCmd[problem_has_been_acknowledged]="$2"; shift 2;;
    --ace) myCmd[active_checks_enabled]="$2"; shift 2;;
    -c|--command) myCmd[check_command]="$2"; shift 2;;
    -cca) myCmd[current_check_attempt]="$2"; shift 2;;
    --cname) myCmd[command_name]="$2"; shift 2;;
    --cline) myCmd[command_line]="$2"; shift 2;;
    -cnn) myCmd[current_notification_number]="$2"; shift 2;;
    -cg|--contactgroup) myCmd[contact_groups]="$2"; shift 2;;
    -cn|--configname) myCmd[config_name]="$2"; shift 2;;
    -D) myCmd[$2]="$3"; shift 3;;
    -hg|--hostgroup) myCmd[hostgroup_name]="$2"; shift 2;;
    -hgm) myCmd[hostgroup_members]="$2"; shift 2;;
    -h|--host) myCmd[host_name]="$2"; shift 2;;
    --output) myCmd[output]="$2"; shift 2;;
    -sg|--servicegroup) myCmd[servicegroup_name]="$2"; shift 2;;
    -sgm) myCmd[servicegroup_members]="$2"; shift 2;;
    -s|--service) myCmd[service_description]="$2"; shift 2;;
    --state) myCmd[current_state]="$2"; shift 2;;
    --stype) myCmd[state_type]="$2"; shift 2;;
    --apply) myOpts[Apply]="true"; shift 1;;
    --col) myOpts[Column]="$2"; shift 2;;
    --ctrace) myOpts[CommandTrace]="true"; shift 1;;
    --create) myOpts[Create]="true"; shift 1;;
    --delete) myOpts[Delete]="true"; shift 1;;
    --disable) myOpts[Disable]="true"; shift 1;;
    --enable) myOpts[Enable]="true"; shift 1;;
    --addvar) myOpts[AddVar]="$2"; shift 1;;
    --users) myOpts[Users]="0"; shift 1;;
    --usersa) myOpts[Users]="1"; shift 1;;
    --useradd) myOpts[UserAdd]="$2"; shift 2;;
    --userdel) myOpts[UserDel]="$2"; shift 2;;
    -f|--fields) myOpts[Fields]="$2"; shift 2;;
    --file) myOpts[File]="$2"; shift 2;;
    -j|--jq) myOpts[MoreJQ]="$2"; shift 2;;
    -o|--opt) myOpts[Options]+="$2,"; shift 2;;
    --order) myOpts[OrderBy]="$2"; shift 2;;
    -q) myOpts[Quick]="true"; myOpts[Options]+="c,"; shift 1;;
    -qq) myOpts[Quick]="true"; myOpts[Options]+="c,C,"; shift 1;;
    -Q|--quick) myOpts[Quick]="true"; shift 1;;
    --records) myOpts[Records]="$2"; shift 2;;
    --save) myOpts[Save]="$2"; shift 2;;
    --end) myOpts[End]="$2"; shift 2;;
    --start) myOpts[Start]="$2"; shift 2;;
    --status) myOpts[Status]="true"; shift 1;;
    --hstatus) myOpts[HStatus]="true"; shift 1;;
    --bstatus) myOpts[Status]="true"; myOpts[BStatus]="true"; shift 1;;
    --bhstatus) myOpts[HStatus]="true"; myOpts[BHStatus]="true"; shift 1;;
    --raw) myOpts[Raw]="true"; shift 1;;
    --test) myOpts[TestMode]="true"; shift 1;;
    *) shift 1;;
  esac
done

# Fix things that are impossible to call
[ "${myOpts[API]}" = "config" -a "${myOpts[APIep]}" = "servicestatus" ] && myOpts[APIep]="service"
[ "${myOpts[API]}" = "config" -a "${myOpts[APIep]}" = "hoststatus" ] && myOpts[APIep]="host"

# Convert command line date formats to UNIX time
[ -n "${myOpts[Start]}" ] && myOpts[Start]=`convert_time "${myOpts[Start]}"`
[ -n "${myOpts[End]}" ] && myOpts[End]=`convert_time "${myOpts[End]}"`

# If we're doing status mode, then override a bunch of other options
if [ -n "${myOpts[Status]}" ]; then
  myOpts[API]="objects"
  myOpts[APIep]="servicestatus"
  myOpts[Create]=""
  myOpts[Disable]=""
  myOpts[Apply]=""
  myOpts[Options]=""
  myOpts[Quick]="true"
  myOpts[Options]+="c,X,"
  # If we did a bstatus, then we only want things that are bad
  if [ -n "${myOpts[BStatus]}" ]; then
    myCmd[current_state]="[^0]"
  fi
fi
if [ -n "${myOpts[HStatus]}" ]; then
  myOpts[API]="objects"
  myOpts[APIep]="hoststatus"
  myOpts[Create]=""
  myOpts[Disable]=""
  myOpts[Apply]=""
  myOpts[Options]=""
  myOpts[Quick]="true"
  myOpts[Options]+="c,X,"
  # If we did a bstatus, then we only want things that are bad
  if [ -n "${myOpts[BHStatus]}" ]; then
    myCmd[current_state]="[^0]"
  fi
fi

# If we're listing users, then set things accordingly
if [ -n "${myOpts[Users]}" ]; then
  myOpts[API]="system"
  myOpts[APIep]="user"
fi

# Get our API key
if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
  do_debug 1 "Looking for url=$url in $APIkeyFile"
  while read url key; do
    if [ -z "$XI_URL" ]; then
      XI_URL="$url"
      APIKEY="$key"
      do_debug 1 "Found url=$url key=$key"
      break
    fi
    do_debug 2 "-> Looking for url=$url XI_URL=$XI_URL"
    if [[ "$url" =~ "$XI_URL" ]]; then
      XI_URL="$url"
      APIKEY="$key"
    fi
  done < "$APIkeyFile"
fi
do_debug 1 "Final URL=$XI_URL and key=$APIKEY"
[ -z "$XI_URL" -o -z "$APIKEY" ] && echo "Empty URL or Key." && exit

do_api() {
  api_start="$1"
  api_command="$2"
  url="${XI_URL}/api/v1/${api_start}/${api_command}?pretty=0&apikey=${APIKEY}"
  [ -n "${myOpts[Start]}" ] && url+="&starttime=${myOpts[Start]}"
  [ -n "${myOpts[End]}" ] && url+="&endtime=${myOpts[End]}"
  [ -n "${myOpts[Records]}" ] && url+="&records=${myOpts[Records]}"
  [ -n "${myOpts[OrderBy]}" ] && url+="&orderby=${myOpts[OrderBy]}"
  [ "${myOpts[Users]}" == "1" ] && url+="&advanced=1"
  if [ -n "${myOpts[Column]}" ]; then
    colName=`echo "${myOpts[Column]}" | cut -d= -f 1 | tr " " "+"`
    colVal=`echo "${myOpts[Column]}" | cut -d= -f 2 | tr " " "+"`
    do_debug 1 "colName=$colName colVal=$colVal"
    url+="&$colName=$colVal"
  fi
  do_debug 2 "start=$1 command=$2"
  do_debug 1 "Executing (testmode=${myOpts[TestMode]}): $url"
  [ -z "${myOpts[TestMode]}" ] && $curl -XGET -k "$url"
}

# curl -XPOST "http://<host>/nagiosxi/api/v1/config/hostgroup?apikey=fsZZ4pXaKaVjSG7IYcjMRYhK8NqcqN2NGPck8gPhFoZMJGKj4YUjZCF8qSqsK7Ln&pretty=1" -d "hostgroup_name=testapihostgroup&alias=HostGroup&applyconfig=1"
do_api_post() {
  api_start="$1"
  api_command="$2"
  api_data=""
  [ -n "$3" ] && api_data="$3&applyconfig=0"
  api_type="${4:-XPOST}"
  url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0"
  do_debug 2 "start=$1 command=$2 type=$api_type"
  do_debug 1 "Executing: $curl -${api_type} -k \"$url\" -d \"$api_data\""
  [ -z "${myOpts[TestMode]}" ] && $curl -${api_type} -k "$url" -d "$api_data"
}

do_api_put() {
  api_start="$1"
  api_command="$2"
  api_data=""
  [ -n "$3" ] && api_data="$3&applyconfig=0"
  api_type="${4:-XPUT}"
  url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0&$api_data"
  do_debug 2 "start=$1 command=$2 type=$api_type"
  do_debug 1 "Executing: $curl -${api_type} -k \"$url\""
  [ -z "${myOpts[TestMode]}" ] && $curl -${api_type} -k "$url"
}

do_api_delete() {
  api_start="$1"
  api_command="$2"
  api_data=""
  [ -n "$3" ] && api_data="$3&applyconfig=0"
  api_type="${4:-XPUT}"
  url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0&${api_data}"
  do_debug 2 "start=$1 command=$2 type=$api_type"
  do_debug 1 "Executing: $curl -${api_type} -k \"$url\" -d \"$api_data\""
  [ -z "${myOpts[TestMode]}" ] && $curl -${api_type} -k "$url" #-d "$api_data"
}

# Time to make a hostgroup
create_hostgroup() {
  do_debug 1 "about to do an API post call"
  api_data="hostgroup_name=${myCmd[hostgroup_name]}&alias=${myCmd[hostgroup_name]}"
  [ -n "${myCmd[host_name]}" ] && api_data+="&members=${myCmd[host_name]}"
  [ -n "${myCmd[hostgroup_members]}" ] && api_data+="&hostgroup_members=${myCmd[hostgroup_members]}"
  do_api_post config hostgroup "$api_data"
}

# Time to make a servicegroup
create_servicegroup() {
  do_debug 1 "about to do an API post call"
  api_data="servicegroup_name=${myCmd[servicegroup_name]}&alias=${myCmd[servicegroup_name]}"
  [ -n "${myCmd[service_description]}" ] && api_data+="&members=${myCmd[service_description]}"
  [ -n "${myCmd[servicegroup_members]}" ] && api_data+="&servicegroup_members=${myCmd[servicegroup_members]}"
  do_api_post config servicegroup "$api_data"
}

# Given a config_name and service_description, set the host_name[s] to be the list given in -h
# NEED: curl          -XPUT "http://<host>/nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=<token>&pretty=1&host_name=NAS-H,NAS-W"
#  GOT: curl -k -s -XPUT -k "http://<host>/nagiosxi/api/v1/config/service/NAS/Physical+Disks?apikey=<token>&pretty=0&host_name=NAS-H,NNA&applyconfig=0"
update_service() {
  cname=`echo "${myCmd[config_name]}" | tr " " "+"`
  sname=`echo "${myCmd[service_description]}" | tr " " "+"`
  hname=`echo "${myCmd[host_name]}" | tr " " "+"`
  do_api_put config "service/$cname/$sname" "host_name=$hname"
}

do_enable() {
  do_debug 1 "about to enable a service"
  cname=`echo "${myCmd[config_name]}" | tr " " "+"`
  sname=`echo "${myCmd[service_description]}" | tr " " "+"`
  if [ -z "$cname" -o -z "$sname" ]; then
    echo "When using --enable then you must specify -cn for the config name and -s for the service to enable"
    exit 1
  fi
  do_api_put config "service/$cname/$sname" "active_checks_enabled=1"
}

do_disable() {
  do_debug 1 "about to disable a service"
  cname=`echo "${myCmd[config_name]}" | tr " " "+"`
  sname=`echo "${myCmd[service_description]}" | tr " " "+"`
  if [ -z "$cname" -o -z "$sname" ]; then
    echo "When using --disable then you must specify -cn for the config name and -s for the service to disable"
    exit 1
  fi
  do_api_put config "service/$cname/$sname" "active_checks_enabled=0"
}

# Delete a service, given hostname and service_description
do_delete() {
  do_debug 1 "Trying to delete something..."
  host="${myCmd[host_name]}"
  svc="${myCmd[service_description]}"
  if [ -n "$host" -a -n "$svc" ]; then
    do_debug 1 "Trying to delete host and service, so let's verify first"
    host=`echo "$host" | tr " " "+"`
    svc=`echo "$svc" | tr " " "+"`
    do_debug 1 "DELETE: about to delete $host: $svc"
    api_data="host_name=$host&service_description=$svc"
    do_api_delete config service "$api_data" XDELETE
  else
    user="${myOpts[UserDel]}"
    if [ -n "$user" ]; then
      do_debug 1 "Trying to delete user, so let's verify first"
      userId=`echo "$user" | egrep "^[0-9]+$"`
      [ -z "$userId" ] && echo "You need to specify a numeric UserID to delete" && exit 1
      do_api_delete system "user/$userId" "" XDELETE
    fi
  fi
}

do_create() {
  do_debug 1 "about to do a create_command"
  [ -n "${myOpts[Delete]}" ] && do_delete
  [ -n "${myCmd[host_name]}" -a -n "${myCmd[service_description]}" -a -n "${myCmd[config_name]}" ] && update_service
  [ -n "${myCmd[hostgroup_name]}" ] && create_hostgroup
  [ -n "${myCmd[servicegroup_members]}" ] && create_servicegroup
}

do_apply() {
  do_debug 1 "About to Apply Configuration..."
  do_api_post system applyconfig
}

# curl -XPUT "http://X.X.X.X/nagiosxi/api/v1/config/host/$x?apikey=KEY&pretty=1&old_host_name=testapihost&_foo=bartoo"
# If we used --addvar then we want to add a custom macro to a host.  Other options available later
if [ -n "${myOpts[AddVar]}" ]; then
  if [ -z "${myCmd[host_name]}" ]; then
    echo "When using --addvar then you must specify -h for the host to add to"
    exit 1
  fi
  hname=`echo "${myCmd[host_name]}" | tr " " "+"`
  varName=`echo "${myOpts[AddVar]}" | cut -d= -f 1 | tr " " "+" | sed -e "s/^_//"`
  varVal=`echo "${myOpts[AddVar]}" | cut -d= -f 2 | tr " " "+"`
  do_debug 1 "varName=$varName varVal=$varVal"
  do_api_put config "host/$hname" "_${varName}=${varVal}"
  exit
fi

# If we said "--create" then we want to make something
if [ -n "${myOpts[Create]}" ]; then
  do_create
  [ -n "${myOpts[Apply]}" -a -z "${myOpts[TestMode]}" ] && do_apply
  exit
fi

# If we want to "--enable" [a service] then let's make that happen
if [ -n "${myOpts[Enable]}" ]; then
  do_enable
  [ -n "${myOpts[Apply]}" -a -z "${myOpts[TestMode]}" ] && do_apply
  exit
fi

# If we want to "--disable" [a service] then let's make that happen
if [ -n "${myOpts[Disable]}" ]; then
  do_disable
  [ -n "${myOpts[Apply]}" -a -z "${myOpts[TestMode]}" ] && do_apply
  exit
fi

# If all we said was --apply, then we want to apply previous changes
if [ -n "${myOpts[Apply]}" ]; then
  do_apply
  exit
fi

# Grab a copy of the JSON data so we don't have to keep making calls over and over
# If we used an existing file, then just use that
# If we're in test mode, then skip this part
if [ -z "${myOpts[File]}" ]; then
  tmpJSON=`mktemp`
  do_debug 1 "tmp file is $tmpJSON"
  do_debug 2 "  myAPI is ${myOpts[API]} and myAPIep is ${myOpts[APIep]}"
  do_api "${myOpts[API]}" "${myOpts[APIep]}" > $tmpJSON
else
  do_debug 1 "myFile=${myOpts[File]}"
  tmpJSON="${myOpts[File]}"
  do_debug 1 "tmpJSON=$tmpJSON"
fi

# if mySave is not empty, then we're just saving it into the file called ${myOpts[Save]}
if [ -n "${myOpts[Save]}" ]; then
  mv $tmpJSON ${myOpts[Save]}
  do_debug 1 "JSON data saved to ${myOpts[Save]}"
  exit
fi

# Helper functions for creating our jqString
# First, we need to know if our tests should be case-sensitive or not
jq_check_case() {
  thing="$*"
  do_debug 1 "### JQ_CHECK_CASE looking for thing=$thing"
  if [ -n "$thing" ]; then
    do_debug 2 "### in JQ_CHECK_CASE cmdOptions=${myOpts[Options]}"
    val="| test(\"$thing\""
    [[ "${myOpts[Options]}" =~ "i," ]] && val+="; \"i\""
    echo "$val)"
  else
    echo ""
  fi
}
  
# Create the jQuery search string
jq_get_fields() {
  [[ "${myOpts[Options]}" =~ "c," ]] && jqString+="| [${myOpts[Fields]}] | @csv" || jqString+="| ${myOpts[Fields]}"
}

# Otherwise, let's parse the JSON data here
# Parse our string
do_debug 1 "APIinfo=${APIinfo[${myOpts[API]}/${myOpts[APIep]}]}"
jqString=${APIinfo[${myOpts[API]}/${myOpts[APIep]}]}
do_debug 2 "  Before: jqString=$jqString"
for thing in "${!myCmd[@]}"; do
  [ -n "${myCmd[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myCmd[$thing]}))"
  do_debug 2 "    During: jqString=$jqString"
done
do_debug 2 "  After: jqString=$jqString"

# endpoint specific things
case "${myOpts[API]}/${myOpts[APIep]}" in
  objects/contactgroupmembers)
    [[ ${myOpts[Options]} =~ "m," ]] && jqString+="| .members[] | .[] | .contact_name"
    [[ ${myOpts[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .contact_object_id"
    ;;
  objects/hostgroupmembers)
    [[ ${myOpts[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
    [[ ${myOpts[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .host_object_id"
    ;;
  objects/servicegroupmembers)
    [[ ${myOpts[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
    [[ ${myOpts[Options]} =~ "o," ]] && jqString+="| .members[] | .[] | .service_object_id"
    [[ ${myOpts[Options]} =~ "s," ]] && jqString+="| .members[] | .[] | .service_description"
    tmpQuick=".service_object_id,.host_name,.service_description"
    ;;
  objects/hoststatus) tmpQuick=".host_name,.address,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.check_command";
	tmpQuicker="Host,Address,State,Type,Last Check,Attempt,Normal,Retry,Max,Command";;
  objects/servicestatus) tmpQuick=".service_description,.host_name,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.output";
	tmpQuicker="Service,Host,State,Type,Last Check,Attempt,Normal,Retry,Max,Output";;
  objects/contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";;
  objects/statehistory) tmpQuick=".host_name,.service_description,.state_time,.state_change,.state,.state_type,.current_check_attempt,.max_check_attempts,.last_state,.last_hard_state,.output";;
  config/service)
    # cat service.json| jq '.[] | select (.host_name | index("NAS-W"))'
    jqString=".[]"
    [ -n "${myCmd[host_name]}" ] && jqString+=" | select (.host_name | index(\"${myCmd[host_name]}\"))"
    [ -n "${myCmd[service_description]}" ] && jqString+="| select(.service_description $(jq_check_case ${myCmd[service_description]}))"
    ;;
  config/host) tmpQuick=".host_name,.address,.check_command";;
  config/contact) tmpQuick=".contact_name,.email";;
  system/user) tmpQuick=".user_id,.username,.name,.email,.enabled";;
esac

do_debug 1 "myOpts[Options]=${myOpts[Options]}"
if [ -n "${myOpts[Quick]}" -a -n "${myOpts[Fields]}" ]; then
  myOpts[Fields]="$tmpQuick,${myOpts[Fields]}"
elif [ -n "${myOpts[Quick]}" ]; then
  myOpts[Fields]="$tmpQuick"
fi
[[ ${myOpts[Options]} =~ "C," ]] && echo "${myOpts[Fields]}"
[[ ${myOpts[Options]} =~ "X," ]] && echo "$tmpQuicker" && printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
do_debug 1 "myOpts[Fields]=${myOpts[Fields]}"
[ -n "${myOpts[Fields]}" ] && jq_get_fields
jqString+="${myOpts[MoreJQ]}"
do_debug 1 "jqString=$jqString"

if [ -n "${myOpts[TestMode]}" ]; then
  echo "### TEST MODE - Here is what would have been done (testMode=${myOpts[TestMode]}) ###"
  echo "cat <tmpfile> | jq -r \"$jqString\""
else
  if [ -n "${myOpts[Raw]}" ]; then
    do_debug 1 "*** RAW specified *** Overriding all other options"
    cat $tmpJSON
  else
    cat $tmpJSON | jq -r "$jqString"
  fi
fi
[ -z "${myOpts[File]}" ] && rm $tmpJSON
