#!/bin/sh
# Copyright (c) 2013 National Instruments.
# All rights reserved.

# Set stack size, OOM killer parameters, and environment, then
# start lvrt.

# save current lvrt_wrapper PID to $PID_LVRT_WRAPPER
echo $$ > $2

# have to special-case serial# since that's not a valid identifier
export serialnum=`/sbin/fw_printenv -n serial#`
OLDIFS=$IFS
IFS='
'
export eval `/sbin/fw_printenv DeviceCode DeviceDesc TargetClass`
IFS=$OLDIFS
export nisafemodeversion=`/usr/local/natinst/bin/nisafemodeversion`

# stack size
ulimit -s 256

# core file size
# uncomment to enable core dumps
#ulimit -c unlimited

umask u=rwx,g=rwx,o=rx
echo 1000 > /proc/self/oom_score_adj
cd /usr/local/natinst/labview

# save $STARTUP_PARAM to environment variable LVRT_STATUS
export LVRT_STATUS=$1
# this variable is checked by code in:
### //labview/branches/{version}/dev/source/appcore/AppMagic.cpp and
### //labview/branches/{version}/dev/source/server/protocolerr.cpp

ARGS=
if [ "$3" == "true" ]; then
  ARGS=--ni-enable-ui
  [ "$DISPLAY" ] || export DISPLAY=:0
fi

exec ./lvrt $ARGS
