#!/bin/bash
MODEL_SETUP=0

# Check the first argument ($1)
if [[ -n "$1" ]]; then
    if [[ "$1" == "-i" || "$1" == "--install_models" ]]; then
        MODEL_SETUP=1
    else
        echo "Invalid option: $1"
        echo "Use -i or --install_models to run model setup."
        exit 1
    fi
fi

# Get the directory of the currently executing script
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Get the current working directory
current_dir="$(pwd)"

# Compare the directories
if [[ "$script_dir" == "$current_dir" ]]; then
    # If they are the same, move up one directory
    cd ..
fi

echo "**** openvino-ai-plugins-gimp Setup started ****"

# Create a virtual environment
python3 -m venv gimpenv3

# Activate the virtual environment
source gimpenv3/bin/activate

# Upgrade pip and install required packages
pip3 install -r "$script_dir/requirements.txt" | grep -v "already satisfied"
pip3 install "$script_dir/."

# post installation steps
python3 -c "from gimpopenvino import install_utils; install_utils.complete_install(repo_weights_dir=r'${script_dir}/weights')"

echo "**** openvino-ai-plugins-gimp Setup Ended ****"
# Deactivate the virtual environment
deactivate

# Copy to GIMP plugin dir
echo "Installing plugin in $HOME/.config/GIMP/3.0/plug-ins"
for d in openvino_utils semseg_ov stable_diffusion_ov superresolution_ov; do
    mkdir -p "$HOME/.config/GIMP/3.0/plug-ins/$d"
    rsync -a gimpenv3/lib/python*/site-packages/gimpopenvino/plugins/"$d" "$HOME/.config/GIMP/3.0/plug-ins/."
done
echo "*** openvino-ai-plugins-gimp Installed ***"

# If MODEL_SETUP was set, run the model setup
if [[ "$MODEL_SETUP" -eq 1 ]]; then
    echo "**** OpenVINO MODEL SETUP STARTED ****"
    gimpenv3/bin/python3 "$script_dir/model_setup.py"
fi

cd $current_dir

exit 0
