vern-scripts/pubnixvm/poll-votes

83 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -x
# Config
home_dir="/home/"
voting_questions_file="/var/voting_prompts"
# setup
set -e
home_dir=${home_dir%/}
# read active prompts
declare -A active_prompts
while IFS=, read q_id q_prompt
do
active_prompts[$q_id]=$q_prompt
done < "$voting_questions_file"
# intialize vote counter
declare -A vote_counter
declare -A vote_counter_total
for q_id in ${!active_prompts[@]}
do
declare -i vote_counter[$q_id]=0
declare -i vote_counter_total[$q_id]=0
done < "$voting_questions_file"
# intialize voter missing list
declare -A voter_missing
parse_user_votes() {
local user_home=$1
local user_voting_file=$user_home/.vote
local user_name=${user_home##*/}
local -A user_votes
if ! [[ -f "$user_voting_file" ]]; then
echo "WARNING: user "${user_name}" doesn't have a ~/.vote" >& 2
return
fi
while IFS=, read q_id q_bool
do
user_votes[$q_id]=$q_bool
done < "$user_voting_file"
for q_id in ${!active_prompts[@]}
do
user_vote=${user_votes[$q_id]}
case $user_vote in
1)
vote_counter[$q_id]+=1
vote_counter_total[$q_id]+=1
;;
0)
vote_counter_total[$q_id]+=1
;;
*)
voter_missing[$q_id]+="${user_name},"
;;
esac
done
}
for user_home in "$home_dir"/*
do
parse_user_votes "$user_home"
done
for q_id in $(printf '%s\n' "${!active_prompts[@]}" | tac | tr '\n' ' ')
do
echo "${q_id}|${active_prompts[$q_id]}|${vote_counter[$q_id]}|${vote_counter_total[$q_id]}|${voter_missing[$q_id]%,}"
done