aboutsummaryrefslogtreecommitdiff
path: root/games/dominion/showKingdom.sh
blob: e405867c5cd005ee975566e0560b004778a9bf9b (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
#!/bin/sh

# showKingdom.sh: Shows the images of a card list received as input
#
# Usage: showKingdom.sh

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

usageMessage="Usage: ${scriptName}"

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

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

outputDirBase='/tmp/showKingdom'
outputDir="${outputDirBase}/$(date '+%y%m%d%H%M%S')"
[ -d "$outputDir" ] || mkdir -p "$outputDir"

counter=1

while read -r card; do
	name="$(echo "$card" | grep -Eo '[A-Za-z][A-Za-z ]*[A-Za-z]' | sed 's/ /_/g')"

	# Avoid redownload if image exists already
	existingFile="$(find "$outputDirBase" -name "${name}.jpg" | sed 1q)"
	if [ -n "$existingFile" ]; then
		if ! cp "$existingFile" "$outputDir"; then
			# If copy fails, assume file exists and copy it with an appended counter
			cp "$existingFile" "${outputDir}/${name}${counter}.jpg"
			counter=$((counter+1))
		fi
	else
		wget "https://robinzigmond.github.io/Dominion-app/images/card_images/${name}.jpg" --directory-prefix="$outputDir"
	fi

done

sxiv -tb "$outputDir" &