#!/bin/sh

# PROVIDE: puppetdb
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Configuration settings for puppetdb in /etc/rc.conf:
#
# puppetdb_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable puppetdb
#
# puppetdb_args (str):
#   Extra arguments passed to start command
#
# puppetdb_java_home (str):
#   Set to "%%JAVA_HOME%%" by default.
#   Set the Java virtual machine to run puppetdb
#
# puppetdb_java_opts (str):
#   Set to "" by default.
#   Java VM args to use.
#
# puppetdb_user (str):
#   Set to "puppetdb" by default.
#   User to run puppetdb as.
#
# puppetdb_group (str):
#   Set to "puppetdb" by default.
#   Group for data file ownership.
#
# puppetdb_log_file (str):
#   Set to "/var/log/puppetdb/puppetdb.log" by default.
#   Log file location.
#

. /etc/rc.subr

name="puppetdb"
rcvar=puppetdb_enable

load_rc_config "${name}"

: ${puppetdb_enable="NO"}
: ${puppetdb_args="services -c %%PREFIX%%/etc/puppetdb/conf.d"}
: ${puppetdb_java_home="%%JAVA_HOME%%"}
: ${puppetdb_user="puppetdb"}
: ${puppetdb_group="puppetdb"}
: ${puppetdb_log_file="/var/log/puppetdb/puppetdb.log"}

pidfile="/var/run/puppetdb/puppetdb.pid"
command="/usr/sbin/daemon"
java_cmd="${puppetdb_java_home}/bin/java"
procname="${java_cmd}"
command_args="-p ${pidfile} ${java_cmd} -Djruby.logger.class=com.puppetlabs.jruby_utils.jruby.Slf4jLogger -Dlogappender=F1 ${puppetdb_java_opts} -Djava.security.egd=/dev/urandom -cp %%DATADIR%%/puppetdb.jar clojure.main -m puppetlabs.puppetdb.core  ${puppetdb_args} > ${puppetdb_log_file} 2>&1"


required_files="${java_cmd}"

start_precmd="puppetdb_prestart"

puppetdb_prestart() {
  if [ ! -d "/var/log/puppetdb" ]; then
    install -d -o "${puppetdb_user}" -g "${puppetdb_group}" -m 750 "/var/log/puppetdb"
  fi
  if [ ! -f "${puppetdb_log_file}" ]; then
    touch "${puppetdb_log_file}"
    chown "${puppetdb_user}:${puppetdb_group}" "${puppetdb_log_file}"
    chmod 640 "${puppetdb_log_file}"
  fi
  if [ ! -d "/var/run/puppetdb" ]; then
    install -d -o "${puppetdb_user}" -g "${puppetdb_group}" -m 750 "/var/run/puppetdb"
  fi

  chown -R "root:${puppetdb_group}" "%%PREFIX%%/etc/puppetdb/conf.d"
  chmod 0750 "%%PREFIX%%/etc/puppetdb/conf.d"

  chown "root:${puppetdb_group}" "%%PREFIX%%/etc/puppetdb/logback.xml"
  chmod 0640 "%%PREFIX%%/etc/puppetdb/logback.xml"

  if [ ! -d "/var/db/puppetdb" ]; then
    install -d -o "${puppetdb_user}" -g "${puppetdb_group}" -m 750 "/var/db/puppetdb"
  fi
  chown -R "${puppetdb_user}:${puppetdb_group}" "/var/db/puppetdb"
  chmod 0750 "/var/db/puppetdb"
}

run_rc_command "$1"
