#!/bin/sh

### BEGIN INIT INFO
# Provides:          ubiflasher
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts ubiflasher as a daemon
# Description:       Starts ubiflasher as a daemon
### END INIT INFO

# Put this script as e.g: /etc/init.d/ubiflasher
# Edit the variables below:

UBIFLASHERPATH="/opt/ubi-flasher"
FIRMWARE="/home/foo/sudowrt/sudowrt.bin"

echo $UBIFLASHERPATH

start()
{
  echo "Starting ubi-flasher daemon"
  start-stop-daemon --start --pidfile /var/run/ubiflasher.pid --chdir ${UBIFLASHERPATH} --background --make-pidfile -a ${UBIFLASHERPATH}/flasher.js -- --retryonfail 5 --retryonsuccess 20 --firmware $FIRMWARE
}

stop()
{
  echo "Stopping ubi-flasher daemon"
  start-stop-daemon --stop --pidfile /var/run/ubiflasher.pid
}

restart()
{
  stop
  start
}

case "$1" in
    start)
        start
    ;;

    stop)
        stop
    ;;

    restart)
        restart
    ;;

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

exit 0