#!/usr/bin/env bash
# Start the Wave Bot on this computer:  bash run.sh
# First run installs what it needs (about 1 minute). Then open http://localhost:8090
set -e
cd "$(dirname "$0")"

if ! command -v python3 >/dev/null; then
  echo "Python 3 is missing. Install it with:  sudo apt install python3 python3-venv"; exit 1
fi
if [ ! -d .venv ]; then
  echo "First run: setting up (about 1 minute)..."
  if ! python3 -m venv .venv 2>/dev/null; then
    echo "Please run this once, then try again:  sudo apt install python3-venv"; exit 1
  fi
  .venv/bin/pip install -q --upgrade pip
  .venv/bin/pip install -q -r requirements.txt
fi
# make sure newly added libraries are installed after an update
.venv/bin/python -c "import ecdsa, Crypto" 2>/dev/null || .venv/bin/pip install -q -r requirements.txt
[ -f .env ] || cp .env.example .env
mkdir -p data

PORT=$(grep -E '^PORT=' .env | cut -d= -f2); PORT=${PORT:-8090}
( sleep 2; command -v xdg-open >/dev/null && xdg-open "http://localhost:$PORT" >/dev/null 2>&1 ) &
exec .venv/bin/python app.py
