#!/bin/bash

# Check if the argument is provided
if [ -z "$1" ]; then
    echo "Usage: $0 <new_core_url>"
    exit 1
fi

# Stop connector
/usr/local/intigua/Intigua-Utils/intigua stop_connector

# Remove csclient.cfg.bin file 
rm /usr/local/intigua/vAgentManager/components/csclient.cfg.bin

# Backup csclient.cfg file
cp /usr/local/intigua/vAgentManager/components/csclient.cfg /usr/local/intigua/vAgentManager/components/csclient.cfg.backup

new_core_url="$1"
new_coreServerAddress="coreServerAddress: \"$new_core_url\""
new_standaloneServiceUrl="standaloneServiceUrl: \"$new_core_url\""

# Temporary file to store modified content
temp_file=$(mktemp)

# Add new lines to the top of the file
echo "$new_coreServerAddress" > "$temp_file"
echo "$new_standaloneServiceUrl" >> "$temp_file"

# Remove lines starting with "coreServerAddress" and "standaloneServiceUrl"
grep -vE '^(coreServerAddress|standaloneServiceUrl):' /usr/local/intigua/vAgentManager/components/csclient.cfg >> "$temp_file"

# Replace the original file with the modified content
mv "$temp_file" /usr/local/intigua/vAgentManager/components/csclient.cfg

echo "Modification complete. Updated csclient.cfg:"
cat /usr/local/intigua/vAgentManager/components/csclient.cfg

sleep 30

/usr/local/intigua/Intigua-Utils/intigua start_connector

