#!/bin/sh # PROVIDE: hommabot # REQUIRE: NETWORKING # KEYWORD: shutdown . /etc/rc.subr name="hommabot" rcvar="${name}_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}" || { echo "${name}: cannot create ${hommabot_data_dir}" return 1 } if [ ! -e "${hommabot_log_file}" ]; then install -m 0600 /dev/null "${hommabot_log_file}" || { echo "${name}: cannot create ${hommabot_log_file}" return 1 } else chmod 0600 "${hommabot_log_file}" || { echo "${name}: cannot set permissions on ${hommabot_log_file}" return 1 } fi } hommabot_start() { local bot_token bot_token=$(cat "${hommabot_token_file}") || { echo "${name}: cannot read ${hommabot_token_file}" return 1 } cd "%%PREFIX%%/libexec/hommabot" || { echo "${name}: cannot change to %%PREFIX%%/libexec/hommabot" return 1 } echo "Starting ${name}." exec /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"