#!/bin/bash

# Ensure required tools are installed
echo "Updating package list and installing unzip if not already installed."
#apt-get update -y && apt-get install -y unzip

# Define the target directory and the script path
#PROTOCOL="<Applicare_protocol>"
#IP="<Applicare_IP>"
#PORT="<Applicare_port>"
TARGET_DIR="/usr/local/ApplicareSingleAgentLinux"
START_SCRIPT="$TARGET_DIR/agentscripts/startAgent.sh"

# Check if the directory exists
if [ ! -d "$TARGET_DIR" ]; then
    # If the directory does not exist, run the curl command
    echo "Directory $TARGET_DIR does not exist. Downloading and extracting ApplicareSingleAgentLinux."
    #curl -k --noproxy '*' "$PROTOCOL://$IP:$PORT/applicare/standalone?userId=$USERID&filename=ApplicareSingleAgentLinux" > ApplicareSingleAgentLinux.zip

    # Extract the downloaded zip file
    #unzip ApplicareSingleAgentLinux.zip -d /usr/local/ApplicareSingleAgentLinux/

    # Set execute permissions for the script if necessary
    chmod -R 777 "$TARGET_DIR"
   
    # Start the singleagent
    nohup bash "$START_SCRIPT" > /dev/null 2>&1 &
    echo "Agent started in nohup mode."
     

else
    # If the directory exists, run the startAgent.sh script in nohup mode
    echo "Directory $TARGET_DIR exists. Starting the agent."
    nohup bash "$START_SCRIPT" > /dev/null 2>&1 &
    echo "Agent started in nohup mode."
fi
