#!/bin/bash

# timeout
# Needed as sometimes BOOG locks up and this timer is needed
# to force an exit

# arg_1 boog output netlist to check
# arg_2 short timeout: expected boog execution time
# arg_3 boog timeout; max time we wait

sleep $2
while true
do
  loop=0
  while [ "$3" -gt "$loop" ]
  do
    let "loop=loop+1"
    if [ -f "$1" ]
    then
#echo "Sleep number "$loop
      exit 0
    fi
    sleep 1
  done
  #echo "# BOOG timed out"
  kill_loop=0
  while [ "$(ps -a | grep ' boog$' | tail -1 | sed 's/^ *//' | tr -s ' ' ' ' | cut -f1 -d' ')" != "" ]
  do
    kill -9 $(ps -a | grep ' boog$' | tail -1 | sed 's/^ *//' | tr -s ' ' ' ' | cut -f1 -d' ') 2>/dev/null
    if [ "$kill_loop" -gt 10 ]
    then
      break
    fi
    let "kill_loop=kill_loop+1"
  done
  exit 1
done
