#!/usr/bin/openrc-run
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

SYNCTHING_USER="${SYNCTHING_USER:-root}"
SYNCTHING_GROUP="${SYNCTHING_GROUP:-root}"
SYNCTHING_HOMEDIR="${SYNCTHING_HOMEDIR:-/var/lib/syncthing/.config/syncthing}"
SYNCTHING_LOGDIR="${SYNCTHING_LOGDIR:-/var/log/syncthing}"
SYNCTHING_LOGFILE="${SYNCTHING_LOGFILE:-${SYNCTHING_LOGDIR}/syncthing.log}"
SYNCTHING_UMASK="${SYNCTHING_UMASK:-007}"
SYNCTHING_IONICE="${SYNCTHING_IONICE:-0}"
SYNCTHING_NICE="${SYNCTHING_NICE:-0}"

description="Syncthing is an open, trustworthy and decentralized cloud storage system"
pidfile="/run/syncthing.pid"
supervisor=supervise-daemon
command="/usr/bin/syncthing"
command_args="-no-browser -home=${SYNCTHING_HOMEDIR} ${SYNCTHING_OPTS}"
command_user="${SYNCTHING_USER}:${SYNCTHING_GROUP}"
output_log="$SYNCTHING_LOGFILE"
error_log="$output_log"

supervisor_daemon_args=" --umask ${SYNCTHING_UMASK} \
  --ionice ${SYNCTHING_IONICE} \
  --nicelevel ${SYNCTHING_NICE}"

depend() {
  need localmount net
}

start_pre() {
  if [ ! -d "$SYNCTHING_HOMEDIR" ]; then
  	install -d "$SYNCTHING_HOMEDIR"
	chown "$command_user" "$SYNCTHING_HOMEDIR"
  fi

  checkpath -d -o "$command_user" "$SYNCTHING_LOGDIR"
  checkpath -f -o "$command_user" "$SYNCTHING_LOGFILE"
}
