#!/bin/bash
#
# dont dlelet next line
export MIVO400_MLX_VERSION_2="mbc"
# # must be the same version as in <ws>/pbx/refsys/modules/linuxBashscripts/upgrade.bash
#
# Startup script for mlx.
#
# chkconfig: 4 20 80
# #1: start for runlevel 2, 3 and 4
# #2: start script number/priority
# #3: kill  script number/priority
# !!: start priority + kill priority = 100 !
#
# 1. copy script "mlx" to /etc/rc.d/init.d
# 2. chmod +x mlx
# 3. chkconfig --add mlx
#    (chkconfig --del mlx to remove the configuration)
#
# chkconfig
# mlx  0:off  1:off  2:off  3:off  4:on  5:off  6:off
#


start() {
        logger -t MLX Start mlx
        # Install a ramdisk after reboot.
        logger -t MLX "Installing ramdisk after reboot"
        /usr/local/bin/mivo400_start_ramdisk.sh
	# Check whether we received the 1st start command from U-Boot
        /usr/local/bin/mivo400_firststart.sh
	# Start tftp access
	sed -i 's/RUN_DAEMON="no"/RUN_DAEMON="yes"/' /etc/default/tftpd-hpa
	sed -i 's/\/var\/lib\/tftpboot/\/home\/mivo400\/tftp/' /etc/default/tftpd-hpa
	# /etc/default/tftpd-hpa is now configured to start the tftp daemon
	# RUN_DAEMON="yes"
	# OPTIONS="-l -s /home/mivo400/tftp"
	/etc/init.d/tftpd-hpa start
	#
	THE_NEXTTASK="/home/mivo400/sw/addon/scripts/mlxStart.bash"
	if [ -x ${THE_NEXTTASK} ];
	then
	  logger -t MLX "Call ${THE_NEXTTASK}"
	  ${THE_NEXTTASK}
	fi
	#
        logger -t MLX "Start mlx daemon under runit supervision"
        # Start the mlx daemon under runit supervision
        touch /var/lock/subsys/mlx
        sv start mlxd
}
stop() {
        logger -t MLX "Shutting down mlx"
        # Stop the mlx daemon under runit supervision
        rm -rf /var/lock/subsys/mlx
        sv stop mlxd
	# Stop tftp access
	/etc/init.d/tftpd-hpa stop
	sed -i 's/RUN_DAEMON="yes"/RUN_DAEMON="no"/' /etc/default/tftpd-hpa
	sed -i 's/\/home\/mivo400\/tftp/\/var\/lib\/tftpboot/' /etc/default/tftpd-hpa
	# /etc/default/tftpd-hpa is now configured to default directory and not start the daemon on start up
	# RUN_DAEMON="no"
	# OPTIONS="-l -s /var/lib/tftpboot"
        sleep 5
	THE_NEXTTASK="/home/mivo400/sw/addon/scripts/mlxStop.bash"
	if [ -x ${THE_NEXTTASK} ];
	then
	  logger -t MLX "Call ${THE_NEXTTASK}"
	  ${THE_NEXTTASK} 
	fi
}
restart() {
        logger -t MLXD Restart mlx
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 3
esac

exit $?

