#!/bin/bash

# Get the absolute path to the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 
# Function to prompt for user confirmation with 'N' as the default
confirm_with_default() {
    echo -e -n "$2"
    while true; do
        if [ "$1" == "N" ]; then
            read -s -p " (y/N): " -n 1 yn
        else
            read -s -p " (${1}/n): " -n 1 yn
        fi

        if [ -z "$yn" ]; then
            yn="$1"  # Corrected assignment without spaces
        fi

        case $yn in
            [Yy]* ) echo "Y" && return 0;;
            [Nn]* ) echo "N" && return 1;;
            * ) echo -e "${YELLOW}Please answer yes or no.${NC}";;
        esac
    done
}



# Function to prompt for input with a default value
prompt_for_input() {
    read -e -i "$2" -p "$1" value
    echo -e "${value:-$2}"
}

# ANSI escape codes
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BOLD='\033[1m'
# ANSI escape code to reset color
NC='\033[0m' # No Color


# Define the name of your virtual environment directory
VENV_DIR="${SCRIPT_DIR}/venv"

# Function to install PyTorch and run make
install_and_make() {
    echo "Installing PyTorch..."
    pip install torch torchvision torchaudio
    echo "Running make..."
    make
}

# Check if the script is already running inside the intended virtual environment
if [[ "$VIRTUAL_ENV" == "$VENV_DIR" ]]; then
    echo "Already running inside the intended virtual environment at ${VIRTUAL_ENV}."
    install_and_make
else
    if [ -z "$VIRTUAL_ENV" ]; then
        echo "Not currently in a virtual environment."
    else
        echo "Running in a different virtual environment ($VIRTUAL_ENV)"
        # lets ask if we can isntall things there by setting $VENV_DIR to $VIRTUAL_ENV or if we should deactivate OR make a subvenv  
    fi

    # Check if the intended virtual environment directory exists
    if [ ! -d "$VENV_DIR" ]; then
        echo "Creating virtual environment at ${VENV_DIR}..."
        python3 -m venv "$VENV_DIR"
    fi

    echo "Activating the intended virtual environment..."
    source "$VENV_DIR/bin/activate"

    # Verify the activation
    if [[ "$VIRTUAL_ENV" == "$VENV_DIR" ]]; then
        echo "Virtual environment activated at ${VIRTUAL_ENV}."
        install_and_make
    else
        echo "Failed to activate the intended virtual environment."
        exit 1
    fi
fi


swi_prolog_version=$(swipl --version)
if [[ $swi_prolog_version == *"9.1"* ]]; then
    echo -e "${GREEN}SWI-Prolog version 9.1 is already installed${NC}."
else
    echo "${YELLOW}SWI-Prolog is not version 9.1${NC}."
    [ /bin/false ] && (
    sudo apt-add-repository -y ppa:swi-prolog/devel
    #sudo apt-get remove -y swi-prolog*
    sudo apt-get update
    sudo apt-get install -y swi-prolog
    # sudo apt-get install -y swi-prolog-bdb swi-prolog-odbc swi-prolog-java
    if [ $? -ne 0 ]; then
        echo -e "${RED}Failed to install SWI-Prolog. Exiting script${NC}."
        exit 1
    fi
    swi_prolog_version=$(swipl --version)
    #if [[ $swi_prolog_version == *"9.1"* ]]; then
    #    echo -e "${GREEN}SWI-Prolog upgraded to 9.1{NC}."
    #else
    #    echo "${YELLOW}SWI-Prolog is still not version 9.1 .. So Janus will probably fail if not already installed${NC}."
    #fi
    )

fi
function ensure_pip() {
    # Check if pip is installed
    if ! command -v pip &> /dev/null; then
        echo "pip is not installed. Installing pip..."
        sudo apt-get update
        sudo apt-get install -y python3-pip
        if [ $? -ne 0 ]; then
            echo -e "${RED}Failed to install pip. Exiting script${NC}."
            exit 1
        fi
    else
        echo "pip is already installed."
    fi
}

# Assuming SWI-Prolog 9.1 is installed successfully
# Install Janus for SWI-Prolog
echo -e "${BLUE}Checking if Janus Python support is already installed${NC}..."
if ! swipl -g "use_module(library(janus)), halt(0)." -t "halt(1)" 2>/dev/null; then
    # janus not installed, prompt the user
    if [ "${easy_install}" == "Y" ] || confirm_with_default "Y" "Would you like to install Python (Janus) support"; then
            echo "Installing Janus for SWI-Prolog..."
            ensure_pip
            sudo pip install git+https://github.com/SWI-Prolog/packages-swipy.git
            sudo apt install -y libpython3-dev
            if [ $? -ne 0 ]; then
                echo -e "${RED}Failed to install Janus. Exiting script${NC}."
                exit 1
            else
                echo "Janus installed successfully."
            fi
    else
        echo -e "${YELLOW}Skipping Janus Python support installation${NC}."
    fi
else
    echo -e "${GREEN}Janus Python support is already installed${NC}."
fi


# Install PySWIP for SWI-Prolog
echo -e "${BLUE}Checking if Pyswip is already installed${NC}..."
if ! python3 -c "import pyswip" &> /dev/null; then
    # Pyswip not installed, prompt the user
    if [ "${easy_install}" == "Y" ] || confirm_with_default "Y" "Would you like to install Pyswip"; then
        echo -e "${BLUE}Installing Pyswip..${NC}."
        ensure_pip
        sudo pip install git+https://github.com/logicmoo/pyswip.git
        echo -e "${GREEN}Pyswip installation complete${NC}."
    else
        echo -e "${YELLOW}Skipping Pyswip installation${NC}."
    fi
else
    echo -e "${GREEN}Pyswip is already installed${NC}."
fi



#python3 src/mettalog.py $@
python3 src/mettalog.py $@
#python3 src/mettalog_no_panda.py $@

deactivate