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

depend() {
	need net
}

checkconfig() {
	if [ "${RC_SVCNAME}" != "v2ray" ] ; then
		V2_CFGNAME="${RC_SVCNAME#*.}."
	fi

	V2_CONFIG="/etc/v2ray/config.${V2_CFGNAME}json"
	V2_PIDFILE="/run/v2ray.${V2_CFGNAME}pid"
	V2_LOG="/var/log/v2ray.${V2_CFGNAME}log"

	if [ ! -f ${V2_CONFIG} ]; then
		ewarn "${V2_CONFIG} does not exist."
		return 1
	fi

	return 0
}

start() {
	checkconfig || return 1

	ebegin "Starting V2ray"
	ebegin "Log File : ${V2_LOG}"
	start-stop-daemon --start	\
	-b -1 ${V2_LOG} -2 ${V2_LOG}	\
	-m -p ${V2_PIDFILE}		\
	--exec /usr/bin/v2ray -- run -config ${V2_CONFIG}
	eend $?

}

stop() {
	checkconfig || return 1
	ebegin "Stopping V2ray"
	start-stop-daemon --stop -p ${V2_PIDFILE}
	eend $?
}
