File: flags.sh

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (73 lines) | stat: -rwxr-xr-x 1,919 bytes parent folder | download
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
#!/usr/bin/env bash

# This script generates icons of countries flags, that are used for the language selector
# in the game options menu. It downloads the images as SVGs from a github repository,
# converts them into PNGs, and shape them into the format expected by the interface code.

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

WZ_ROOT=$(dirname "$(dirname "$SCRIPT_DIR")")

main() {
	check_dependencies

	temp_dir=$(mktemp -d)
	trap cleanup_temp_dir EXIT

	cd "$temp_dir" || exit 1

	download_images || exit 1

	log 'Rendering images...'
	for flag in BG ES CZ DK DE GR US GB ES EE ES FI FR NL IE HR HU ID IT KR LT LV NO NO NL PL BR PT RO RS RU SK SI SE TR UZ UA CN TW VA; do
		file="$flag.svg"

		if ! [ -e "$file" ]; then
			log 'Unable to find flag file "%s"' "$file"
			continue
		fi

		rsvg-convert -a -w 1000 -h 1000 "$flag.svg" -o temp.png

		gravity=Center
		# these flags have details on the left side that would be clipped out with gravity=Center
		if grep -q "$flag " <<< 'CZ GR RS SI US CN TW '; then
			gravity=West
		fi

		convert temp.png -resize 50^x45^ -gravity "$gravity" -extent 50x45 "$WZ_ROOT/data/base/images/flags/flag-$flag.png"
	done
}

check_dependencies() {
	for bin in rsvg-convert convert curl; do
		if ! [ "$(which "$bin")" ]; then
			log 'This script requires the command "%s". Please install it before continuing.' "$bin"
			exit 1
		fi
	done
}

download_images() {
	log 'Downloading images...'
	repo_owner=madebybowtie
	repo_name=FlagKit
	repo_svgs_path=Assets/SVG
	download_url="https://codeload.github.com/$repo_owner/$repo_name/tar.gz/master"

	curl "$download_url" --silent --show-error | tar -xz --xform='s#^.+/##x' "$repo_name-master/$repo_svgs_path"
}

cleanup_temp_dir() {
	if [ -d "$temp_dir" ]; then
		log 'Cleaning up temporary directory...'
		rm -rf "$temp_dir"
	fi
}

log() {
	pattern="$1"; shift
	printf "$pattern\n" "$@" 1>&2
}

main