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

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

extra_commands="checkconfig"

depend() {
	need net
}

checkconfig() {
	if [ ! -f ${V2_CONFIG} ]; then
		ewarn "${V2_CONFIG} does not exist."
	fi
	v2ray test -c "$V2_CONFIG"
}

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() {
	ebegin "Stopping V2ray"
	start-stop-daemon --stop -p ${V2_PIDFILE}
	eend $?
}
