#!/bin/bash

CUR_DIR=$(dirname $(readlink -f $0))

. $CUR_DIR/images_watch.sh

WPREFIX=sedov_tst_000
IMG_PATTERN="pics/dend_kepler_tst_#X##X##X##X#.png"

status=`tac ${WPREFIX}.log | grep -e "\s*@\s*0:\s*nstep\s*=" -m 1 || echo "-"`
status=`echo "$status" | sed -e 's/^[ ]\+@[ ]\+//'`
headers=`head -n 1 ${WPREFIX}.tsl`
table_data=`grep -v '^#' ${WPREFIX}.tsl`

lines=''
while read line; do
    if [ ! -z "$lines" ]; then
        lines="$lines,\n"
    fi

    lines="$lines \"$line\""
done <<< "$table_data"

images=

for file in `images_numbered_get_last "$IMG_PATTERN" 10`; do
	if [ ! -z "$images" ]; then
		images="$images,\n"
	fi

	data=`base64 -w 0 $file`
	images="$images{\n\"name\": \"dend_kepler_tst\",\n\"data\": \"$data\"\n}"
done

echo 'qcg.monitor.scheme=piernik'
echo -e "{"
echo -e "\t\"status\": \"$status\","
echo -e "\t\"headers\": \"$headers\","
echo -e "\t\"rows\": [\n$lines\n]"
if [ ! -z "$images" ]; then
	echo -e ",\n\t\"images\": [\n$images\n]"
fi
echo -e "}"
