#!/bin/bash
##
## Allow providing node binary in the path of the installation
##

## Default to installed os node
NODE="/usr/bin/env node"

## Resolve the real outpost agent script directory (following symlinks) [where this bash script actually is]
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

## Traverse directory tree looking for node binary file
NODEDIR=$SCRIPTDIR
while [ "/" !=  "$NODEDIR" ]; do

    if [ ! -f "$NODEDIR/node" ]; then
        NODEDIR=`dirname $NODEDIR`
    else
        NODE=$NODEDIR/node
        break
    fi
done

$NODE $SCRIPTDIR/agent "$@"
