#!/bin/sh

TARGET_DIR=/usr/local/ApplicareSingleAgentLinux
START_SCRIPT="$TARGET_DIR/agentscripts/startAgent.sh"
LOG_FILE="$TARGET_DIR/agent.log"

# Check if the Java process is already running
if pgrep -f "java -jar .*AgentStartHelper.jar" > /dev/null; then
    echo "Agent is already running. Exiting without starting a new instance."
    exit 0
fi

# Ensure the target directory exists
if [ ! -d "$TARGET_DIR" ]; then
    echo "Directory $TARGET_DIR does not exist. Creating the directory."
    mkdir -p "$TARGET_DIR"
    chmod -R 777 "$TARGET_DIR"
fi

# Ensure the start script exists
if [ ! -f "$START_SCRIPT" ]; then
    echo "Error: $START_SCRIPT not found. Please ensure the script is present."
    exit 1
fi

# Start the agent using nohup with sh
echo "Starting the agent in nohup mode using sh. Logs will be written to $LOG_FILE."
nohup sh "$START_SCRIPT" > "$LOG_FILE" 2>&1 &

