#!/bin/bash

set -e

TITLE="Give Firefox Microphone and Storage Access?"
SUBTITLE="Firefox wants to use your microphone and storage."
BODY="Access persmissions can be changed at any time from the privacy settings."
OPTIONS="{'icon':<'audio-input-microphone-symbolic'>,'choices':<@a(ssa(ss)s) [('camera','Camera',[],'false'),('storage','Storage',[('no','No Access'),('read','Read Access'),('write','Write Access')],'no')]>}"

function help()
{
    cat <<EOF
Test helper to show Access dialogs of the org.freedesktop.portal api
Arguments

  -h Shows help
  -t <String>: The title of the dialog
      Example: "$TITLE"
  -s <String>: The subtitle of the dialog
      Example: "$SUBTITLE"
  -b <String>: The body of the dialog
      Example: "$BODY"
  -o <String>: The options of the dialog (https://flatpak.github.io/xdg-desktop-portal/#gdbus-org.freedesktop.impl.portal.Access)
      Example: "$OPTIONS"

EOF
    exit 0
}

while getopts "ht:s:b:o:" opt; do
  case $opt in
    t) TITLE="$OPTARG"
    ;;
    s) SUBTITLE="$OPTARG"
    ;;
    b) BODY="$OPTARG"
    ;;
    o) OPTIONS="$OPTARG"
    ;;
    h) help
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    exit 1
    ;;
  esac
done

echo "$OPTIONS"

DEST="sm.puri.Phosh.Portal"
OBJECT_PATH="/org/freedesktop/portal/desktop"
METHOD="org.freedesktop.impl.portal.Access.AccessDialog"

gdbus call --session \
            --dest $DEST \
            --object-path $OBJECT_PATH \
            --method $METHOD \
            /sm/puri/Phosh/Access sm.puri.Phosh '' \
            "$TITLE" "$SUBTITLE" "$BODY" "$OPTIONS"
