#!/bin/bash
DEBUG=0
VERSION=4.3.7
LOGIN=AellaMeta
PASSWORD=WroTQfm/W6x10
IP_ADDRESS=$(ifconfig | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep -v '127.0.0.1' | grep -v 255)
VDC_IP_LIST=(192.168.239 192.168.240 192.168.242)
AWS2_IP_LIST=(172.31 10.50.241 10.50.42 10.50.40 10.50.29)
AWS1_IP_LIST=(192.168.32 192.168.129 192.168.130 192.168.132 192.168.29)
echo "Installing Dependancies"
apt install -y net-tools expect dos2unix
if [[ "$DEBUG" = 1 ]]; then
        set -x
fi
for selection in "${VDC_IP_LIST[@]}"; do
    if [[ "$IP_ADDRESS" =~ ^$selection ]]; then
        SENSOR_IP="192.168.242.142"
        LOCATION=VDC
    fi
done
for selection in "${AWS1_IP_LIST[@]}"; do
    if [[ "$IP_ADDRESS" =~ ^$selection ]]; then
        SENSOR_IP="192.168.132.47"
        LOCATION=AWS1
    fi
done
for selection in "${AWS2_IP_LIST[@]}"; do
    if [[ "$IP_ADDRESS" =~ ^$selection ]]; then
        SENSOR_IP="10.50.40.44"
        LOCATION=AWS2
    fi
done
if [[ "$LOCATION" = VDC ]]; then
        echo "Executing Directives for VDC Environment........."
        echo "Attempting to Install CypherX Suite"
        if systemctl is-active --quiet aella_mon; then
                echo "Service aella_mon is already running. Nothing to do."
        else
                curl -k -u $LOGIN:$PASSWORD -o /opt/ds_linux_install.sh https://acps.stellarcyber.ai/release/$VERSION/datasensor/ds_linux_install.sh --fail
                bash /opt/ds_linux_install.sh -v $VERSION
                expect -c "
                        spawn aella_cli
                        sleep 1
                        send \"set cm cm-securicentrix.stellarcyber.cloud\n\"
                        send \"set agg $SENSOR_IP\n\"
                        send \"quit\n\"
                        sleep 1
                        interact
                        "
        fi
                echo "Attempting to configure RSYSLOG"
                if ! grep -q "$SENSOR_IP" "/etc/rsyslog.d/50-default.conf"; then
                                echo "*.* action(type="omfwd" target="$SENSOR_IP" port="514" protocol="udp" action.resumeRetryCount="100" queue.type="linkedList" queue.size="10000")" >> "/etc/rsyslog.d/50-default.conf"
                                service rsyslogd restart
                                service rsyslog restart
                                echo "RSyslog updated."
                else
                                echo "RSyslog already configured"
                fi
fi
if [[ "$LOCATION" = AWS1 ]]; then
        echo "Executing Directives for AWS1 Environment........."
        echo "Attempting to Install CypherX Suite"
        if systemctl is-active --quiet aella_mon; then
                echo "Service aella_mon is already running. Nothing to do."
        else
                curl -k -u $LOGIN:$PASSWORD -o /opt/ds_linux_install.sh https://acps.stellarcyber.ai/release/$VERSION/datasensor/ds_linux_install.sh --fail
                bash /opt/ds_linux_install.sh -v $VERSION
                expect -c "
                        spawn aella_cli
                        sleep 1
                        send \"set cm cm-securicentrix.stellarcyber.cloud\n\"
                        send \"set agg $SENSOR_IP\n\"
                        send \"quit\n\"
                        sleep 1
                        interact
                        "
        fi
                echo "Attempting to configure RSYSLOG"
                if ! grep -q "$SENSOR_IP" "/etc/rsyslog.d/50-default.conf"; then
                                echo "*.* action(type="omfwd" target="$SENSOR_IP" port="514" protocol="udp" action.resumeRetryCount="100" queue.type="linkedList" queue.size="10000")" >> "/etc/rsyslog.d/50-default.conf"
                                service rsyslogd restart
                                service rsyslog restart
                                echo "RSyslog updated."
                else
                                echo "RSyslog already configured"
                fi
fi
if [[ "$LOCATION" = AWS2 ]]; then
        echo "Executing Directives for AWS2 Environment........."
        echo "Attempting to Install CypherX Suite"
        if systemctl is-active --quiet aella_mon; then
                echo "Service aella_mon is already running. Nothing to do."
        else
                curl -k -u $LOGIN:$PASSWORD -o /opt/ds_linux_install.sh https://acps.stellarcyber.ai/release/$VERSION/datasensor/ds_linux_install.sh --fail
                bash /opt/ds_linux_install.sh -v $VERSION
                expect -c "
                        spawn aella_cli
                        sleep 1
                        send \"set cm cm-securicentrix.stellarcyber.cloud\n\"
                        send \"set agg $SENSOR_IP\n\"
                        send \"quit\n\"
                        sleep 1
                        interact
                        "
        fi
                echo "Attempting to configure RSYSLOG"
                if ! grep -q "$SENSOR_IP" "/etc/rsyslog.d/50-default.conf"; then
                                echo "*.* action(type="omfwd" target="$SENSOR_IP" port="514" protocol="udp" action.resumeRetryCount="100" queue.type="linkedList" queue.size="10000")" >> "/etc/rsyslog.d/50-default.conf"
                                service rsyslogd restart
                                service rsyslog restart
                                echo "RSyslog updated."
                else
                                echo "RSyslog already configured"
                fi
fi
##################################################################################################################################################################
echo "Executing commands common to all sites"
