aboutsummaryrefslogtreecommitdiffstats
path: root/net-im/hommabot/files/hommabot.in
blob: 602433d354dbcf4f56d164c2c056405658b312bc (plain)
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
#!/bin/sh

# PROVIDE: hommabot
# REQUIRE: NETWORKING
# KEYWORD: shutdown

. /etc/rc.subr

name="hommabot"
rcvar="hommabot_enable"

load_rc_config "${name}"

: ${hommabot_enable:="NO"}
: ${hommabot_token_file:="%%PREFIX%%/etc/hommabot.token"}
: ${hommabot_data_dir:="/var/db/hommabot"}
: ${hommabot_tz:="Europe/Helsinki"}
: ${hommabot_log_file:="/var/log/hommabot.log"}

pidfile="/var/run/${name}.pid"
procname="/usr/sbin/daemon"

start_precmd="${name}_prestart"
start_cmd="${name}_start"

hommabot_prestart()
{
	if [ ! -r "${hommabot_token_file}" ]; then
		echo "${name}: token file ${hommabot_token_file} does not exist or is not readable"
		return 1
	fi

	if [ ! -s "${hommabot_token_file}" ]; then
		echo "${name}: token file ${hommabot_token_file} is empty"
		return 1
	fi

	install -d -m 0700 "${hommabot_data_dir}" || return 1

	if [ ! -e "${hommabot_log_file}" ]; then
		install -m 0600 /dev/null "${hommabot_log_file}" || return 1
	else
		chmod 0600 "${hommabot_log_file}" || return 1
	fi
}

hommabot_start()
{
	local bot_token

	if check_pidfile "${pidfile}" "${procname}" >/dev/null; then
		echo "${name} is already running."
		return 1
	fi

	bot_token=$(cat "${hommabot_token_file}") || return 1

	if [ -z "${bot_token}" ]; then
		echo "${name}: token file ${hommabot_token_file} is empty"
		return 1
	fi

	cd "%%PREFIX%%/libexec/hommabot" || {
		echo "${name}: cannot change to %%PREFIX%%/libexec/hommabot"
		return 1
	}

	echo "Starting ${name}."

	/usr/sbin/daemon \
		-f \
		-H \
		-P "${pidfile}" \
		-o "${hommabot_log_file}" \
		/usr/bin/env \
		PATH="%%PREFIX%%/bin:/usr/bin:/bin" \
		TELEGRAM_BOT_TOKEN="${bot_token}" \
		SQLITE_PATH="${hommabot_data_dir}/db.sqlite3" \
		TZ="${hommabot_tz}" \
		"%%PREFIX%%/bin/node" \
		"%%PREFIX%%/libexec/hommabot/node_modules/tsx/dist/cli.mjs" \
		"%%PREFIX%%/libexec/hommabot/src/bot.ts"
}

run_rc_command "$1"