#!/bin/sh
# this script sets LD_LIBRARY_PATH environment variable
# and checks consistence of the system configuration

LD_LIBRARY_PATH=.:${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH
PATH=.:${PATH}
export PATH

if [ $UID -ne "0" ]; then
	echo "You need to run $@ as root."
	exit 1
fi

TEST=`lsmod|grep bcmdriver >/dev/null && echo 1`
if [ ! -z $TEST ]; then
    rmmod bcmdriver || exit;
fi
TEST=`lsmod|grep nexus >/dev/null && echo 1`
if [ ! -z $TEST ]; then
    rmmod nexus || exit;
fi
TEST=`lsmod|grep wakeup_drv >/dev/null && echo 1`
if [ ! -z $TEST ]; then
    rmmod wakeup_drv || exit;
fi
TEST=`lsmod|grep brcmv3d >/dev/null && echo 1`
if [ ! -z $TEST ]; then
    rmmod brcmv3d || exit;
fi

# because msg_modules is so common, automatically tunnel it through config
if [[ -z ${config} && ! -z ${msg_modules} ]]; then config="msg_modules=${msg_modules}"; fi

# brcmv3d.ko has to be first as nexus.ko resolves a weak symbol in it on load
if [ -e brcmv3d.ko ]; then
insmod brcmv3d.ko
fi

insmod nexus.ko config=\"${config}\" || exit;

if [ -e wakeup_drv.ko ]; then
insmod wakeup_drv.ko
fi
if [ ! -e /dev/wake0 ]; then
mknod -m a=rw /dev/wake0 c 34 0
fi

if [ -e bcm_astra.ko ]; then
insmod bcm_astra.ko
fi

# Start whatever application is requested
target=$1;
shift
exec env ${config} ${target} "$@"
