#!/bin/bash

# Try to send the New Idea command to an already running IdeaBox
if gdbus call --session \
    --dest uk.co.strus.IdeaBox \
    --object-path /uk/co/strus/IdeaBox \
    --method org.freedesktop.Application.ActivateAction \
    new_idea \
    "[]" \
    "{}" >/dev/null 2>&1
then
    exit 0
fi

# IdeaBox isn't running, so start it
cd "$HOME/IdeaBox"
python3 ideabox.py >/dev/null 2>&1 &

# Wait for IdeaBox to register on D-Bus
for i in {1..20}; do

    if gdbus call --session \
        --dest uk.co.strus.IdeaBox \
        --object-path /uk/co/strus/IdeaBox \
        --method org.freedesktop.Application.ActivateAction \
        new_idea \
        "[]" \
        "{}" >/dev/null 2>&1
    then
        exit 0
    fi

    sleep 0.25
done
