#!/bin/sh

# PROVIDE: pcdm
# REQUIRE: LOGIN cleanvar dbus
# KEYWORD: shutdown
# Add the following to /etc/rc.conf to start PCDM at boot time:
#
# pcdm_enable="YES"
#

. /etc/rc.subr

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

name="pcdm"
rcvar=pcdm_enable
command="/usr/local/sbin/PCDMd"
pidfile="/var/run/PCDMd.pid"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"

load_rc_config ${name}
: ${pcdm_enable:=no} #default to not start pcdm automatically

pcdm_status(){
  #returns 0 if running, 1 if not
  if [ ! -f ${pidfile} ]; then
    return 1
  elif [ `pgrep -q -F ${pidfile}` ]; then
    return 1
  fi
  return 0
}

pcdm_start()
{
  if ! checkyesno pcdm_enable ; then
     return 1
  fi

  # Check if boot-loader set option to skip xorg
  if [ "`kenv noxorg 2>/dev/null`" = "YES" ] ; then
     return 0
  fi
  #check if PCDMd is already running
  pcdm_status
  if [ 0 -eq $? ]; then
    return 0
  fi
  echo "Starting PCDM."
  #Start the PCDM login daemon
  if [ ! -e /var/run/nologin ]; then
    /usr/sbin/daemon -S -T pcdm ${command} ${pcdm_flags} 
  fi
}

pcdm_stop()
{
  #Send the PCDM daemon the signal not to start a new session after this one finishes
  ${command} stop
}


run_rc_command "$1"
