#!/bin/sh
#
# $NetBSD: gotosocial.sh,v 1.2 2024/05/27 20:04:46 nikita Exp $
#
# PROVIDE: gotosocial
# REQUIRE: NETWORKING DAEMON
# KEYWORD: chrootdir

if test -f /etc/rc.subr; then
	. /etc/rc.subr
fi

name="gotosocial"
rcvar=${name}

command="/usr/pkg/bin/gotosocial"
: ${gotosocial_config:=/usr/pkg/etc/gotosocial/config.yaml}
command_args="--config-path ${gotosocial_config} server start 2>&1 | logger -t ${name} -p daemon.info &"
required_files="${gotosocial_config}"

start_precmd="gotosocial_precmd"

gotosocial_precmd()
{
	if ! test -f ${required_files}; then
		warn "${required_files} does not exist."
		return 1
	fi
	if ! test -d /var/chroot/gotosocial; then
		/bin/mkdir -p -m 0755 /var/chroot/gotosocial
	fi
	if ! test -f /var/db/gotosocial; then
		/bin/mkdir -p -m 0755 /var/db/gotosocial
	fi
	if ! test -f /var/www/gotosocial; then
		/bin/mkdir -p -m 0755 /var/www/gotosocial
	fi
	if ! test -f /var/db/gotosocial/storage/certs; then
		/bin/mkdir -p -m 0755 /var/db/gotosocial/storage/certs
	fi
	/usr/sbin/chown gotosocial:gotosocial /var/chroot/gotosocial
	# /usr/sbin/chown gotosocial:gotosocial /var/www/gotosocial
	/bin/cp -R /usr/pkg/share/examples/gotosocial/web/assets /var/www/gotosocial
	#/bin/cp -R /usr/pkg/share/examples/gotosocial/web/source /var/www/gotosocial
	/bin/cp -R /usr/pkg/share/examples/gotosocial/web/template /var/www/gotosocial
}

load_rc_config $name
run_rc_command "$1"
