#!/bin/bash
# bash required for printf with \xXX escaping

# USAGE: ./repost-latest-sceneresult < mqttlogfile.txt
#
# Greps latest post to scenecalib/result and re-posts it.
# Use when scenecalc and web-frontend were stopped but camera positions
# have not changed.


L="`grep '|[^|]*/scene/result|' | tail -n1`"   # from stdin
echo "$L"

TOP="${L#*|}"
TOP="${TOP%%|*}"
PAY="${L#*|*|}"

TMPF="`mktemp`" || exit 1

# Note: printf is safe because mqttlog output is strictly specified.
printf "$PAY" > "$TMPF"
mosquitto_pub -t "`printf "$TOP"`" -f "$TMPF"

rm -f "$TMPF"