#!/bin/sh

# Copyright (c) 2018-2022, diyIT.org
# All rights reserved.
#
# BSD 2-Clause License ("Simplified BSD License" or "FreeBSD License")
# https://diyit.org/license/
#
#

# the two lines below are not just comments but required by rcorder; service -e
# PROVIDE: portfolio
# REQUIRE: NETWORKING DAEMON

. /etc/rc.subr

: ${portfolio_enable="NO"}
 
name=portfolio
port=8000
appdir="/data/${name}"

rcvar=${name}_enable

GUNICORN_CMD="/usr/local/bin/gunicorn -D -b unix:/var/run/${name}.sock --log-file /var/log/${name}.log \
    --chdir ${appdir} ${name}.wsgi"
# GUNICORN_CMD="/usr/local/bin/gunicorn -D -b 0.0.0.0:${port} -b [::]:${port} --log-file /var/log/${name}.log \
#     --chdir ${appdir} ${name}.wsgi"

start_cmd="server_start"
stop_cmd="server_stop"
restart_cmd="server_restart"

server_start()
{
    $GUNICORN_CMD
}

server_stop()
{
    ps ax | grep -ie "gunicorn: master \[${name}.wsgi\]" | grep -v grep | awk '{print $1}' | xargs kill -9
    ps ax | grep -ie "gunicorn: worker \[${name}.wsgi\]" | grep -v grep | awk '{print $1}' | xargs kill -9
}

server_restart()
{
    ps ax | grep -ie "gunicorn: master \[${name}.wsgi\]" | grep -v grep | awk '{print $1}' | xargs kill -9
	server_start
}

load_rc_config ${name}
run_rc_command "$1"
