summaryrefslogtreecommitdiffstats
path: root/mkballots.sh
blob: c4730c78ad7b1b13e4049927463b4a93e832d3e7 (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
75
76
#!/bin/bash -x
export PATH=$(realpath -s $(dirname "$0"))/bin:"$PATH"
if [ -z "$1" -o -z "$2" ]; then
    set +x
    echo "Usage: $0 project numballots [batches [scoresheets]]" 1>&2
    exit 1
fi
nbatch="${3:-1}"
perbatch="${2:-100}"
scoresheets="${4:-1}"
proj="${1:-test}"
declare -A sub
. "${proj}.conf"
paper="${paper:-letter}"
sub[PAPER]="${paper}"
writeins="${writeins:-2}"
template="${template:-$proj.tex}"
if [ ! -f "$template" ]; then
    set +x
    echo "$0: template file $template missing!" 1>&2
    exit 1
fi
shuf='shuf --random-source=/dev/urandom'
sdaps="sdaps $proj"
rm -rf "$proj"
rm -f "$proj".sub.sed
for s in "${!sub[@]}"; do
    echo "s/%%${s//\//\\\/}%%/${sub[$s]//\//\\\/}/g" >> "$proj".sub.sed
done
cat >> "$proj".sub.sed <<EOF
t
s:%%WRITEINS%%::
t writeins
s:%%CANDIDATES%%:$shuf < '$proj'.cand:e
T
s/^\\(.*\\)\$/\\\\choiceline{\\1}/mg
b
: writeins
EOF
# Note: n-1 p commands as sed itself will print one
for (( x=1 ; x < writeins ; x=x+1 )); do
    echo 'p' >> "$proj".sub.sed
done
sed -f "$proj".sub.sed < "$template" > "$proj".pp.tex
$sdaps setup_tex "$proj".pp.tex
rm "$proj".sub.sed "$proj".pp.tex
if [ ! -f "$proj"/questionnaire.pdf ]; then
    set +x
    echo "$0: project $proj failed to compile!" 1>&2
    exit 1
fi
# Produce a tiebreakers file for csvconv.pl so we can reproduce the output
seq 1 $(wc -l < "$proj".cand) | $shuf | tr '\n' ',' | \
    sed -e 's/,$/\n/' > "$proj"/tiebreakers.csv
# Generate score sheets (one PDF with blank ID, one with individual numbers)
scoresheet.pl "$proj" "$paper" > "$proj"/scoresheet.tex
genids.pl "$prefix"/MT $scoresheets 1 > "$proj"/scoresheet.ids
scoresheet.pl "$proj" "$paper" "$proj"/scoresheet.ids > "$proj"/scoresheets.tex
cd "$proj"
pdflatex scoresheet.tex && pdflatex scoresheet.tex
pdfmerge questionnaire.pdf scoresheet.pdf chkalign.pdf
pdflatex scoresheets.tex && pdflatex scoresheets.tex
cd ..
bdigits=$(echo -n $((nbatch)) | wc -c)
start=1
genids.pl "$prefix" $((nbatch*perbatch)) $start | $shuf | \
    split -a $bdigits --numeric-suffixes=1 -l $perbatch \
	  --additional-suffix='.list' - "$proj"/id

for (( batch=1 ; batch <= nbatch ; batch++ )); do
    bname=$(printf "%0${bdigits}d" $batch)
    $sdaps stamp -f "$proj"/id${bname}.list -o "$proj"/ballots_${bname}.pdf
done
# Make a backup copy of the project directory
tar cvvfJ "$proj".sdaps.tar.xz "$proj" "$template" \
    "$proj".cand "$proj".conf mkballots.sh