#!/bin/sh
# 
# Victor Manuel Jaquez Leal <vjaquez@ti.com>
#
# description: starts and stops the dsp bridge
#
# usage:
#     bridge {start|stop|restart} [dspimage.dof]


# Sanity verificatiosn
[ -d /dspbridge ] || exit 0

# pull in defaults:
. /etc/default/bridge

# if an image is  specified on the command-line, override the default:
if [ -n "$2" ]; then
    BASEIMAGE=$2
else
    BASEIMAGE=$DEFAULT_BASEIMAGE
fi

[ -f $BASEIMAGE ] || exit 0

start ()
{
    cd /dspbridge
    echo -n "Loading up Bridge driver: "
    echo insmod /lib/modules/`uname -r`/kernel/drivers/dspbridge/bridgedriver.ko  $GT_STR phys_mempool_base=$PHYS_MEMPOOL_BASE phys_mempool_size=$PHYS_MEMPOOL_SIZE shm_size=$SHM_SIZE
    insmod /lib/modules/`uname -r`/kernel/drivers/dspbridge/bridgedriver.ko $GT_STR phys_mempool_base=$PHYS_MEMPOOL_BASE phys_mempool_size=$PHYS_MEMPOOL_SIZE shm_size=$SHM_SIZE > /dev/null 2>&1
    sleep 2
    /dspbridge/cexec.out -v -T $BASEIMAGE 2>&1 | logger -t dspbridge
    [ $? -eq 0 ] || exit $?
    echo "OK"
    
    # if this is done at startup (which I guess it should be), maybe we want to fork this off
    # and run it in background, so we don't add 3 seconds to boot time:
    if [ "$BASEIMAGE" = "$DEFAULT_BASEIMAGE" ]; then
        echo -n "Launching OMX Managers: "
	OMXAudioManager 2>&1 | logger -t OMXAudioManager &
	OMXPolicyManager 2>&1 | logger -t OMXPolicyManager &
	# OMXResourceManager 2>&1 | logger -t OMXResourceManager &

	echo "OK"
    fi
}

stop ()
{
    echo -n "Shutting down OpenMAX: "
    killall OMXAudioManager > /dev/null 2>&1
    killall OMXPolicyManager > /dev/null 2>&1
    # killall OMXResourceManager > /dev/null 2>&1
    echo "OK"

    echo -n "Unloading Bridge driver: "
    sleep 3
    rmmod bridgedriver > /dev/null 2>&1
    echo "OK"
}

restart ()
{
    stop
    start
}

cd /

case "$1" in
    start)
        GT_STR=""
	    start
	    ;;
    stop)
        GT_STR=""
	    stop
	    ;;
    restart)
        GT_STR=""
	    restart
	    ;;
    debug)
        GT_STR="GT_str=\"**=0123456789\""
        restart
        ;;
    *)
	echo "Usage: $0 {start|stop|restart} [dsp_image]"
	exit 1
esac

cd - > /dev/null 2>&1

exit $?
