#!/bin/bash

TEMPLATE=$1
if [ -z "$TEMPLATE" ]; then
  echo need template
  exit 1
fi

i=0
for stat in statistics.*; do
  date=`tail -1 $stat | cut -d" " -f1`
  echo $date $stat
done | grep -v NMEA | sort -n | while read date file; do
  title=`grep "^$file " "${TITLES}" 2> /dev/null | cut -d" " -f2`
  if [ -z "$title" ]; then
    title=${file#statistics.}
  fi
  if [ $i != 0 ]; then
    echo -n ,
  else
    i=1
  fi
  echo $TEMPLATE | sed -e "s/__FILE__/$file/g" -e "s/__TITLE__/$title/g"
done
