aboutsummaryrefslogtreecommitdiff
path: root/games/dominion/showKingdom.sh
blob: dd4b3a236180d0efb7fca91a61778091265b98a2 (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
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/sh

# showKingdom.sh: Shows the images of a card list received as input
#
# Needs card images in ./local/share/dominion/cards
#
# Usage: showKingdom.sh [-oi]

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

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

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

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

dir="${HOME}/.local/share/dominion/cards"

if ! [ -d "$dir" ]; then
	error "Needed ${dir} directory not found."
	exit 1
fi

PRINT_SELECTION=''
OUTPUT_AS_IMAGE=''
TMP_IMAGE='showKingdom.jpg'

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

		'o' )
			PRINT_SELECTION='y'
			;;

		'i' )
			OUTPUT_AS_IMAGE='y'
			;;

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

	esac
done

shift $((OPTIND - 1))

if [ -n "$PRINT_SELECTION" ] && [ -n "$OUTPUT_AS_IMAGE" ]; then
	error 'Can not use visually filter cards with this configuration.'
	exit 1
fi

while read -r card; do
	name="$(echo "$card" | grep -Eo '[A-Za-z].*[A-Za-z]' | sed 's/[ \/]/_/g')"
	find "${dir}" -name "${name}.jpg"
done |
	if [ -n "$OUTPUT_AS_IMAGE" ]; then
		oldIFS="$IFS"
		IFS=$'\n'
		montage $(cat -) -tile '5x' -mode 'concatenate' -background 'black' "$TMP_IMAGE"
		magick "$TMP_IMAGE" -trim "$TMP_IMAGE"
		sxiv "$TMP_IMAGE" || termux-open "$TMP_IMAGE"
		rm "$TMP_IMAGE"
		IFS="$oldIFS"
	elif [ -n "$PRINT_SELECTION" ]; then
		sxiv -iotb | sed 's|^/.*/||;s|_| |g;s|.jpg$||'
	else
		sxiv -itb &
	fi