#!/bin/sh

# PROVIDE: jackd
# REQUIRE: LOGIN
#
# Add the following line to /etc/rc.conf to enable jackd:
#
# jackd_enable="YES"
#

: ${jackd_enable="NO"}
: ${jackd_user=""}
: ${jackd_args="--no-realtime -doss -r48000 -p1024 -w32"}

. /etc/rc.subr

name=jackd
rcvar=jackd_enable
procname=%%PREFIX%%/bin/jackd
start_cmd="start_jackd"

fail() {
  echo "JACK failed to start: $1" >&2
  exit 1
}

check_config() {
  # check that jackd_user is set
  if [ -z "$jackd_user" ]; then
    fail "jackd_user has to be defined"
  fi

  # detect intent to use realtime priority
  if { echo "$jackd_args" | grep -q -- "oss" && \
       { echo "$jackd_args" | grep -qw -- "--realtime"  || \
         echo "$jackd_args" | grep -qw -- "-[A-Za-mo-z]*R[A-Za-z]*"; }; }; then
    # check that mac_priority(4) is enabled
    if [ "$(sysctl -qn security.mac.priority.realtime)" != "1" ]; then
      fail "Realtime priority requires mac_priority(4) to be loaded"
    fi
    # check that the user has realtime privileges
    if ! (su -m "$jackd_user" -c "rtprio 10 test -z"); then
      fail "User jackd_user=$jackd_user is not a member of the realtime group"
    fi
  fi
}

start_jackd() {
  echo "Starting ${name}."

  # check that setup is valid
  check_config

  # log the date and parameters
  echo -e "\n[`date`] Starting the daemon, user=$jackd_user args=\"$jackd_args\"" >> /var/log/${name}.log

  # start the daemon
  daemon -p /var/run/${name}.pid -u "$jackd_user" %%PREFIX%%/bin/jackd $jackd_args
}

load_rc_config ${name}
run_rc_command "$1"
