#!/bin/sh

# Sends a command to PuavoMenu through the IPC socket

set -eu

if [ "$#" -lt 2 ]; then
    echo "Usage: $0 <socket> arg1 [arg2 ... argN]"
    exit 1
fi

socket=$1
cmd=$2
shift

echo "$@" | nc -U -w 1 "${socket}"

# FIXME: This is an excessively ugly hack, but it works and
# it fixes the keyboard focus problem. We can't use this
# forever though...
if [ "$cmd" = "show" -o "$cmd" = "toggle" ]; then
    window=$(xdotool search --name ProdPuavomenu) || return 1

    if [ -n "$window" ]; then
        sleep 0.2
        xdotool windowactivate "$window" || return 1
    fi
fi
