#!/bin/sh
### BEGIN INIT INFO
# Provides:          bumblebeed
# Required-Start:    $localfs $syslog nvidia-kernel
# Required-Stop:     $localfs $syslog nvidia-kernel
# Should-Start:      kdm gdm
# Should-Stop:       kdm gdm
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Bumblebee supporting nVidia Optimus cards
# Description:       Daemon responsible for handling power management for
#                    nVidia Optimus cards and handling optirun requests which
#                    allows the discrete card to be used
### END INIT INFO

# Bumblebee daemon handler script. Distro-independent script to start/stop
# daemon. Should be runnable in any distro but won't give any feedback.

# Avoid bashism since this script runs using /bin/sh, not /bin/bash!

NAME=bumblebeed
BIN=/usr/sbin/bumblebeed
PIDFILE=/var/run/$NAME.pid

# returns 0 if running, non-zero otherwise
status() {
	local pid
	# program is not running
	[ -s "$PIDFILE" ] || return 3

	pid="$(cat "$PIDFILE" 2>/dev/null)"

	# process is not running, pid file not properly cleared
	kill -0 "$pid" || return 1

	# process is running
	return 0
}

start() {
	# program is not installed
	[ -x "$BIN" ] || return 5

	# return if already started
	status && return 0 || true
	"$BIN" --daemon
	status && return 0 || return 1
}

stop() {
	local retries=10
	local pid="$(cat "$PIDFILE" 2>/dev/null)"

	# not running, we're done
	status || return 0

	# first ask the daemon nicely to quit
	kill "$pid" || true
	# and check whether it listened
	while [ $retries -gt 0 ]; do
		retries=$((retries - 1))

		# process has gone, return success
		status || return 0
		# wait for half a minut before polling again
		sleep .5
	done
	# failed to stop or timeout
	return 1
}

restart() {
	stop && start
}

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