#!/bin/sh

set -e

EXAMPLES_DIR="/usr/share/doc/ibsim-utils/net-examples"
NET_MAP="$EXAMPLES_DIR/net.1"
NUM_EXPECTED=3

ibsim -s -n "$NET_MAP" &
PID=$!
sleep 1

if ! ps "$PID" >/dev/null; then
	echo >&2 "Error: ibsim failed to start"
	exit 10
fi

ibsim-run /usr/sbin/ibnetdiscover

NUM=`ibsim-run /usr/sbin/ibnetdiscover -l 2>/dev/null | wc -l`
if [ "$NUM" != "$NUM_EXPECTED" ]; then
	echo >&2 "Error: ibnetdiscover does not seem to see the network. Lines seen: $NUM, expected: $NUM_EXPECTED."
	exit 20
fi
kill "$PID"
