#!/bin/sh

# PROVIDE: seafile
# REQUIRE: LOGIN cleanvar mysql
# KEYWORD: shutdown
#

#
# Add the following lines to /etc/rc.conf to enable seafile:
#
# seafile_enable (bool):	Set to "NO" by default.
#				Set it to "YES" to enable seafile.
# seafile_user (str):		User to run seafile as
#				Default to "%%USERS%%" created by the port
# seafile_group (str):		Group to run seafile as
#				Default to "%%GROUPS%%" created by the port
# seafile_path (str):		Set to "" by default will use the path
#				%%PREFIX%%/%%SEAFILE_SERVER%%.
#				Set it to a different path.
# seafile_ccnet (str):		Set to "" by default will use the path
#				%%PREFIX%%/%%HAIWENDIR%%/ccnet.
#				Set it to a different path.
# seafile_conf (str):		Set to "" by default will use the path
#				%%PREFIX%%/%%HAIWENDIR%%/conf.
#				Set it to a different path.
# seafile_datadir (str):	Set to "" by default will use the path
#				in file %%PREFIX%%/%%HAIWENDIR%%/ccnet/seafile.ini.
#				Set it to a different path.
# seafile_logdir (str):		Set to "" by default will use the path
#				%%PREFIX%%/%%HAIWENDIR%%/logs
#				Set it to a different path.
# seafile_loglevel (str):	Set to "info" by default.
#				Possible values are debug, warning, info.

. /etc/rc.subr

name="seafile"
rcvar="seafile_enable"

load_rc_config $name

start_cmd="seafile_start"
restart_cmd="seafile_restart"
stop_cmd="seafile_stop"

: ${seafile_enable="NO"}
: ${seafile_user:="%%USERS%%"}
: ${seafile_group:="%%GROUPS%%"}
: ${seafile_path:="%%PREFIX%%/%%SEAFILE_SERVER%%"}
: ${seafile_ccnet:="%%PREFIX%%/%%HAIWENDIR%%/ccnet"}
: ${seafile_conf:="%%PREFIX%%/%%HAIWENDIR%%/conf"}
: ${seafile_datadir:="%%PREFIX%%/%%HAIWENDIR%%/seafile-data"}
: ${seafile_logdir:="%%PREFIX%%/%%HAIWENDIR%%/logs"}
: ${seafile_loglevel:="info"}

command="%%PREFIX%%/%%SEAFILE_SERVER%%/seafile/bin/seafile-controller"
command_args="-c \"${seafile_ccnet}\" -d \"${seafile_datadir}\" -F \"${seafile_conf}\" -l \"${seafile_logdir}\" -g \"${seafile_loglevel}\" -G \"${seafile_loglevel}\""

required_dirs="${seafile_ccnet} ${seafile_conf} ${seafile_datadir}"

test_config() {
	if ! su -m ${seafile_user} -c "${command} -t ${command_args}" ; then
		exit 1;
	fi
}

check_component_running() {
	name=$1
	cmd=$2
	if pid=$(pgrep -f "$cmd" 2>/dev/null); then
		echo "{$name} is running, pid $pid. You can stop it by: "
		echo
		echo "        kill $pid"
		echo
		echo "Stop it and try again."
		echo
		exit
	fi
}

validate_already_running() {
	if pid=$(pgrep -f "seafile-controller -c ${seafile_ccnet}" 2>/dev/null); then
		echo "Seafile controller is already running, pid $pid"
		echo
		exit 1;
	fi

	check_component_running "ccnet-server" "ccnet-server -c ${seafile_ccnet}"
	check_component_running "seaf-server" "seaf-server -c ${seafile_ccnet}"
	check_component_running "fileserver" "fileserver -c ${seafile_ccnet}"
	check_component_running "seafdav" "wsgidav.server.run_server"
}

prepare_env() {
export PATH=${seafile_path}/seafile/bin:$PATH
export LD_LIBRARY_PATH=${seafile_path}/seafile/lib/:${seafile_path}/seafile/lib64:${LD_LIBRARY_PATH}
export CCNET_CONF_DIR=${seafile_ccnet}
export SEAFILE_CONF_DIR=${seafile_datadir}
export SEAFILE_CENTRAL_CONF_DIR=${seafile_conf}
export PYTHON=%%PREFIX%%/bin/%%PYTHON%%
export PYTHONPATH=${seafile_path}/seafile/lib/%%PYTHON%%/site-packages:${seafile_path}/seafile/lib64/%%PYTHON%%/site-packages:${seafile_path}/seahub/thirdpart:$PYTHONPATH
export SEAFILE_RPC_PIPE_PATH=${seafile_path}/runtime
}

seafile_start() {
	check_required_before;
	validate_already_running;
	prepare_env;
# test_config broke in 8.0.x, commenting until this is fixed.
# https://forum.seafile.com/t/seafile-controller-t-functionality-broken-on-8-x/13738
#	test_config;

	echo "Starting seafile server, please wait ..."

	su -m "${seafile_user}" -c "mkdir -p $seafile_logdir"
	su -m "${seafile_user}" -c "$command $command_args"

	sleep 3

	# check if seafile server started successfully
	if ! pgrep -f "seafile-controller -c ${seafile_ccnet}" 2>/dev/null 1>&2; then
	echo "Failed to start seafile server"
	exit 1;
	fi

	echo "Seafile server started"
	echo
}

seafile_stop() {
	if ! pgrep -f "seafile-controller -c ${seafile_ccnet}" 2>/dev/null 1>&2; then
		echo "Seafile is not running"
	return 1;
	fi

	echo "Stopping ${name}."
	pkill -SIGTERM -f "seafile-controller -c ${seafile_ccnet}"
	pkill -f "ccnet-server -c ${seafile_ccnet}"
	pkill -f "seaf-server -c ${seafile_ccnet}"
	pkill -f "fileserver -c ${seafile_ccnet}"
	pkill -f "soffice.*--invisible --nocrashreport"
	pkill -f  "wsgidav.server.run_server"
	return 0
}

seafile_restart() {
	seafile_stop;
	sleep 2
	seafile_start;
}

run_rc_command "$1"
