#!/bin/sh
# PROVIDE: bitcoin
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable :
# bitcoin_enable (bool):	Set to "NO" by default.
#				Set it to "YES" to enable bitcoin
# bitcoin_user (str)		Set to "bitcoin" by default.
# bitcoin_group (str)		Set to "bitcoin" by default.
# bitcoin_conf (str)		Set to "/bitcoin/bitcoin.conf" by default.
# bitcoin_data_dir (str)	Set to "/bitcoin" by default.
# bitcoin_syslog_facility(str)	Set to "local0" by default.
# bitcoin_syslog_priority(str)	Set to "info" by default.
# bitcoinlimits_enable (bool)	Set to "NO" by default.
#				Set it to "YES" to enable bitcoinlimits
# bitcoinlimits_args		Set to "-e -U ${bitcoin_user}" by default


. /etc/rc.subr

name="bitcoin"
rcvar=bitcoin_enable

start_precmd="bitcoin_precmd"
start_cmd="bitcoin_start"
restart_precmd="bitcoin_checkconfig"
reload_precmd="bitcoin_checkconfig"
configtest_cmd="bitcoin_checkconfig"
status_cmd="bitcoin_status"
stop_cmd="bitcoin_stop"
stop_postcmd="bitcoin_wait"
command="/usr/local/bin/bitcoind"
daemon_command="/usr/sbin/daemon"
extra_commands="configtest"
pidfile="/bitcoin/bitcoind.pid"

: ${bitcoin_enable:="NO"}
: ${bitcoinlimits_enable:="NO"}

load_rc_config ${name}

: ${bitcoin_user:="bitcoin"}
: ${bitcoin_group:="bitcoin"}
: ${bitcoin_data_dir:="/bitcoin"}
: ${bitcoin_config_file:="/bitcoin/bitcoin.conf"}
: ${bitcoin_syslog_facility:="local0"}
: ${bitcoin_syslog_priority:="info"}
: ${bitcoin_syslog_tag:="bitcoin"}
: ${bitcoin_kill_after:="300"}
: ${bitcoinlimits_args:="-e -U ${bitcoin_user}"}

# set up dependant variables
procname="${command}"
required_files="${bitcoin_config_file}"
pidfile="${bitcoin_data_dir}/bitcoind.pid"

bitcoin_checkconfig()
{
  echo "Performing sanity check on bitcoin configuration:"
  if [ ! -d "${bitcoin_data_dir}" ]
  then
    echo "Missing data directory: ${bitcoin_data_dir}"
    exit 1
  fi
  chown -R "${bitcoin_user}:${bitcoin_group}" "${bitcoin_data_dir}"

  if [ ! -f "${bitcoin_config_file}" ]
  then
    echo "Missing configuration file: ${bitcoin_config_file}"
    exit 1
  fi
  if [ ! -x "${command}" ]
  then
    echo "Missing executable: ${command}"
    exit 1
  fi
  return 0
}

bitcoin_cleanup()
{
  rm -f "${pidfile}"
}

bitcoin_precmd()
{
  bitcoin_checkconfig

  pid=$(check_pidfile "${pidfile}" "${procname}")
  if [ -z "${pid}" ]
  then
    echo "Bitcoind is not running"
    rm -f "${pidfile}"
  fi

  if checkyesno bitcoinlimits_enable
  then
    eval $(/usr/bin/limits ${bitcoinlimits_args}) 2>/dev/null
  else
    return 0
  fi
}

bitcoin_status()
{
  local pid
  pid=$(check_pidfile "${pidfile}" "${procname}")
  if [ -z "${pid}" ]
  then
    echo "Bitcoind is not running"
    return 1
  else
    echo "Bitcoind running, pid: ${pid}"
  fi
}

bitcoin_start()
{
  echo "Starting bitcoin:"
  cd "${bitcoin_data_dir}" || return 1
  ${daemon_command} \
    -u "${bitcoin_user}" \
    -l "${bitcoin_syslog_facility}" \
    -s "${bitcoin_syslog_priority}" \
    -T "${bitcoin_syslog_tag}" \
    ${command} \
    -printtoconsole=1 \
    -conf="${bitcoin_config_file}" \
    -datadir="${bitcoin_data_dir}"
}

bitcoin_stop()
{
  echo "Stopping bitcoin:"
  pid=$(check_pidfile "${pidfile}" "${procname}")
  if [ -z "${pid}" ]
  then
    echo "Bitcoind is not running"
    return 1
  else
    kill ${pid}
  fi
}

bitcoin_wait()
{
  local n="${bitcoin_kill_after}"
  echo "Waiting for bitcoin shutdown:"
  while :
  do
    printf '.'
    pid=$(check_pidfile "${pidfile}" "${procname}")
    if [ -z "${pid}" ]
    then
      printf '\n'
      break
    fi
    sleep 1
    n=$((${n} - 1))
    if [ ${n} -eq 0 -a -f "${pidfile}" ]
    then
      printf "\nForce shutdown"
      kill -9 $(cat "${pidfile}")
      for n in 1 2 3
      do
        printf '.'
        sleep 1
      done
      printf '\n'
      break
    fi
  done
  rm -f "${pidfile}"
  echo "Shutdown complete"
}

run_rc_command "$1"
