#!/bin/bash

# Copyright 2019 Edward G. Bruck <ed.bruck1@gmail.com>
#
# This file is part of Radiotray-NG.
#
# Radiotray-NG is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Radiotray-NG is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radiotray-NG.  If not, see <http://www.gnu.org/licenses/>.

# Issue dbus commands from a cron job.
#
# Usage: rtng-dbus <command> [ARG1] [ARG2]
#
# Available commands:
#
#    get_bookmarks
#    get_config
#    get_player_state
#    mute
#    next_station
#    play
#    play_station 'group' 'station'
#    play_url 'url'
#    previous_station
#    quit
#    reload_bookmarks
#    set_volume 'level'
#    stop
#    volume_down
#    volume_up

if [ "$#" -gt 0 ]; then
    if [ -z "${DBUS_SESSION_BUS_ADDRESS}" ]; then
        export $(dbus-launch)
        DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
    fi

    DBUS_SEND_CMD="dbus-send --session --print-reply=literal --type=method_call --dest=com.github.radiotray_ng /com/github/radiotray_ng com.github.radiotray_ng.$1"

    for arg in "${@:2}"; do ARGS="$ARGS 'string:$arg'"; done

    eval $DBUS_SEND_CMD $ARGS

    if [ ! -z "${DBUS_SESSION_BUS_PID}" ]; then
        kill $DBUS_SESSION_BUS_PID
    fi
fi
