aboutsummaryrefslogtreecommitdiff
path: root/games/mtg/cardToHtml.sh
blob: c7a93dc9da5cfd8ceca69eb74b5f8bd6ef2f2d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/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 '<a href="%s">
  <img src="%s"
       alt="%s">
</a>\n' "$scryfallUrl" "$imageUrl" "$cardName"