#!/usr/bin/env bash

# Waits for and returns the first message generated by a given service, interface path method

BUS_TYPE=${1}
SERVICE=${2}
INTERFACE=${3}
DBUS_PATH=${4}
METHOD=${5}

while IFS= read -r line; do
  if [[ "$line" == *"${INTERFACE}.${METHOD}"* ]] && [[ "$line" == *"${DBUS_PATH}"* ]]; then
    echo "$line"
    exit 0
  fi
  if [[ "$line" == *"Error"* ]]; then
    exit 1
  fi
done < <(gdbus monitor --"${BUS_TYPE}" --dest "${SERVICE}")

exit 1
