#!/bin/bash

PATH=@EMC2_BIN_DIR@:$PATH
REALTIME=@REALTIME@

if [ $# -eq 0 ]; then set -- -kf; fi

INTERACTIVE=0
while true; do
	case $1 in
		-I) INTERACTIVE=1; shift;;
		-U) halcmd -R; halcmd stop; halcmd unload all; $REALTIME stop; exit;;
		*) break
	esac
done

if $REALTIME status > /dev/null; then
    echo "Realtime already running.  Use 'halrun -U' to stop existing realtime session." 1>&2
    exit 1
fi

IS_HALTCL=false
case $1 in
*.tcl) IS_HALTCL=true ;;
*.hal) set -- -f $1 ;;
esac

$REALTIME start

if $IS_HALTCL; then
    haltcl $@; result=$?
else
    halcmd $@; result=$?
fi

if [ $INTERACTIVE -eq 1 ]; then halcmd -kf; fi

halcmd stop || result=$?
halcmd unload all || result=$?

$REALTIME stop || result=$?

exit $result
