#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: subsonic
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Configuration settings for subsonic in /etc/rc.conf:
#
# subsonic_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable subsonic.
#
# subsonic_home (str):
#   Set to "/var/subsonic" by default.
#
# subsonic_host (str):
#   Set to "0.0.0.0" by default.
#   Specify which IP address to listen to.
#
# subsonic_port (int):
#   Set to "4040" by default.
#   Specify which port to listen on for HTTP(S).
#
# subsonic_ssl (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable SSL.
#
# subsonic_ssl_keystore (str):
#   Optionally set path to custom SSL keystore.
#
# subsonic_ssl_password (str):
#   Optionally set password for custom SSL keystore.
#
# subsonic_context_path (str):
#   Set to "/" by default.
#   Specify the last part of the Subsonic URL, typically "/" or "/subsonic".
#
# subsonic_max_memory (int):
#   Set to "100" by default.
#   Specify the memory limit (Java heap size) in megabytes.
#

. /etc/rc.subr

name=subsonic
rcvar=${name}_enable
procname="/usr/local/openjdk8/bin/java"

load_rc_config "${name}"

: ${subsonic_enable:="NO"}
: ${subsonic_user:="subsonic"}
: ${subsonic_group:="subsonic"}
: ${subsonic_home:="/var/subsonic"}
: ${subsonic_host:="0.0.0.0"}
: ${subsonic_port:="4040"}
: ${subsonic_ssl:="NO"}
: ${subsonic_context_path:="/"}
: ${subsonic_max_memory:="100"}

if checkyesno subsonic_ssl; then
	SUBSONIC_SSL_FLAGS="-Dsubsonic.httpsPort=${subsonic_port}" && subsonic_port="0"
	if [ ! -z ${subsonic_ssl_keystore} ]; then
		SUBSONIC_SSL_FLAGS=${SUBSONIC_SSL_FLAGS}" -Dsubsonic.ssl.keystore=${subsonic_ssl_keystore} -Dsubsonic.ssl.password=${subsonic_ssl_password}"
	fi
fi

start_precmd="export LC_CTYPE='en_US.UTF-8'"
subsonic_chdir="/usr/local/share/subsonic-standalone"
command="/usr/sbin/daemon"
command_args="-f ${procname} \
	-Dsubsonic.home=${subsonic_home} \
	-Dsubsonic.host=${subsonic_host} \
	-Dsubsonic.port=${subsonic_port} \
	${SUBSONIC_SSL_FLAGS} \
	-Dsubsonic.contextPath=${subsonic_context_path} \
	-Dsubsonic.defaultMusicFolder=${subsonic_home}/music \
	-Dsubsonic.defaultPodcastFolder=${subsonic_home}/Podcast \
	-Dsubsonic.defaultPlaylistFolder=${subsonic_home}/playlists \
	-Xmx${subsonic_max_memory}m \
	-Djava.awt.headless=true \
	-jar ${subsonic_chdir}/subsonic-booter-jar-with-dependencies.jar"

run_rc_command "$1"
