#!/bin/sh
# @description re-initialise the agent to make it be seen as a new node on the server
# @man This command will delete all local agent data, including its uuid and
# @man keys, and also reset the agent internal state.
# @man The only configuration kept is the server hostname or ip configured in _policy_server.dat_.
# @man It will also send an inventory to the server, which will treat it as a new node inventory.
# @man +
# @man *WARNING*: This command will permanently delete your node uuid and keys, and no configuration will
# @man be applied before re-accepting and configuring the node on the server.
# @man +
# @man *Options*:
# @man +
# @man *-f*: force the reinitialization without asking for confirmation
# @man +
# @man *-i*: run the agent in information mode, prints basic information
# @man +
# @man *-v*: run the agent in verbose mode, prints detailed information
# @man +
# @man *-d*: run the agent in debug mode, prints low-level information
# @man +
# @man *-q*: run the agent in quiet mode (display only error messages)
# @man +
# @man *-w*: show full strings, never cut output
# @man +
# @man *-c*: run the agent without color output
# @man +
# @man *-T*: display timing information
# @man +
# @man *-r*: run the agent with raw output
# @man +
# @man *-R*: run the agent in completely unparsed mode, with no return code of 1 in case of error. A little faster.

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

VERBOSE=false
QUIET=false
FORCE=0
OPTS=""

reset_logic()
{
  # Backup essential files
  [ "$VERBOSE" = true ] && echo "Making a backup copy of essential files into /var/backups/rudder"
  mkdir -p /var/backups/rudder
  cp -f ${RUDDER_DIR}/etc/uuid.hive /var/backups/rudder/uuid.hive-$(date +%Y%m%d) 2>/dev/null
  cp -f ${RUDDER_VAR}/cfengine-community/policy_server.dat /var/backups/rudder/policy_server.dat-$(date +%Y%m%d) 2>/dev/null
  cp -af ${RUDDER_VAR}/cfengine-community/ppkeys/ /var/backups/rudder/ppkeys-$(date +%Y%m%d) 2>/dev/null
  cp -f ${RUDDER_DIR}/etc/ssl/agent.cert /var/backups/rudder/agent.cert-$(date +%Y%m%d) 2>/dev/null

  # reset policies
  ${RUDDER_BIN} agent reset

  # - remove ppkeys (check will recreate them)
  [ "$VERBOSE" = true ] && echo "Removing the agent keys..."
  rm -f ${RUDDER_VAR}/cfengine-community/ppkeys/localhost*
  rm -f ${RUDDER_DIR}/etc/ssl/agent.cert

  # - remove all trust
  [ "$VERBOSE" = true ] && echo "Untrusting all other systems..."
  rm -f ${RUDDER_VAR}/cfengine-community/ppkeys/*
  rm -f "${SERVER_HASH_FILE}"

  # - remove uuid (check will recreate it)
  [ "$VERBOSE" = true ] && echo "Removing UUID..."
  rm -f ${RUDDER_DIR}/etc/uuid.hive

  # - remove old inventory
  [ "$VERBOSE" = true ] && echo "Removing old inventory..."
  rm -rf ${RUDDER_DIR}/var/fusioninventory/*
  rm -rf ${RUDDER_VAR}/tmp/inventory/*
  rm -f  ${RUDDER_VAR}/tmp/inventory_sent

  # - check and repair everything missing (keys, uuid, reset, update, inventory ...)
  ${RUDDER_BIN} agent check -f -r ${OPTS}
}

while getopts "iIvdqcTf" opt; do
  case $opt in
    i|I|v|d)
      VERBOSE=true
      QUIET=false
      ;;
    q)
      VERBOSE=false
      QUIET=true
      OPTS="${OPTS} -q"
      ;;
    c)
      clear_colors
      OPTS="${OPTS} -c"
      ;;
    T)
      TIMING=1
      ;;
    f)
      FORCE=1
      ;;
  esac
done

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

echo "Reinitializing a node removes its UUID, its keys and trusts, and its applied policies!"
echo "You will need to remove this node from the server and accept it again if it was already accepted."

if [ ${FORCE} -eq 0 ]
then
  echo "Do yo really want to do it?"
  echo "Type no to abort now or yes to continue"
  read yn
  case $yn in
    [Yy]* ) reset_logic;;
    [Nn]* ) echo "Aborting factory reset"; exit 0;;
    * ) echo "Invalid response"; exit 1;;
  esac
else
  reset_logic
fi