#!/bin/sh

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

: ${butterfly_enable="NO"}
: ${butterfly_user="root"}
: ${butterfly_args="--unsecure"}
: ${butterfly_hostname="localhost"}

. /etc/rc.subr

name=butterfly
rcvar=butterfly_enable
pidfile=/var/run/${name}.pid
start_cmd="start_butterfly"

load_rc_config $name

start_butterfly() {
  echo "Starting ${name}."
  if ! [ -r %%ETCDIR%%/ssl/butterfly_${butterfly_hostname}.crt -a %%ETCDIR%%/ssl/butterfly_${butterfly_hostname}.key -a %%ETCDIR%%/ssl/butterfly_ca.crt ]; then
    %%PREFIX%%/bin/butterfly.server.py --generate-certs --host=${butterfly_hostname} &&
    echo "${name}: generated the ssl certificate in %%ETCDIR%%/ssl"
  fi
  daemon -p $pidfile -u ${butterfly_user} %%PREFIX%%/bin/butterfly.server.py ${butterfly_args} --host=${butterfly_hostname} >/var/log/${name}.log 2>&1
}

command="%%PYTHON_CMD%%"
run_rc_command "$1"
