#!/bin/sh /etc/rc.common

START=82

USE_PROCD=1
PROG=/usr/bin/gerbera

start_service() {
	local enabled
	local debug
	local user
	local group
	local home

	config_load 'gerbera'
	config_get_bool enabled config 'enabled' '0'
	config_get_bool debug config 'debug' '0'

	[ "$enabled" -eq 0 ] && {
		echo "Gerbera not enabled. Please enable in /etc/config/gerbera"
		return 1
	}

	config_get user config 'user' 'gerbera'
	config_get group config 'group' 'gerbera'
	config_get home config 'home' '/tmp/gerbera'

	[ -d "$home" ] || {
		mkdir -p "$home"
		chown "$user":"$group" "$home"

		gerbera -m "$home" -f '' --create-config > "$home/config.xml" 2> /dev/null
		chown "$user":"$group" "$home/config.xml"
		chmod 600 "$home/config.xml"

		echo "Created default gerbera config at $home/config.xml"
		echo "Please edit to your liking and restart."
		return 2
	}

	procd_open_instance
	procd_set_param user "$user"
	procd_set_param group "$group"
	procd_set_param command "$PROG" -c "$home/config.xml"
	procd_set_param stdout "$debug"
	procd_set_param stderr 1
	procd_close_instance
}
