#!/bin/sh
### BEGIN INIT INFO
# Provides:          falcon-sensor
# Required-Start:    $local_fs $syslog
# Should-Start:      $network
# Required-Stop:     $local_fs $syslog
# Should-Stop:       $network 
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: falcon-sensor daemon
# Description:       Loads the falcon-sensor kernel modules and starts the user
#                    space daemon.
### END INIT INFO

set -e

falcond="/opt/CrowdStrike/falcond"

# Check for daemon presence
[ -x "$falcond" ] || exit 0

# Get lsb functions
. /lib/lsb/init-functions

case "$1" in
  start)
    if ! /opt/CrowdStrike/falconctl -g --cid > /dev/null; then
        exit 6
    fi
    log_begin_msg "Starting falcon-sensor..."
    start-stop-daemon --start --quiet --oknodo --exec "$falcond"
    log_end_msg $?
    ;;
  stop)
    log_begin_msg "Stopping falcon-sensor..."
    start-stop-daemon --stop --quiet --oknodo --retry 60 --name falcond
    log_end_msg $?
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
    status_of_proc "$falcond" falcon-sensor
    ;;
  *)
    log_success_msg "Usage: /etc/init.d/falcon-sensor {start|stop|restart|status}"
    exit 1
esac

