#!/bin/bash

$XYMONHOME/bin/xymon localhost "xymondboard fields=hostname,testname,acktime,ackmsg" | \
  sed -e 's/\\n//g' | \
  while read line
do
  HOST=`echo "$line" | cut -f1 -d'|'`
  TEST=`echo "$line" | cut -f2 -d'|'`
  ACKTIME=`echo "$line" | cut -f3 -d'|'`
  ACKMESG=`echo "$line" | cut -f4 -d'|' | sed -e 's/\n/\\n/g'`

  # Got an ACK?
  if [ $ACKTIME -ne 0 ]; then
    # Convert epoch time to something readable
    ACKTIME=`echo "puts [clock format $ACKTIME]" | tclsh`

    # Somewhere to stash the messages
    NOTES=$XYMONNOTESDIR/$HOST

    if [ -f $NOTES ]; then
      # File exists - does it already contain this timestamp?
      if [ `grep -c "$ACKTIME" $NOTES` -ne 0 ]; then
        # yep, skip it
        echo got this one already
        continue
      fi
    fi

    # by now we know the file either doesn't exist or doesn't have this message
    echo "$TEST: $ACKTIME $ACKMESG" >> $NOTES
    echo "got one: $HOST / $TEST / $ACKTIME"
  fi
done
