#!/bin/bash

PYTHON=$(which python3)
if [ -z "$PYTHON" ]; then
  echo "Cannot find python3 executable - exiting"
  exit 1
fi

# Check if already running
PID=$(pgrep -u $USER -f 'python3 .*import.*kodi-playing')
if [ ! -z "$PID" ]; then
    echo "kodi-playing is already running - exiting"
    exit 2
fi

mkdir -p "$HOME/.kodi-playing"

DEBUG=false; case "$@" in -d|--debug) DEBUG=true; esac

# Use file as tty output
# Launch with all passed arguments (future reserved)
if $DEBUG; then
    # Use importlib to import a module with a hyphen in its name
    $PYTHON -Wd -c "import importlib; kp = importlib.import_module('kodi-playing'); kp.main()"
else
    $PYTHON -OO -c "import importlib; kp = importlib.import_module('kodi-playing'); kp.main()" &
    echo "You can now close the terminal."
fi

exit 0
