#! /bin/sh

PATH=/usr/sbin:/sbin:/usr/bin:/bin
IFS=

cd `dirname "${0}"` || exit 1
readonly G_LOCAL_SBIN=`pwd`

run_limit() {

        ulimit -m 2097152 -n 1024
# nice limit
        nice_prefix=""
        type nice >/dev/null && nice_prefix="nice -n 19"

# taskset
        core_num="$(cat /proc/cpuinfo | grep processor | wc -l)"
        last_core="$((${core_num} - 1))"
        taskset_prefix=""
        type taskset >/dev/null
        [ $? -eq 0 -a $core_num -gt 1 ] &&
        taskset_prefix="taskset -c 1-$last_core "
        eval "$nice_prefix $taskset_prefix $@" >/dev/null
        </dev/null 2>&1
}

run()
{
    for i in 1 2 3 4 5 6 7 8 9 10
    do
        run_limit ${G_LOCAL_SBIN}/bcm-agent
        sleep 30
    done
    exit 1
}

if [ $# -eq 1 ]; then
    if [ x"${1}" = x"--run" ]; then
        run
    fi
fi

exec setsid "${0}" --run </dev/null >/dev/null 2>&1
exit 1
