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

START=50
USE_PROCD=1

start_service() {
	KEYS=$(find /usr/share/tang/db -name "*.jw*" -maxdepth 1 2>/dev/null | wc -l)
	if [ -z "${KEYS}" ] || [ "${KEYS}" = "0" ]; then # if db is empty generate new key pair
		mkdir -p /usr/share/tang/db
		/usr/sbin/tangd-keygen /usr/share/tang/db
		chown -R tang /usr/share/tang/db
	fi

	config_load "tang"
	config_get_bool enabled "config" "enabled" 0
	config_get port "config" "port" "9090"

	[ "${enabled}" = "1" ] || return

	procd_open_instance
	procd_set_param command /usr/sbin/tangd -p "${port}" -l /usr/share/tang/db
	procd_set_param respawn
	procd_set_param user tang
	procd_close_instance
}
