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

UDP2RAW_BINARY="${UDP2RAW_BINARY:-/usr/bin/udp2raw}"
UDP2RAW_LOGFILE="${UDP2RAW_LOGFILE:-/var/log/udp2raw.log}"
UDP2RAW_PIDFILE="${UDP2RAW_PIDFILE:-/run/${RC_SVCNAME}.pid}"
UDP2RAW_TERMTIMEOUT="${UDP2RAW_TERMTIMEOUT:-"TERM/25/KILL/5"}"

USER="nobody"
GROUP="nobody"

description="A tunnel which turns UDP Traffic into encrypted FakeTCP/UDP/ICMP traffic"
command="${UDP2RAW_BINARY}"
command_background=true
command_user="${USER}:${GROUP}"
command_args="
	--conf-file ${UDP2RAW_CONFIGFILE}
	${UDP2RAW_OPTS}"

pidfile="${UDP2RAW_PIDFILE}"
start_stop_daemon_args="--quiet -1 ${UDP2RAW_LOGFILE}"
retry="${UDP2RAW_TERMTIMEOUT}"

extra_commands="checkconfig"

depend() {
	before net
}

checkconfig() {
	if ! [ -f "${UDP2RAW_CONFIGFILE}" ] ; then
		eerror "You need an /etc/udp2raw/*.conf file to run udp2raw"
		eerror "There is a sample file in /usr/share/doc/udp2raw-tunnel-*"
		return 1
	fi

	if ! [ -f "${UDP2RAW_LOGFILE}" ]; then
		touch "${UDP2RAW_LOGFILE}" \
			&& chmod 0660 "${UDP2RAW_LOGFILE}" > /dev/null 2>&1 \
			&& chown ${USER}:${GROUP} "${UDP2RAW_LOGFILE}" > /dev/null 2>&1 \
			|| eerror "Failed to create: ${UDP2RAW_LOGFILE}"
	fi
}

start_pre() {
	checkconfig || return 1
}

# vim: set ft=gentoo-init-d ts=4 :
