#!/bin/sh

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')]>}"

if [ "$1" = "-h" ]; then
    echo Test helper to show Access dialogs of the org.freedesktop.portal api
    echo
    echo Arguments
    echo
    echo "  -h Shows help"
    echo "  -t <String>: The title of the dialog"
    echo "      Example: $TITLE"
    echo "  -s <String>: The subtitle of the dialog"
    echo "      Example: $SUBTITLE"
    echo "  -b <String>: The body of the dialog"
    echo "      Example: $BODY"
    echo "  -o <String>: The options of the dialog (https://flatpak.github.io/xdg-desktop-portal/#gdbus-org.freedesktop.impl.portal.Access)"
    echo "      Example: $OPTIONS"
    echo
    return
fi

while getopts ":t:s:b:o:" opt; do
  case $opt in
    t) TITLE="$OPTARG"
    ;;
    s) SUBTITLE="$OPTARG"
    ;;
    b) BODY="$OPTARG"
    ;;
    o) OPTIONS="$OPTARG"
    ;;
    \?) 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"
