#!/bin/bash

declare -a SPECIAL_UNITS=(
  hprobe.timer
  telemprobd.socket
  telempostd.path
  klogscanner.service
  journal-probe-tail.service
  python-probe.path
)

declare -a SERVICES=(
  hprobe.service
  pstore-probe.service
  telemprobd.service
  telempostd.service
  journal-probe.service
)

SCRIPT="$0"
TELEM_DIR=/etc/telemetrics
OPT_OUT_FILE=${TELEM_DIR}/opt-out
TELEM_WRK_DIRS_CONF=/usr/lib/tmpfiles.d/telemetrics-dirs.conf

create_work_dirs() {
  # Creates dirs if missing, adjust ownership if exists
  systemd-tmpfiles --create ${TELEM_WRK_DIRS_CONF}
}

telem_remove_work_dirs() {
  # Remove dirs
  awk '/^d/{print $2}' ${TELEM_WRK_DIRS_CONF} | xargs rm -rf;
}

exit_ok() {
  echo "$1" > /dev/stderr
  exit 0
}

exit_err() {
  echo "$1" > /dev/stderr
  exit 1
}

notice() {
  echo "$1" > /dev/stderr
}

for_each_service() {
  local action=$1 && shift
  local -a array=($*)
  for service in "${array[@]}"; do
    systemctl $action $service
    [ $? -ne 0 ] && notice "Failed to $action ${service}. Continuing..."
  done
}

telem_stop() {
  # the special units must be stopped first so that activation no longer happens
  for_each_service "stop" ${SPECIAL_UNITS[@]}
  for_each_service "stop" ${SERVICES[@]}
}

telem_start() {
  [ -f $OPT_OUT_FILE ] && exit_err "Opt out is enabled. Cannot start services."
  # trigger systemd-tmpfiles work dirs creation
  create_work_dirs
  # the units in SERVICES are activated as needed, so no need to start them
  for_each_service "start" ${SPECIAL_UNITS[@]}
}

telem_enable() {
  [ -f $OPT_OUT_FILE ] && exit_err "Opt out is enabled. Cannot enable services."
  # the units in SERVICES are activated as needed, so no need to enable them
  for_each_service "enable" ${SPECIAL_UNITS[@]}
}

telem_disable() {
  # the units in SERVICES are only enabled, so only disable them, too.
  for_each_service "disable" ${SPECIAL_UNITS[@]}
}

telem_is_active() {
  # check only activation units
  echo "telemprobd :" $(systemctl is-active telemprobd.socket)
  echo "telempostd :"  $(systemctl is-active telempostd.path)
}

telem_opt_out() {
  [ -f $OPT_OUT_FILE ] && exit_ok "Already opted out. Nothing to do."
  mkdir -p $TELEM_DIR || exit_err "Failed to create ${TELEM_DIR}."
  touch $OPT_OUT_FILE || exit_err "Failed to create ${OPT_OUT_FILE}."
  telem_stop
  telem_remove_work_dirs
}

telem_opt_in() {
  [ ! -f $OPT_OUT_FILE ] && exit_ok "Already opted in. Nothing to do."
  rm -f $OPT_OUT_FILE || exit_err "Failed to remove ${OPT_OUT_FILE}."
  telem_start
}

telem_restart() {
  telem_stop
  telem_start
}

telem_journal_cli() {
  telem_journal "$@"
}

usage() {
  format='  %-10s %s\n'
  printf "\n"
  printf "%s - Control actions for telemetry services\n" "$SCRIPT"
  printf "\n"
  printf "$format" "stop" "Stops all running telemetry services"
  printf "$format" "start" "Starts all telemetry services"
  printf "$format" "restart" "Restarts all telemetry services"
  printf "$format" "is-active" "Checks if telemprobd and telempostd are active"
  printf "$format" "enable" "Enable all telemetry services"
  printf "$format" "disable" "Disable all telemetry services"
  printf "$format" "opt-in" "Opts in to telemetry, and starts telemetry services"
  printf "$format" "opt-out" "Opts out of telemetry, and stops telemetry services"
  printf "$format" "journal" "Prints telemtry journal contents. Use -h argument with"
  printf "$format" ""        "command for more options"
  printf "\n"
  exit 2
}

if [ "$1" != "journal" ] && [ $# -ne 1 ]; then
     usage
fi

if [ $EUID -ne 0 ]; then
  exit_err "Must be root to run this command. Exiting..."
fi

SUBCOMMAND=$1

case $SUBCOMMAND in
  disable)
    telem_disable ;;
  enable)
    telem_enable ;;
  opt-out)
    telem_opt_out ;;
  opt-in)
    telem_opt_in ;;
  stop)
    telem_stop ;;
  start)
    telem_start ;;
  restart)
    telem_restart ;;
  is-active)
    telem_is_active ;;
  journal)
    telem_journal_cli "$@" ;;
  *)
    notice "Unknown command passed to $SCRIPT"
    usage ;;
esac

exit 0

# vi: ts=8 sw=2 sts=2 et tw=80
