#!/bin/sh
# @description Reset all keys known from this node.
# @man This command will delete all known keys from this node, allowing it to connect to
# @man another server or relay by trusting it.
# @man *Options*:
# @man +
# @man *-f*: force the reset of all keys without asking for confirmation
# @man +
# @man *-v*: run the command in verbose mode

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

VERBOSE=false
FORCE=0
OPTS=""


while getopts "vf" opt; do
  case $opt in
    v)
      VERBOSE=true
      ;;
    f)
      FORCE=1
      ;;
  esac
done

if [ "${UUID}" = "root" ]
then
  echo "Reinitializing all known keys on the root server breaks all agents. ABORTING!" >&2
  exit 1
fi

if [ ${FORCE} -eq 0 ]
then
  echo "Resetting all known keys allows the node to connect to another server than the one it is currently trusting."
  echo "Do yo really want to do it?"
  echo "Type ctrl-c to abort now or enter to continue"
  read a
fi

# - remove all known keys
[ "$VERBOSE" = true ] && echo "Removing all known keys..."
rm -f ${RUDDER_VAR}/cfengine-community/ppkeys/root*
rm -f ${RUDDER_VAR}/cfengine-community/ppkeys/policy_server_hash
rm -f ${RUDDER_VAR}/lib/ssl/policy_server_hash

[ "$VERBOSE" = true ] && echo "Resetting policies..."
${RUDDER_BIN} agent reset
