#!/bin/sh -eu
# SPDX-License-Identifier: WTFPL

EX_USAGE=64


if [ -z "${FXRELAY_TOKEN-}" ]
then
	echo '$FXRELAY_TOKEN must be set' >&2
	exit 1
fi

usage () {
	cat <<- EOF
		usage: FXRELAY_TOKEN=xxx $0 ID {nothing | ads | all}
	EOF
	exit "$1"
}

if [ "${1-}" = -h ]
then
	usage 0
elif [ $# -ne 2 ]
then
	usage $EX_USAGE >&2
fi

case "$2" in
	nothing)
		json='{"enabled":true,"block_list_emails":false}'
		;;
	ads)
		json='{"enabled":true,"block_list_emails":true}'
		;;
	all)
		json='{"enabled":false,"block_list_emails":true}'
		;;
	*)
		usage $EX_USAGE >&2
		;;
esac

curl -f -S -s https://relay.firefox.com/api/v1/relayaddresses/"$1"/ \
	-X PATCH \
	-m 60 \
	-H "Authorization: Token $FXRELAY_TOKEN" \
	-H "content-type: application/json" \
	-d "$json"
