aboutsummaryrefslogtreecommitdiff
path: root/games/mtg/deckLister.sh
blob: 606ca734ebe574d94d2d4adad38fa6d10901c360 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh

# deckLister.sh: Interactively reads card names in any language, searches for
# them and writes them in English line by line.
#
# Usage: deckLister.sh [-f FILE]

scriptName="${0##*/}"

usageMessage="Usage: ${scriptName} [-f FILE]"

error() {
	printf '%s error: %s\n' "$scriptName" "$*" >&2
}

errorAndUsage() {
	printf '%s error: %s\n%s\n' "$scriptName" "$*" "$usageMessage" >&2
}

file=''
welcomeMessage="${scriptName}: Input non-ambiguous card names in any language."
prompt='> '

# Process options
while getopts ':f:' opt; do
	case $opt in

		'f' )
			file="$OPTARG"
			;;

		'?' )
			printf '%s\n' "$usageMessage" >&2
			exit 1

	esac
done
shift $((OPTIND - 1))

[ -n "$1" ] && errorAndUsage 'Too many arguments.' && exit 1

log() {
	if [ -n "$file" ]; then
		printf '%s\n' "$@" >> "$file"
	else
		printf '%s\n' "$@"
	fi
}

nameToEnglish() {
	json="$(getScryfallJSON.sh "$@")" || return 1
	echo "$json" | jq -r .name
}

echo "$welcomeMessage" >&2

while true; do
	printf '%s' "$prompt" >&2
	read -r cardname
	englishName="$(nameToEnglish "$cardname")"
	[ -n "$englishName" ] && log "$englishName"
done