# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .sh
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.16.4
#   kernelspec:
#     display_name: Bash
#     language: bash
#     name: bash
# ---

# +
#!/bin/bash

# --- Configuration ---
ENV_NAME="CIVIL324_Project"
PYTHON_EXEC="python3" # Use python3, often the default for venv

# --- Check if python3 is available ---
if ! command -v $PYTHON_EXEC &> /dev/null
then
    echo "Error: '$PYTHON_EXEC' command not found."
    echo "Please ensure Python 3 is installed and available in your PATH."
    exit 1
fi

# --- Create the Virtual Environment ---
echo "Creating virtual environment '$ENV_NAME' using '$PYTHON_EXEC -m venv'..."
$PYTHON_EXEC -m venv $ENV_NAME

if [ $? -ne 0 ]; then
    echo "Error: Failed to create virtual environment '$ENV_NAME'."
    exit 1
fi

# --- Activate the Environment (for this script's session) ---
echo "Activating environment '$ENV_NAME' for package installation..."
source $ENV_NAME/bin/activate

# --- Activate the Environment ---
echo "Activating environment '$ENV_NAME' for package installation..."
source $ENV_NAME/bin/activate

# --- Upgrade pip and install wheel ---
echo "Upgrading pip and installing wheel..."
pip install --upgrade pip wheel

# --- Set environment variable to ignore external PROJ ---
echo "Setting PROJ_IGNORE_EXISTING_PROJ_DIR=YES to prevent conflicts..."
export PROJ_IGNORE_EXISTING_PROJ_DIR=YES


# --- Install Python Packages using pip ---
echo "Installing required Python packages into '$ENV_NAME' using pip..."
echo "(This may take a while, especially for geospatial libraries...)"

# List all required packages:
pip install --no-cache-dir \
    geopandas \
    hvplot \
    bokeh \
    panel \
    networkx \
    matplotlib \
    contextily \
    ipykernel

# --- Check pip install exit status ---
PIP_EXIT_CODE=$?
if [ $PIP_EXIT_CODE -ne 0 ]; then
    echo ""
    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    echo "Error: 'pip install' failed with exit code $PIP_EXIT_CODE."
    echo "This often happens with geospatial libraries if pre-compiled wheels"
    echo "are not available for your system AND required system development"
    echo "libraries (like libgdal-dev, libproj-dev, libgeos-dev) are missing."
    echo "If you have sudo access, you might need to install them first, e.g.:"
    echo "  sudo apt-get update && sudo apt-get install -y libgdal-dev libproj-dev libgeos-dev"
    echo "Otherwise, you may be limited by the libraries supported by your cloud environment."
    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    # Deactivate before exiting on error
    deactivate
    exit 1
fi

# --- Register the Kernel with Jupyter ---
echo "Registering '$ENV_NAME' kernel with Jupyter..."
# Use the python from the created environment
python -m ipykernel install --user --name=$ENV_NAME --display-name="Python ($ENV_NAME)"

if [ $? -ne 0 ]; then
    echo "Warning: Failed to automatically register the kernel '$ENV_NAME'."
    echo "You might need to refresh your JupyterLab page and check if it appears,"
    echo "or potentially restart the JupyterLab server if possible."
fi

# --- Deactivate the environment (the script's session) ---
deactivate

# --- Success Message ---
echo ""
echo "--------------------------------------------------------------------"
echo "Success! Environment '$ENV_NAME' created and packages installed."
echo ""
echo "To activate this environment manually in a terminal, run:"
echo "  source $ENV_NAME/bin/activate"
echo ""
echo "**IMPORTANT: In JupyterLab, you don't need to activate manually.**"
echo "1. **Refresh your JupyterLab browser window/tab.**"
echo "2. Go to the 'Kernel' menu > 'Change Kernel...'."
echo "3. Select the kernel named 'Python ($ENV_NAME)'."
echo ""
echo "If the kernel doesn't appear after a refresh, you might need to"
echo "restart your JupyterLab instance (if you have the ability to do so)."
echo "--------------------------------------------------------------------"

exit 0
# -


