#!/bin/bash
#
# This script is meant to be launched from inside an OpenViBE application (such as a scenario in Designer)
#

CALLCMD=$1
shift

ov_run_bg=0
if [ "$1" == "--run-bg" ]; then
	ov_run_bg=1
	shift
fi

if [ "$OV_PATH_ROOT" == "" ]; then
	SOURCE="${BASH_SOURCE[0]}"
	while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
	  OV_PATH_ROOT="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
	  SOURCE="$(readlink "$SOURCE")"
	  [[ $SOURCE != /* ]] && SOURCE="$OV_PATH_ROOT/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
	done
	OV_PATH_ROOT="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
fi
if [ "$OV_PATH_BIN" == "" ]; then
	OV_PATH_BIN="$OV_PATH_ROOT/bin"
fi


ret_code=0

if [ $ov_run_bg == 0 ]; then
	eval "$OV_PATH_BIN/$CALLCMD" @OV_CMD_ARGS@ $*
	ret_code=$?
else
	"$OV_PATH_BIN/$CALLCMD" @OV_CMD_ARGS@ $* &
fi

exit $ret_code