aboutsummaryrefslogtreecommitdiffstats
path: root/irc/py-sopel/files/sopel.in
blob: 278c461cbe37273d237fdf3026efc64b69cf4dcf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh

# PROVIDE: sopel
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Configuration settings for sopel in /etc/rc.conf
#
# sopel_enable (bool):	Enable sopel. (default=NO)
# sopel_piddir (str):	Directory in which to put the process ID file. (default=/var/run/sopel)
# sopel_confdir (str):	Configuration directory. (default=%%ETCDIR%%)
# sopel_flags (str):	Flags used for sopel. (default=--config-dir "${sopel_confdir}")
# sopel_script (str):	Path to sopel application. (default=%%PREFIX%%/bin/sopel)
# sopel_user (str):	User to run sopel as. (default=sopel)
# sopel_profiles (str):	List of profiles for running multiple sopel instances.
#			(default=default)
# sopel_prefix (str):	Each profile or configuration file must begin with thix prefix
#			followed by the profile name, followed by the extension .cfg,
#			such as sopel-default.cfg, sopel-libera.chat.cfg, etc.
#			(default=sopel-)
# sopel_output (str):	Send stdout and stderr to a file. If you set the logdir parameter in
#			the sopel file configuration, your best option is to send the output
#			to /dev/null. But this can be changed for debugging.
#			(default=/dev/null)

. /etc/rc.subr

name=sopel
rcvar=sopel_enable
desc="Simple, easy-to-use, open-source IRC utility bot, written in Python"
start_precmd=sopel_checkprofile
stop_precmd=sopel_checkprofile
start_cmd=sopel_start
stop_cmd=sopel_stop
restart_cmd=sopel_restart
status_cmd=sopel_status
configure_cmd=sopel_configure
extra_commands="configure status"
command_interpreter="%%PYTHON_CMD%%"

load_rc_config $name

: ${sopel_enable:=NO}
: ${sopel_piddir:=/var/run/sopel}
: ${sopel_confdir:=%%ETCDIR%%}
: ${sopel_flags=--config-dir "${sopel_confdir}"}
: ${sopel_script:=%%PREFIX%%/bin/sopel}
: ${sopel_user:=sopel}
: ${sopel_profiles:=default}
: ${sopel_prefix:=sopel-}
: ${sopel_output:=/dev/null}

sopel_checkprofile()
{
	if ! [ -f "${sopel_confdir}/${sopel_prefix}${profile}.cfg" ]; then
		echo "Sopel profile '${profile}' does not exist."
		return 1
	fi

	return 0
}

sopel_start()
{
	local profile

	profile="$1"; shift

	echo "Starting sopel profile '${profile}'." && sleep 1
        daemon \
		-o "${sopel_output}" \
		-t "${desc}" \
		-u "${sopel_user}" \
		${command_interpreter} \
		${sopel_script} start ${sopel_flags} \
			-c "${sopel_prefix}${profile}" $@
}

sopel_stop()
{
	local pid pidfile profile

	profile="$1"; shift

	pidfile="${sopel_piddir}/sopel-${sopel_prefix}${profile}.pid"
	if ! [ -f "${pidfile}" ]; then
		echo "sopel profile '${profile}' not running? (check ${sopel_piddir}/sopel-${sopel_prefix}${profile}.pid)."
		return 1
	fi

	pid=`cat ${pidfile}`

	echo "Stopping sopel profile '${profile}'."
        daemon \
		-o "${sopel_output}" \
		${command_interpreter} \
		${sopel_script} stop ${sopel_flags} \
			-c "${sopel_prefix}${profile}" $@

	wait_for_pids $pid
}

sopel_restart()
{
	local profile

	profile="$1"; shift

        run_rc_command stop "${profile}" $@
        run_rc_command start "${profile}" $@
}

sopel_status()
{
	local profile pid

	profile="$1"; shift

	pid=`check_pidfile \
		"${sopel_piddir}/sopel-${sopel_prefix}${profile}.pid" \
		"${sopel_script}" \
		"${command_interpreter}"`

	if [ -n "${pid}" ]; then
		echo "Sopel profile '${profile}' is running as pid ${pid}."
	else
		echo "Sopel profile '${profile}' is not running."
	fi
}

sopel_configure()
{
	local profile

	profile="$1"; shift

	echo "Configuring profile '${profile}'..."

	${command_interpreter} \
        ${sopel_script} configure ${sopel_flags} \
		-c "${sopel_confdir}/${sopel_prefix}${profile}" $@
}

cmd="$1"; shift
for profile in ${sopel_profiles}; do
	run_rc_command "${cmd}" "${profile}" $@
done