Skip to content

Instantly share code, notes, and snippets.

@schinken
Created February 9, 2016 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schinken/16af477869ad9f4221ec to your computer and use it in GitHub Desktop.
Save schinken/16af477869ad9f4221ec to your computer and use it in GitHub Desktop.
#!/bin/sh
set -eu
MQTT_HOST=mqtt
VOTE_TOPIC=soundboard/vote
SOUNDFILE_DIR=/mnt/nfs/soundboard/files
VOTES_FILE=/mnt/nfs/soundboard/votes.json
THRESHOLD=-10
if [ ! -d "$SOUNDFILE_DIR" ]; then
echo "$SOUNDFILE_DIR is not a directory" >&2
exit 1
fi
if [ ! -f "$VOTES_FILE" ]; then
echo '{}' > $VOTES_FILE
fi
mosquitto_sub -h "$MQTT_HOST" -t "$VOTE_TOPIC/+" -v | while read -r event; do
topic="${event% [+-]}"
[ -z "$topic" -o "$topic" = "$event" ] && continue
sound="$(basename -- "$topic")"
vote="${event##* }"
case "$vote" in
-) diff=-1;;
+) diff=1;;
*) continue;;
esac
jq ".\"$sound\" += $diff" <"$VOTES_FILE" >"${VOTES_FILE}.new"
mv "${VOTES_FILE}.new" "$VOTES_FILE"
newval=$(jq ".\"$sound\"" <"$VOTES_FILE")
[ "$newval" -le "$THRESHOLD" ] && rm -f "$SOUNDFILE_DIR/$sound"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment