#!/bin/sh
#
# /etc/rc.d/rc.tlp-pd
#
# Start/stop/restart/status for the TLP Profiles Daemon.

DAEMON=/usr/sbin/tlp-pd
PIDFILE=/run/tlp-pd.pid

running() {
  [ -r "$PIDFILE" ] &&
    kill -0 "$(cat "$PIDFILE")" 2>/dev/null
}

case "$1" in
  start)
    if running; then
      echo "tlp-pd is already running."
    else
      rm -f "$PIDFILE"
      echo "Starting tlp-pd..."
      "$DAEMON"
    fi
    ;;
  stop)
    if running; then
      echo "Stopping tlp-pd..."
      kill -TERM "$(cat "$PIDFILE")"
    else
      rm -f "$PIDFILE"
      echo "tlp-pd is not running."
    fi
    ;;
  restart)
    "$0" stop
    sleep 1
    "$0" start
    ;;
  status)
    if running; then
      echo "tlp-pd is running (PID $(cat "$PIDFILE"))."
    else
      echo "tlp-pd is stopped."
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac
