#!/sbin/openrc-run
# Copyright 2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

name="Klipper daemon"
description="3D printer firmware with motion planning on the host"

pidfile=${pidfile:-"/run/${RC_SVCNAME}.pid"}
user=${user:-${RC_SVCNAME}}
group=${group:-${RC_SVCNAME}}

: ${klipper_config:=${KLIPPER_CONFIG:-/etc/klipper/printer.cfg}}
: ${klipper_socket:=${KLIPPER_SOCKET:-/run/${RC_SVCNAME}/${RC_SVCNAME}.sock}}
: ${klipper_log:=${KLIPPER_LOG:-/tmp/klippy.log}}

command="/usr/libexec/${RC_SVCNAME}/klippy/klippy.py"
command_args="${command_args:-${klipper_config} --api-server=${klipper_socket} --logfile=${klipper_log}}"
command_background="true"

retry="TERM/25/KILL/5"

start_pre() {
	# We need to test if runtime directories exists, since script will fail if it does not
	checkpath --owner ${RC_SVCNAME}:${RC_SVCNAME} --mode 0775 -q -d "${klipper_config%\/*}"
 	checkpath --owner ${RC_SVCNAME}:${RC_SVCNAME} --mode 0755 -q -d "${klipper_socket%\/*}"
	checkpath --owner ${RC_SVCNAME}:${RC_SVCNAME} --mode 0755 -q -d "${klipper_log%\/*}"
}

depend() {
	need localmount
	need udev
	after net
}

start_stop_daemon_args="--user ${user} --group ${group} \
	--stdout ${klipper_log} \
	--stderr ${klipper_log}"
