#!/bin/bash
FLAGS=-g
#FLAGS=-O2
if [ ! -f "$1" ]; then
  echo "error: must specify rooms file"
  exit 1
fi

N=0

while read HOST_LINE; do
  HOST="`echo $HOST_LINE | cut -d\| -f1`"
  MAC="`echo $HOST_LINE | cut -d\| -f2 | tr '[A-Z]' '[a-z]'`"
  IP="`echo $HOST_LINE | cut -d\| -f3`"
  NUM="`echo $HOST_LINE | cut -d\| -f4`"
  HEX="`echo "obase=16; $NUM" | bc | tr '[A-Z]' '[a-z]'`"
  >&2 echo found host $HOST mac $MAC ip $IP num $NUM hex $HEX
  echo "\"[$HEX] $HOST\","
  N=$((N+1))
done < HOSTS > acatcher.cxx.inc

sed -e "s/_NUMHOSTS_/$N/" -e '/_HOSTS_/ {' -e 'r acatcher.cxx.inc' -e 'd' -e '}' < acatcher.cxx.template > acatcher.cxx

CLANG=""
type -P "clang++" && CLANG="clang++"
type -P "clang++-3.5" && CLANG="clang++-3.5"
type -P "clang++-3.4" && CLANG="clang++-3.4"
if [ -z "$CLANG" ]; then
  echo "ERROR: could not find clang"
else
  echo "using $CLANG"
  $CLANG $FLAGS -lopencv_core -lopencv_highgui -lopencv_imgproc -o acatcher acatcher.cxx
fi
