#!/bin/sh
#
# PROVIDE: seahub
# REQUIRE: LOGIN cleanvar seafile
# KEYWORD: shutdown
#

#
# Add the following lines to /etc/rc.conf to enable seahub:
#
# seahub_enable (bool):		Set to "NO" by default.
#				Set it to "YES" to enable seahub.
# seafile_user (str): 		User to run seafile as
#				Default to "seafile" created by the port
# seafile_group (str):		Group to run seafile as
#				Default to "seafile" created by the port
# seafile_path (str):		Set to "" by default will use the path
#				/usr/local/www/haiwen/seafile-server.
#				Set it to a different path.
# seafile_ccnet (str):		Set to "" by default will use the path
#				/usr/local/www/haiwen/ccnet.
#				Set it to a different path.
# seafile_conf (str):		Set to "" by default will use the path
#				/usr/local/www/haiwen/conf.
#				Set it to a different path.
# seafile_datadir (str):	Set to "" by default will use the path
#				in file /usr/local/www/haiwen/ccnet/seafile.ini.
#				Set it to a different path.
# seafile_logdir (str):		Set to "" by default will use the path
#				/usr/local/www/haiwen/logs.
#				Set it to a different path.
# seahub_host (int):		Default is 0.0.0.0.
# seahub_port (int):		Default is 8000.

. /etc/rc.subr

name="seahub"
rcvar=seahub_enable

load_rc_config $name

extra_commands="clearsessions"
start_cmd="seahub_start"
restart_cmd="seahub_restart"
stop_cmd="seahub_stop"
clearsessions_cmd="seahub_clearsessions"

: ${seahub_enable="NO"}
: ${seafile_user:="seafile"}
: ${seafile_group:="seafile"}
: ${seafile_path:="/usr/local/www/haiwen/seafile-server"}
: ${seafile_ccnet:="/usr/local/www/haiwen/ccnet"}
: ${seafile_conf:="/usr/local/www/haiwen/conf"}
: ${seafile_datadir:="/usr/local/www/haiwen/seafile-data"}
: ${seafile_logdir:="/usr/local/www/haiwen/logs"}
: ${seahub_host:="0.0.0.0"}
: ${seahub_port:="8000"}

manage_py=${seafile_path}/seahub/manage.py
gunicorn_exe=/usr/local/bin/gunicorn-3.9
gunicorn_conf=${seafile_conf}/gunicorn.conf.py
pidfile=/usr/local/www/haiwen/pids/seahub.pid

command="/usr/local/bin/python3.9"

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

validate_seahub_running() {
	if pgrep -f "${manage_py}" 2>/dev/null 1>&2; then
		echo "Seahub is already running."
		exit 1;
	fi
}

warning_if_seafile_not_running() {
	if ! pgrep -f "seafile-controller -c ${seafile_ccnet}" 2>/dev/null 1>&2; then
		echo
		echo "Warning: seafile not running. Have you run \"service seafile start\" ?"
		echo
		exit 1
	fi
}

prepare_env() {
	if [ -z "$LANG" ]; then
		echo "LANG is not set in ENV, set to en_US.UTF-8"
		export LANG='en_US.UTF-8'
	fi
	if [ -z "$LC_ALL" ]; then
		echo "LC_ALL is not set in ENV, set to en_US.UTF-8"
		export LC_ALL='en_US.UTF-8'
	fi

	export CCNET_CONF_DIR=${seafile_ccnet}
	export SEAFILE_CONF_DIR=${seafile_datadir}
	export SEAFILE_CENTRAL_CONF_DIR=${seafile_conf}
	export PYTHONPATH=${seafile_path}/seafile/lib/python3.9/site-packages:${seafile_path}/seafile/lib64/python3.9/site-packages:${seafile_path}/seahub/thirdpart:$PYTHONPATH
	export SEAHUB_LOG_DIR=${seafile_logdir}
	export SEAFILE_RPC_PIPE_PATH=${seafile_path}/runtime

}

before_start() {
	prepare_env;
	warning_if_seafile_not_running;
	validate_seahub_running;
}

seahub_clearsessions() {
	prepare_env;

	echo "Start clear expired session records ..."
	su -m "${seafile_user}" -c "$command \"${manage_py}\" clearsessions"

	echo
	echo "Done"
	echo
}

seahub_start()
{
	if checkyesno seahub_enable; then
		check_required_before;
		before_start;
		echo "Starting seahub at port ${seahub_port} ..."
		su -m "${seafile_user}" -c "$command \"${gunicorn_exe}\" seahub.wsgi:application -c \"${gunicorn_conf}\" -b \"${seahub_host}:${seahub_port}\" --preload --chdir \"$seafile_path/seahub\""

	# Ensure seahub is started successfully
		sleep 5
		if ! pgrep -f "seahub.wsgi:application" 2>/dev/null 1>&2; then
			printf "\033[33mError:Seahub failed to start.\033[m\n"
			echo "Please try to run \"./seahub.sh start\" again"
			exit 1;
		fi
		echo
		echo "Seahub is started"
		echo
		else
			return 0
	fi
}

seahub_stop() {
	if [ -f ${pidfile} ]; then
		pid=$(cat "${pidfile}")
		echo "Stopping ${name}."
		kill ${pid}
		rm -f ${pidfile}
		return 0
	else
		echo "Seahub is not running"
	fi
}

seahub_restart()
{
	seahub_stop;
	sleep 2
	seahub_start;
}

run_rc_command "$1"
