#!/bin/sh

# PROVIDE: gwsocket
# REQUIRE: LOGIN
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable gwsocket
# gwsocket_enable="YES"
#
# Add the following lines to /etc/rc.conf to enable multiple instances of gwsocket
# gwsocket_multi_enable="YES"
# An instance is created for each *.conf file found in the configration files directory
#
# gwsocket_enable (bool):		Set to YES to enable gwsocket
#					Default: NO
# gwsocket_multi_enable (bool):	Set to YES to run multiple instances (gwsocket_config is ignored)
#					Default: NO
# gwsocket_conf_d (str):		gwsocket configration files directory
#					Default: /usr/local/etc/gwsocket
# gwsocket_config (str):		gwsocket configration file (ignored when gwsocket_multi_enable="YES")
#					Default: /usr/local/etc/gwsocket/gwsocket.conf
# gwsocket_user (str):		gwsocket daemon user
#					Default: www
# gwsocket_group (str):		gwsocket daemon group
#					Default: www
# gwsocket_logdir (str):		gwsocket log directory
#					Default: /var/log/gwsocket
# gwsocket_piddir (str):		gwsocket pid file directory
#					Default: /var/run/gwsocket

. /etc/rc.subr

name="gwsocket"
rcvar=gwsocket_enable
load_rc_config $name

: ${gwsocket_enable:="NO"}
: ${gwsocket_multi_enable:="NO"}
: ${gwsocket_user:="www"}
: ${gwsocket_group:="www"}
: ${gwsocket_conf_d:="/usr/local/etc/gwsocket"}
: ${gwsocket_config:="${gwsocket_conf_d%/}/${name}.conf"}
: ${gwsocket_logdir:="/var/log/gwsocket"}
: ${gwsocket_piddir:="/var/run/gwsocket"}

required_dirs="${gwsocket_logdir} ${gwsocket_piddir}"
required_files=${gwsocket_config}
procname="/usr/local/bin/${name}"
procdesc="${name}"
pidfile="${gwsocket_piddir%/}/${name}.pid"
logfile="${gwsocket_logdir%/}/${name}.log"
command=/usr/sbin/daemon
start_precmd="gwsocket_precmd"

gwsocket_precmd()
{	
	# Loads the options declared in the configuration file into "$command_args".
	gwsocket_conf_to_args ${gwsocket_config}
}

gwsocket_conf_to_args()
{
	local _line gwsocket_config_param gwsocket_config_value config_file_path=$1
	command_args="-f -t ${procdesc} -p ${pidfile} -o ${logfile} ${procname}"

	while read -r _line; do
		# Only proceed with lines which contain variable declaration.
		echo "${_line}" | grep -q -E \
        	-e "^[[:blank:]]*[[:alpha:]_-][[:alnum:]_-]{0,30}=" ||
			continue

		gwsocket_config_param=${_line%%=*}
		gwsocket_config_value=${_line#*=}

		# Properly handle flag type paramaters
		if gwsocket_is_flag_param ${gwsocket_config_param}; then
			if checkyesno gwsocket_config_value; then
				command_args="$command_args --${gwsocket_config_param}"
			fi
		elif [ -n "${gwsocket_config_value}" ]; then
			command_args="$command_args --${gwsocket_config_param}=${gwsocket_config_value}"
		fi
	done < ${config_file_path}
}

gwsocket_find_instances()
{
	local instance_config
	for instance_config in ${gwsocket_conf_d%/}/*.conf; do
		[ -f "$instance_config" ] || break
		gwsocket_instances="$gwsocket_instances $(basename ${instance_config%.conf})";
	done
}

gwsocket_is_flag_param()
{
	case $1 in
	'echo-mode' | 'strict' )
		return 0
		;;
	*)
		return 1
		;;
	esac
}

# Handles multi-instance feature
if [ $# -eq 2 ]; then
	# Performs action on single instance by name
	_instance=$2
	echo "===> Instance: ${_instance}"

	# Setup for the requested instance name
	gwsocket_config="${gwsocket_conf_d%/}/${_instance}.conf"
	procdesc="${name}_${_instance}"
	pidfile="${gwsocket_piddir%/}/${_instance}.pid"
	logfile="${gwsocket_logdir%/}/${_instance}.log"
	
	# The config file for the named instance must exist
	required_files="${gwsocket_config}"
elif checkyesno gwsocket_multi_enable; then
	# Compile list of all instances or given instances
	_swap=$*; shift; _instances=$*
	gwsocket_find_instances
	_instances=${_instances:-${gwsocket_instances}}
	set -- ${_swap}

	# Performs action on each instance
	for _instance in ${_instances}; do
		/usr/local/etc/rc.d/${name} $1 ${_instance}
	done	
	exit 0
fi

run_rc_command "$1"

