#!/bin/bash
# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one
# or more contributor license agreements. Licensed under the Elastic License 2.0 as shown at
# https://securityonion.net/license; you may not use this file except in compliance with the
# Elastic License 2.0.

# Usage: Run with --force to update the Elastic Defend integration policy

. /usr/sbin/so-elastic-fleet-common

# Manage Elastic Defend Integration for Initial Endpoints Policy
for INTEGRATION in /opt/so/conf/elastic-fleet/integrations/elastic-defend/*.json
do
  printf "\n\nInitial Endpoints Policy - Loading $INTEGRATION\n"
  elastic_fleet_integration_check "endpoints-initial" "$INTEGRATION"
  if [ -n "$INTEGRATION_ID" ]; then
    if [ "$1" = "--force" ]; then
      printf "\n\nIntegration $NAME exists - Updating integration\n"
      elastic_fleet_integration_update "$INTEGRATION_ID" "@$INTEGRATION"
    else
      printf "\n\nIntegration $NAME exists - Not updating - rerun with --force to force the update.\n"
    fi
  else
    printf "\n\nIntegration does not exist - Creating integration\n"
    elastic_fleet_integration_create "@$INTEGRATION"
  fi
done
