#!/bin/bash

COMMAND=""
CHECK_EXIT=""
CHECK_OUTPUT=""

while test $# -gt 0; do
  case $1 in
    --command|-c) shift; COMMAND=$1;;
    --exit|-e) shift; CHECK_EXIT=$1;;
    --output|-o) shift; CHECK_OUTPUT=$1;;
  esac
  shift
done

output=$($COMMAND 2>&1)
exit_code=$?

if [[ -n "$CHECK_EXIT" ]] && [[ "$exit_code" -ne "$CHECK_EXIT" ]]; then
  echo "CRITICAL - Exit code $exit_code didn't match $CHECK_EXIT"
  exit 2
fi

if [[ -n "$CHECK_OUTPUT" ]] && [[ -z $(echo "$output" | grep "$CHECK_OUTPUT") ]]; then
  echo "CRITICAL - Output '${output}' did not match '${CHECK_OUTPUT}'"
  exit 2
fi

echo "OK - $output"
exit 0
