#!/bin/bash
#
# Start_Linux.sh
# -----------------------------------------------
# Bootstrap Borealis Virtual Python Environment
# Usage: chmod +x Start_Linux.sh && ./Start_Linux.sh
#

: '
---------------------------------------------------
SECTION 1: Script Initialization & Path Definitions
---------------------------------------------------
This section defines all necessary paths and variables to be used
throughout the script, ensuring clarity and maintainability.
'

# Define paths
venvPath="Borealis-Workflow-Automation-Tool"
dataSource="Data"
dataDestination="$venvPath/Borealis"


: '
---------------------------------------------------
SECTION 2: Virtual Environment Creation
---------------------------------------------------
In this section, we check if the virtual environment already exists
by verifying the presence of the "activate" script. If it doesn’t
exist, we create it.
'

if [ ! -f "$venvPath/bin/activate" ]; then
    echo "Creating virtual environment '$venvPath'..."
    python3 -m venv "$venvPath"
else
    echo "Virtual environment '$venvPath' already exists."
fi


: '
---------------------------------------------------
SECTION 3: Copy Data Folder
---------------------------------------------------
If the "Data" folder is present, we remove any previously copied data
in the virtual environment’s "Borealis" directory, create a new
"Borealis" folder, and then copy the "Data" folder into it.
'

if [ -d "$dataSource" ]; then
    echo "Copying Data folder into virtual environment..."

    # Remove old data if it exists
    if [ -d "$dataDestination" ]; then
        rm -rf "$dataDestination"
    fi

    # Create the Borealis directory inside the virtual environment
    mkdir -p "$dataDestination"

    # Copy Data into the virtual environment under Borealis
    cp -r "$dataSource/"* "$dataDestination/"
else
    echo "Warning: Data folder not found, skipping copy."
fi


: '
---------------------------------------------------
SECTION 4: Activate Environment & Install Dependencies
---------------------------------------------------
This section activates the newly created (or existing) virtual
environment and installs required dependencies based on the
"requirements.txt" file if it exists.
'

echo "Activating virtual environment..."
source "$venvPath/bin/activate"

if [ -f "requirements.txt" ]; then
    echo "Installing dependencies..."
    pip install -r requirements.txt
else
    echo "No requirements.txt found, skipping installation."
fi


: '
---------------------------------------------------
SECTION 5: Run Main Script
---------------------------------------------------
Run the main Python script from within the copied Data folder
inside the virtual environment.
'

if [ -f "$dataDestination/borealis.py" ]; then
    echo "Starting Borealis Workflow Automation Tool..."
    python "$dataDestination/borealis.py"
else
    echo "borealis.py not found in $dataDestination. Skipping execution."
fi


: '
---------------------------------------------------
SECTION 6: Deactivate Environment
---------------------------------------------------
After the main script completes execution, the virtual environment
is deactivated.
'

echo "Deactivating virtual environment..."
deactivate