#!/bin/sh
### BEGIN INIT INFO
# Provides:          platform-util
# Required-Start:    kmod bootmisc
# Required-Stop:
# Should-Start:      checkroot
# Should-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Load the Broadcam platform-util.
# Description:       Load the Broadcam platform-util.
### END INIT INFO

if [ -e /sys/class/net/sys0 ]; then
  ifconfig sys0 up
fi
case "$1" in
  start)
    # first boot?
    [ -f /etc/dropbear/dropbear_rsa_host_key ]
    echo "start and init. platform..."
	extra_module_path=/lib/modules/4.1.20/extra

	# brcmv3d.ko has to be first as nexus.ko resolves a weak symbol in it on load
	if [ -e $extra_module_path/brcmv3d.ko ] ; then
		echo "loading v3d..."
		insmod $extra_module_path/brcmv3d.ko
	fi

	# load api module
	echo "loading api module..."
	insmod $extra_module_path/nexus.ko

	if [ -e $extra_module_path/wakeup_drv.ko ] ; then
		echo "loading wakeup module..."
		insmod $extra_module_path/wakeup_drv.ko
	fi
 	if [ ! -e /dev/wake0 ] ; then
 		mknod -m a=rw /dev/wake0 c 34 0
	fi
	
	# start nexus server
	echo "run api server..."
	start-stop-daemon --start --exec /usr/bin/nxserver -- &

  	if [ ! -e /dev/brcm0 ] ; then
 		mknod -m a=rw /dev/brcm0 c 30 0
   	fi

	sleep 10

	# load vuplus init_client
	# start-stop-daemon --start --exec /usr/bin/init_client -- &
		
	# load proprietary module
	echo "loading proprietary module..."	
	#insmod $extra_module_path/bcmlinuxdvb.ko _hwtype=336
	start-stop-daemon --start --exec /usr/bin/init_driver $extra_module_path/bcmlinuxdvb.ko _hwtype=336 -- &
	#insmod $extra_module_path/bcmlinuxdvb.ko _hwtype=336 

    echo "(___)"
    ;;
  stop)
    ;;
  restart)
    ;;
  *)
    echo "Usage: /etc/init.d/vuplus-platform-util {start|stop|restart}"
    exit 1
esac

exit 0
