#!/bin/sh
# @description reset agent status and cache
# @man Remove all locks and state cache of the agent, and restore initial policies.
# @man This won't affect the desired state of the node, but will only
# @man reset the internal state of the agent. It is useful to test a rule
# @man without caching interference or when you have trouble with the policies updates,
# @man and is in most cases sufficient to resolve issues.
# @man +
# @man To completely reinitialize the agent and make it appear as a new node again, please
# @man use "rudder agent factory-reset" instead.
# @man +
# @man *Options*:
# @man +
# @man *-p*: just reset the package cache and nothing else
# @man +
# @man *-i*: run the agent in information mode, prints basic information
# @man +
# @man *-q*: run the agent in quiet mode (display only error messages)
# @man +
# @man *-c*: run the agent without color output

. "${BASEDIR}/../lib/common.sh"

VERBOSITY=""
VERBOSE=false
QUIET=false

while getopts "iIvdqcp" opt; do
  case $opt in
    i|I)
      VERBOSITY="-I ${INFO_CLASS}"
      VERBOSE=true
      QUIET=false
      ;;
    v)
      VERBOSITY="-v ${VERBOSE_CLASS}"
      VERBOSE=true
      QUIET=false
      ;;
    d)
      VERBOSITY="-d ${DEBUG_CLASS}"
      VERBOSE=true
      QUIET=false
      ;;
    q)
      VERBOSITY=""
      VERBOSE=false
      QUIET=true
      ;;
    c)
      COLOR=""
      clear_colors
      ;;
    p)
      # - remove state
      [ "$VERBOSE" = true ] && echo "Resetting package cache..."
      rm -f ${RUDDER_VAR}/cfengine-community/state/packages_*
      exit $?
      ;;
  esac
done

# Remove temporary update archives an etags
rm -rf /var/rudder/tmp/policies*
rm -f /var/rudder/tmp/*.etag

# Try to remove everything that can block

# - remove ncf
rm -rf ${RUDDER_VAR}/ncf/common/*
rm -rf ${RUDDER_VAR}/ncf/local/*

# - remove state
[ "$VERBOSE" = true ] && echo "Resetting internal agent state..."
rm -rf ${RUDDER_VAR}/cfengine-community/state/*
rm -f ${RUDDER_VAR}/cfengine-community/*.lmdb

# - remove locks
[ "$VERBOSE" = true ] && echo "Removing all agent locks..."
rm -f ${RUDDER_VAR}/cfengine-community/*lock

# - remove the disable status
[ "$VERBOSE" = true ] && echo "Enabling the agent if needed..."
rm -f ${RUDDER_DIR}/etc/disable-agent

# - Restore bootstrap/initial policies
# - bootstrap agent (but not on root server without initial promises)
if [ -f "${RUDDER_VAR}/cfengine-community/policy_server.dat" ]
then
  # Update to make sure we have working policies
  [ "$VERBOSE" = true ] && echo "Getting initial policies..."
  # $@ to pass options through
  if is_https_only && [ "${UUID}" = "root" ]; then
    [ "${QUIET}" = false ] && echo "Cannot reset policies on root server in https-only mode"
    exit 1
  else
    reset_policies "$@"
  fi
else
  [ "${QUIET}" = false ] && echo "No policy server file, cannot update"
  exit 1
fi

# Reenable execd service
service_enable rudder-cf-execd enable

if [ "$QUIET" = false ]; then
  printf "${GREEN}ok${NORMAL}: Rudder agent state has been reset.\n"
fi
