#!/bin/sh /etc/rc.common
# Copyright (C) 2015 OpenWrt.org

START=99
STOP=1

USE_PROCD=1
PROG=/usr/bin/squeezelite

#
# Auto config checks for existing codec installations if not specified in config
# Explicit disable (override) by setting appropriate "decode_xxx" in config
#
checkcodec() {
	config_get_bool auto_conf options "decoder_auto_conf" 1
	config_get_bool codec options "$1" $auto_conf
	
	if [ $codec -ne 0 ] ; then 
		if [ $auto_conf -eq 0 ] ; then
			#force use requested
			echo "$4"
		else
			if [ -e "/usr/lib/${2}" ] ; then
				# Use codec (it exists)
				echo "$4"
			else
 				#exclude non-existant
			        if [ -z "$4" ] ; then
			                echo "-e $3"
			        else
			                echo "$4,$3"
			        fi
			fi
		fi
	else
		# explicitly excluded
	        if [ -z "$4" ] ; then
	                echo "-e $3"
	        else
	                echo "$4,$3"
	        fi
	fi
}

make_cmdline() {
	cmdline=""

	config_get name options name "SqueezeWrt"
	cmdline="$cmdline -n $name"

	config_get model_name options model_name "SqueezeLite"
	cmdline="$cmdline -M $model_name"

	config_get interface options interface ""
	[ -n "$interface" ] && cmdline="$cmdline -I $interface"

	config_get device options device ""
	[ -n "$device" ] && cmdline="$cmdline -o $device"

	config_get alsa_buffer options alsa_buffer 200
	[ $alsa_buffer -eq 0 ] && alsa_buffer="200"
    
	config_get alsa_period options alsa_period 4
	[ $alsa_period -eq 0 ] && alsa_period="4"
    
	config_get alsa_format options alsa_format 16
	[ $alsa_format = "0" ] && alsa_format="16"

	config_get alsa_mmap options alsa_mmap 0
	cmdline="$cmdline -a $alsa_buffer:$alsa_period:$alsa_format:$alsa_mmap"

	config_get stream_bufsiz options stream_bufsiz 2048
	config_get out_bufsiz options out_bufsiz 3763
	cmdline="$cmdline -b $stream_bufsiz:$out_bufsiz"

	config_get max_sr options max_sr 0
	if [ $max_sr -ne 0 ] ; then
		max_sr="-r $max_sr"

		config_get sr_delay options sr_delay 0
		[ $sr_delay -ne 0 ] && 	max_sr="$max_sr:$sr_delay"
		cmdline="$cmdline $max_sr"
	fi


	config_get close_delay options close_delay 0
	[ $close_delay -ne 0 ] && cmdline="$cmdline -C $close_delay"

	config_get server_addr options server_addr ""
	if [ -n "$server_addr" ] ; then
		config_get server_port options server_port 3483
		cmdline="$cmdline -s $server_addr:$server_port"
	fi

	config_get priority options priority 0
	[ $priority -ne 0 ] && cmdline="$cmdline -p $priority"

	#
	# ***NOTE: codec lib names are in squeezelite.h (set decode_auto_conf to 0 to ignore)
	#
	local excl_codecs=""
	local vorbis_lib="libvorbisidec.so.1"

	excl_codecs=`checkcodec decode_flac "libFLAC.so.8" flac "$excl_codecs"`
	excl_codecs=`checkcodec decode_mp3 "libmad.so.0" mp3 "$excl_codecs"`
	excl_codecs=`checkcodec decode_aac "libfaad.so.2" aac "$excl_codecs"`

	[ -e "/usr/lib/$vorbis_lib" ] || vorbis_lib="libvorbisfile.so.3"

	excl_codecs=`checkcodec decode_ogg "$vorbis_lib" ogg "$excl_codecs"`
	excl_codecs=`checkcodec decode_wma_alac "libavcodec.so.56" wma,alac "$excl_codecs"`
	cmdline="$cmdline $excl_codecs"

	config_get dop options dsd_over_pcm 0
	[ $dop -eq 1 ] && cmdline="$cmdline -D"
}

start_service() {
	config_load squeezelite

	config_get_bool enabled options 'enabled' 0
	[ $enabled -eq 0 ] && return

	# Build command params
	make_cmdline

	procd_open_instance
	logger -t 'squeezelite' "$cmdline"
	procd_set_param command "$PROG" $cmdline
	procd_close_instance
}

# Wait for service to exit and release sockets
reload_service() {
	stop
	sleep 2
	start
}

restart() {
	reload_service
}