#!/usr/bin/ruby
# This file is a runner for ruby part of pcsd callable from a systemd unit.
# It also serves as a holder of a selinux context.

begin
  # add pcsd to the load path (ruby -I)
  libdir = File.dirname(__FILE__)
  $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)

  # change current directory (ruby -C)
  Dir.chdir('/var/lib/pcsd')

  # import and run ruby daemon
  require 'rserver.rb'
rescue SignalException => e
  if [Signal.list['INT'], Signal.list['TERM']].include?(e.signo)
    # gracefully exit on SIGINT and SIGTERM
    # pcsd sets up signal handlers later, this catches exceptions which occur
    # by receiving signals before the handlers have been set up.
    exit
  else
    raise
  end
end
