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

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

sed -e "s/_NUMHOSTS_/$NUM/" -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
