#!/bin/bash

BASE_DIR="${HOME}/.config/mapthemesh"
VENV_DIR="${BASE_DIR}/venv"

mkdir -p "${BASE_DIR}"

# Helper functions
show_spinner() {
    local pid=$1
    local delay=0.1
    local spinstr='|/-\'
    local i=0

    while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
        # Get the current character for the spinner
        printf "\b%c" "${spinstr:i++%${#spinstr}:1}"
        sleep $delay
    done

    # Clear the spinner once the process is complete
    printf "\b \b \n"
}

install_python_requirements_with_spinner() {
    info "Installing Python requirements...\n"
    local requirementstxt=$1

    # Start curl in the background, suppressing all output and errors
    pip install -r "${requirementstxt}" > /dev/null 2>&1 &

    # Capture the PID of curl
    local pid=$!

    # Show the spinner while curl is running
    show_spinner $pid

    # Wait for curl to complete
    wait $pid

    # Check if the install was successful
    if [ $? -eq 0 ]; then
        success "Python requirements installed.\n"
    else
        error "Failed to install Python requirements."
        exit 1
    fi
}

info() {
    local blue_bg="\033[44m"
    local white_text="\033[97m"
    local reset="\033[0m"
    printf " ${blue_bg}${white_text} INFO ${reset} $1"
}

success() {
    local green_bg="\033[42m"
    local black_text="\033[30m"
    local reset="\033[0m"
    printf " ${green_bg}${black_text} SUCCESS ${reset} $1 \n"
}

error() {
    local red_bg="\033[41m"
    local white_text="\033[97m"
    local reset="\033[0m"
    printf " ${red_bg}${white_text} ERROR ${reset} $1 \n"
}

clear


info "Installing JQ, you may be asked for root password\n"
( sudo apt-get update && sudo apt-get install -y jq ) > /dev/null

# Check if the install was successful
if [ $? -eq 0 ]; then
    success "JQ installed.\n"
else
    error "Failed to install JQ."
    exit 1
fi


info "Setting up Python Virtual Environment...\n"
python -m venv "${VENV_DIR}" > /dev/null 2>&1

# Check if the install was successful
if [ $? -eq 0 ]; then
    success "Python Virtual Environment created.\n"
else
    error "Failed create Python Virtual Environment."
    exit 1
fi

source "${VENV_DIR}/bin/activate"

wget -q -O - https://script.themesh.live/requirements.txt > /tmp/mapthemesh_requirements.txt
install_python_requirements_with_spinner /tmp/mapthemesh_requirements.txt
rm -f /tmp/mapthemesh_requirements.txt

wget -q -O - https://script.themesh.live/mapthemesh-runner > "${BASE_DIR}/mapthemesh-runner"
chmod +x "${BASE_DIR}/mapthemesh-runner"

( crontab -l; echo 'MAILTO=""' ) | crontab -
( crontab -l; echo "PATH=\"${VENV_DIR}/bin:${PATH}\"" ) | crontab -
( crontab -l; echo "HOME=\"${HOME}\"" ) | crontab -
( crontab -l; echo "0 0 * * * (wget -q -O - https://script.themesh.live/requirements.txt > "${BASE_DIR}/requirements.txt" && pip install --upgrade -r "${BASE_DIR}/requirements.txt") > /dev/null 2>&1" ) | crontab -
( crontab -l; echo "*/15 * * * * ${BASE_DIR}/mapthemesh-runner > ${BASE_DIR}/mapthemesh-runner.log 2>&1" ) | crontab -

printf "\n"
success "MapTheMesh Runner Created.\n\n\n"
printf "\n\n"

info "Please edit this file to update it with your API Key."
printf "\n\n"

printf "If you aren't using USB Serial, then uncomment and edit the relevant lines.\n"
