aboutsummaryrefslogtreecommitdiff
blob: bbfbc60eacdce95f8ff30f500cd31af05d7a6ec5 (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/bash
# Copyright 2024-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
#
# populate-election.sh: Create election files based on the election-details file.
#

ELECTION=${1%/}
fullpath=$(readlink -f "./${ELECTION}")

warn() {
	echo "$@" 1>&2
}

die() {
	echo "$@" 1>&2
	exit 1
}

if [[ -z "${ELECTION}" ]]; then
	die "usage: $(basename "$0") ELECTION-NAME"
fi

details="${fullpath}"/election-details

if ! test -f "${details}" ; then
	warn "Could not find $ELECTION at ${details}: missing file - generating from template"
	mkdir -p "${ELECTION}"/
	cat election-details.template >"${ELECTION}"/election-details
	_type=${ELECTION//*-}
	_yyyymm=${ELECTION//-*}
	sed -i \
		-e "s/__TYPE__/${_type}/g" \
		-e "s/__YYYYMM__/${_yyyymm}/g" \
		-e "s/__YYYY__/${_yyyymm:0:4}/g" \
		"${ELECTION}"/election-details
fi

if ! grep -sq -x -e "name: ${ELECTION}" "${details}" ; then
	die "Could not find $ELECTION at ${details}: bad content"
fi

for f in name startDate endDate officials voters ballot url ; do
	awk -v f=$f -F ': ' '($1==f){print $2}' "${details}" |grep -E -e '.{6,}' -sq || die "$ELECTION: missing field $f in $details"
done

for f in voters ballot officials ; do
	k1=${f/:*}
	k2=${f/*:}
	d=$ELECTION/${k1}-$ELECTION
	v=$(awk -v f="$k2" -F ': ' '($1==f){print $2}' "${details}")
	if [[ "$v" =~ "https://" ]]; then
		curl --fail -Lsq -o "${d}" "$v" || warn "Could not fetch ${f} from $v"
	else
		tr -s ', ' '\n' <<<"$v" |fmt -1 |sort >"$d"
	fi
done

for f in start:startDate stop:endDate ; do
	k1=${f/:*}
	k2=${f/*:}
	d=$ELECTION/${k1}-$ELECTION
	v=$(awk -v f="$k2" -F ': ' '($1==f){print $2}' "${details}")
	date +%s -d "$v" >"${d}"
done

if [ -f "${ELECTION}/ballot-${ELECTION}" ]; then
	overlap_candidate_official=$(grep -x -f "${ELECTION}/officials-${ELECTION}"  "${ELECTION}/ballot-${ELECTION}" -o)
	if [[ -n "${overlap_candidate_official}" ]]; then
		die "ERROR: One or more candidates are also election officials: ${overlap_candidate_official}"
	fi
fi

ln -sf "../Votify.pm" "${ELECTION}/"