#!/bin/sh

# PROVIDE: v2ray
# REQUIRE: DAEMON NETWORKING FILESYSTEMS
# BEFORE: LOGIN
# KEYWORD: shutdown

# Add these lines to /etc/rc.conf.local or /etc/rc.conf to enable `v2ray':
#
# v2ray_enable (bool):          Set to "NO" by default.
#                               Set it to "YES" to enable v2ray
# v2ray_config (path):          Set to "/usr/local/etc/v2ray/config.json" by default
#                               Set it to the v2ray server config
# v2ray_logdir (path):		Set to "/var/log/v2ray" by default.
#                               Set it to the directory of v2ray log files
# v2ray_env (str):		Set to "" by default.
#                               Set it to the desired environment variables

. /etc/rc.subr

desc="v2ray startup script"
name="v2ray"
rcvar="v2ray_enable"

load_rc_config ${name}

: ${v2ray_enable="NO"}
: ${v2ray_env=""}
: ${v2ray_config="/usr/local/etc/v2ray/config.json"}
: ${v2ray_group="v2ray"}
: ${v2ray_user="v2ray"}
: ${v2ray_logdir="/var/log/v2ray"}

asset_env="V2RAY_LOCATION_ASSET=/usr/local/share/v2ray"
pidfile="/var/run/v2ray.pid"
procname="/usr/local/bin/v2ray"
command="/usr/sbin/daemon"
command_args="-c -p ${pidfile} /usr/bin/env ${asset_env} ${v2ray_env} ${procname} run -config ${v2ray_config}"
required_files="${v2ray_config}"

start_precmd="v2ray_startprecmd"

v2ray_startprecmd() {
	touch "${pidfile}"
	mkdir -p "${v2ray_logdir}"
	chown -R ${v2ray_user}:${v2ray_group} "${pidfile}" "${v2ray_logdir}"
}

run_rc_command "$1"
