#!/bin/bash
# Sporet.app lives inside the project folder, as a sibling of app.py --
# this resolves back up to that folder from wherever this bundle's actual
# executable sits (Contents/MacOS/Sporet, three levels down).
DIR="$(cd "$(dirname "$0")/../../.." && pwd)"
cd "$DIR"

if command -v python3 &> /dev/null; then
    PYTHON_CMD="python3"
elif command -v python &> /dev/null; then
    PYTHON_CMD="python"
else
    osascript -e 'display alert "Python not found" message "Install Python from python.org, or with '"'"'brew install python3'"'"', then try again."'
    exit 1
fi

"$PYTHON_CMD" -m pip install -r requirements.txt --quiet --disable-pip-version-check
if [ $? -ne 0 ]; then
    osascript -e 'display alert "Setup failed" message "Sporet couldn'"'"'t install its dependencies automatically. Check your Python/pip setup and try again."'
    exit 1
fi

nohup "$PYTHON_CMD" app.py > sporet.log 2>&1 &
disown
exit 0
