#!/bin/bash
# run this with one line:
#  curl https://raw.githubusercontent.com/zackees/install.py/main/install -o install && bash ./install

set -e

kernelName="$(uname -s)"

# Get the directory of the current script
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# Change to that directory
cd "$script_dir"
echo changed to $(pwd)

# Check if install.py exists, if not, download it
if [ ! -f "./install.py" ]; then
    curl https://raw.githubusercontent.com/zackees/install.py/main/install.py -o ./install.py
else
    echo "install.py already exists. Skipping download."
fi

if [ "$kernelName" == "Darwin" ]; then
    # Check if Homebrew is installed
    if command -v brew >/dev/null 2>&1; then
        echo "Homebrew is installed. Continuing with installation."
        brew install python@3.11
        python3.11 -m venv venv
        source venv/bin/activate
        ls -al
        pwd
        python ./install.py
        echo "finished macos install"
        exit 0
    else
        echo "Error: Homebrew is not installed. Please install Homebrew and rerun this script."
        exit 1
    fi
elif [ "$kernelName" == "Linux" ]; then
    sudo apt-get install python3.11 python3.11-venv
    python3.11 -m venv venv
    source venv/bin/activate
    python ./install.py
    echo "finished linux install"
    exit 0
elif [[ "$kernelName" == CYGWIN* ]] || [[ "$kernelName" == MINGW32* ]] || [[ "$kernelName" == MSYS* ]] || [[ "$kernelName" == MINGW* ]]; then
    echo "Windows OS detected"
    python -m venv venv
    source venv/Scripts/activate
    python install.py
    echo "finished windows install"
    exit 0
else
    # Error
    echo "Unknown operating system: $kernelName"
    exit 1
fi
