#!/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; you may not use
# this file except in compliance with the Elastic License 2.0.
{%- import_yaml 'elasticfleet/defaults.yaml' as ELASTICFLEETDEFAULTS %}
{%- set SUPPORTED_PACKAGES = salt['pillar.get']('elasticfleet:packages', default=ELASTICFLEETDEFAULTS.elasticfleet.packages, merge=True) %}

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

{%- for PACKAGE in SUPPORTED_PACKAGES %}
echo "Setting up {{ PACKAGE }} package..."
if VERSION=$(elastic_fleet_package_version_check "{{ PACKAGE }}"); then
    if ! elastic_fleet_package_install "{{ PACKAGE }}" "$VERSION"; then
        # packages loaded by this script should never fail to install and REQUIRED before an installation of SO can be considered successful
        echo -e "\nERROR: Failed to install default integration package -- $PACKAGE $VERSION"
        exit 1
    fi
else
    echo -e "\nERROR: Failed to get version information for integration $PACKAGE"
    exit 1
fi
echo
{%- endfor %}
echo
