#!/bin/sh
#
# $NetBSD: syncthing.sh,v 1.3 2022/05/18 20:45:36 triaxx Exp $
#
# PROVIDE: syncthing
# REQUIRE: DAEMON
#
# Consider installing pkgtools/rc.subr in unprivileged.
#
# You will need to set some variables in /etc/rc.conf to start syncthing:
#
# syncthing=YES

$_rc_subr_loaded . /etc/rc.subr

name="syncthing"
rcvar=${name}

load_rc_config $name

: ${syncthing_user:=syncthing}
: ${syncthing_user_home:=/var/db/syncthing}
: ${syncthing_group:=syncthing}
: ${syncthing_home:=/usr/pkg/etc/syncthing}
: ${syncthing_logfile:=/var/log/syncthing.log}

command="/usr/pkg/bin/syncthing"
command_args="-logfile ${syncthing_logfile}"
command_args="${command_args} -home ${syncthing_home} > /dev/null &"

syncthing_env="STNODEFAULTFOLDER=1"
syncthing_env="${syncthing_env} USER=${syncthing_user}"
syncthing_env="${syncthing_env} HOME=${syncthing_user_home}"

start_precmd="syncthing_precmd"

syncthing_precmd()
{
	/usr/bin/touch ${syncthing_logfile} && \
	/usr/sbin/chown ${syncthing_user}:${syncthing_group} ${syncthing_logfile} && \
	/bin/chmod 0750 ${syncthing_logfile}
}

run_rc_command "$1"
