#!/bin/sh
# cardToHtml.sh: Generates an image HTML tag of an MTG card, with the name of
# the card as alternate text and the image being a link to its Scryfall page.
#
# Usage: cardToHtml.sh CARDNAME
#
# Exotic dependencies: jq
usageMessage="Usage: ${0} CARDNAME"
error() {
printf '%s error: %s\n' "$0" "$*" >&2
}
errorAndUsage() {
printf '%s error: %s\n%s\n' "$0" "$*" "$usageMessage" >&2
}
# Process options
while getopts ':' opt; do
case $opt in
'?' )
printf '%s\n' "$usageMessage" >&2
exit 1
esac
done
shift $((OPTIND - 1))
json="$(getScryfallJSON.sh "$@")"
[ -z "$json" ] && exit 1
cardName="$(echo "$json" | jq -r '.name')"
imageUrl="$(echo "$json" | jq -r '.image_uris.large' | sed 's/?.*$//')"
scryfallUrl="$(echo "$json" | jq -r '.scryfall_uri' | sed 's/?.*$//')"
printf '
\n' "$scryfallUrl" "$imageUrl" "$cardName"