#!/bin/sh
### BEGIN INIT INFO
# Provides:          cpuminer-opt
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: cpuminer-opt
# Description:       Optimized multi algo CPU crypto-money miner
### END INIT INFO


# UNTESTED, should trigger systemctl calls anyway.
NAME=cpuminer-opt
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
DESC="Optimized multi algo CPU crypto-money miner"

[ -r /etc/default/$NAME ] && . /etc/default/$NAME

test "$ENABLED" != "false" || exit 0

. /lib/lsb/init-functions

do_start() {
    
    [ "$VERBOSE" != no ] && log_daemon_msg "$NAME will be built it not existing yet, can take up to a few minutes"
    cpuminer-opt --version >/dev/null 2>&1
    if [ ! $? -eq 0 ]; then
        echo "cpuminer-opt failed to build."
        echo "Please run "cpuminer-opt" for additional debugging information"
        exit 1
    fi

    start-stop-daemon --start \
                      --exec /usr/bin/cpuminer-opt \
                      --chuid nobody:nogroup \
                      --background \
                      --pidfile $PIDFILE \
                      --make-pidfile -- $OPTIONS
}

do_stop() {
    start-stop-daemon --stop --pidfile $PIDFILE
}


case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  restart|force-reload)
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
		# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
