#!/usr/bin/sh

# Hooks parameter are passed by environment variable:
#
# - RUDDER_GENERATION_DATETIME   : generation datetime: ISO-8601 YYYY-MM-ddTHH:mm:ss.sssZ date/time that identify that policy generation start
# - RUDDER_NODE_ID               : the nodeId
# - RUDDER_NODE_HOSTNAME         : the node fully qualified hostname
# - RUDDER_NODE_POLICY_SERVER_ID : the node policy server id
# - RUDDER_AGENT_TYPE            : agent type ("cfengine-community" or "dsc")
# - RUDDER_POLICIES_DIRECTORY_NEW: the full path of the base directory containing next policies for that node (during a generation) (/var/rudder/share/$RUDDER_NODE_ID/rules.new/$RUDDER_AGENT_TYPE)

if [ -z "${RUDDER_POLICIES_DIRECTORY_NEW}" ]; then
  #bad rules.new directory
  echo "The directory for node ${RUDDER_NODE_ID} new policies is empty"
  exit 1;
else
  case "${RUDDER_AGENT_TYPE}" in
    "cfengine-community")
      exec /opt/rudder/bin/cf-promises -f "${RUDDER_POLICIES_DIRECTORY_NEW}/promises.cf"
    ;;

    "dsc")
      # Not checking dsc agent policies for now
      exit 0
    ;;

    *)
      #unknown agent type
      echo "Agent type ${RUDDER_AGENT_TYPE} for node ${RUDDER_NODE_ID} is not supported"
      exit 2
    ;;
  esac
fi
