#!/bin/bash

set -e  # Exit immediately if any command exits with a non-zero status


activate_venv() {
    source env/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }
}

install_module() {
    pip install --upgrade pip || { echo "Failed to upgrade pip"; exit 1; }  # Upgrade pip
    pip install -e . || { echo "Failed to install/upgrade module"; exit 1; }  # Install/upgrade modules
}

update_config() {
    local bak_file="instance/config.py.bak"

    read -p "Enter the external URL your OwnchatBot uses, with \"https://\": " OCB_URL
    
    if [[ -n "$OCB_URL" ]]; then  # Check if OCB_URL is not empty
        echo "OCB_URL = '$OCB_URL'" >> instance/config.py  # Append the line to config.py
        echo "Set OCB_URL successfully."
        # Optionally create a backup of the original config.py
        cp instance/config.py "$bak_file"  # Replace this with your backup file variable
        rm "$bak_file"  # Remove the .bak file if the update was successful
    else
        echo "Failed to set OCB_URL. Please provide a valid URL."
        exit 1  # Exit the script with a non-zero status
    fi
}

update_config

echo -e "Your OwnchatBot configuration has been upgraded. Happy streaming!"
