#!/bin/bash
#
# Test if SSH is enabled.

# Exit on unset variable.
set -u

# Exit on first error.
set -e

if (( "${EUID}" == 0 )); then
  >&2 echo "This script doesn't require root privileges."
  >&2 echo 'Please re-run as tinypilot:'
  >&2 echo "  runuser tinypilot --command '$0 $*'"
  exit 1
fi

print_help() {
  cat << EOF
Usage: ${0##*/} [-h]
Returns an exit code of 0 if SSH is enabled and 255 if SSH is disabled.
  -h Display this help and exit.
EOF
}

# Parse command-line arguments.
while getopts 'h' opt; do
  case "${opt}" in
    h)
      print_help
      exit
      ;;
    *)
      print_help >&2
      exit 1
  esac
done

if systemctl is-active --quiet ssh ; then
  exit 0
fi

exit 255
