#!/bin/bash
#
# check and verify status of NGCP system

set -e
set -u

export LC_ALL=C.UTF-8

# basic checks
if ! [ -r /etc/debian_version ] ; then
  echo "Error: this doesn't look like a valid Debian system." >&2
  exit 1
fi

if ! [ -r /etc/ngcp_version ] ; then
  echo "Error: this doesn't look like a valid NGCP system." >&2
  exit 1
fi

if [ "$(id -u)" -ne 0 ] ; then
  echo "Error: please execute this script with root permissions." >&2
  exit 1
fi

# system information
DEBIAN_VERSION="$(cat /etc/debian_version)"
export DEBIAN_VERSION
NGCP_VERSION="$(cat /etc/ngcp_version)"
export NGCP_VERSION
NGCP_STATUS_VERSION="$(dpkg-query --show --showformat="\${Version}" ngcp-status 2>/dev/null|| true)"

COLOR=${COLOR:-auto}
if [[ "${COLOR}" = "auto" ]] ; then
  if [[ -t 1 ]] ; then
    COLOR=true
  else
    COLOR=false
  fi
fi
export COLOR

# helper functions
usage() {
  echo "Usage: $0 [<action>...]

Action:
  --all          Check system status for all registered nodes.
  --ignore-load  Do not report high load situation as error.
  --integrity    Check system integrity for this node.
  --help         Display this help text and exit program.
  --hw           Display hardware related detailed summary.
  --status       Check system status for this node (default).
  --format=F     Use F as output format (values: text, json).
  --version      Display program version and exit program.
  "
}

# cmdline handling
CMDLINE_OPTS=all,help,hw,ignore-load,integrity,status,version,format:

if ! _opt_temp=$(getopt --name ngcp-status -o h --long $CMDLINE_OPTS -- "$@"); then
  echo "Try 'ngcp-status --help' for more information." >&2
  exit 1
fi
eval set -- "$_opt_temp"

_opt_action=status
_opt_format=text

while :; do
  case "$1" in
    --status)
      _opt_action=status
      ;;
    --all)
      _opt_action=status_all
      ;;
    --ignore-load)
      export NGCP_STATUS_IGNORE="load"
      ;;
    --integrity)
      _opt_action=integrity
      ;;
    --help|-h)
      _opt_action=help
      ;;
    --hw)
      _opt_action=hardware
      ;;
    --version)
      _opt_action=version
      ;;
    --format)
      shift
      case "$1" in
        text|json)
          _opt_format="$1"
          NGCP_STATUS_FORMAT="${_opt_format}"
          export NGCP_STATUS_FORMAT
          ;;
        *)
          echo "Error: unknown output format $1" >&2
          exit 1
          ;;
      esac
      ;;
    --)
      shift
      break
      ;;
    *)
      echo "Internal getopt error!" >&2
      exit 1
      ;;
  esac
  shift
done

case "$_opt_action" in
  status)
    /usr/share/ngcp-status/functions/host-summary
    exit $?
    ;;
  status_all)
    if which ngcp-parallel-ssh >/dev/null ; then
      ngcp-parallel-ssh all "COLOR=${COLOR} ngcp-status --format=${_opt_format}"
    else
      echo "Warning: missing ngcp-parallel-ssh, checking local system only!" >&2
      /usr/share/ngcp-status/functions/host-summary
    fi
    exit $?
    ;;
  integrity)
    if ! /usr/share/ngcp-status/functions/integrity-check ; then
      echo "* WARNING: Identified problems (see above), please investigate."
      exit 1
    else
      echo "* Everything OK, nothing to do."
    fi
    ;;
  version)
    echo "${NGCP_STATUS_VERSION:-}"
    exit 0
    ;;
  help)
    usage
    exit 0
    ;;
  hardware)
    inxi -Fxxz
    exit 0
    ;;
esac

exit 0
