#!/usr/bin/with-contenv bashio
set -e

bashio::log.info  'Setup settings'

if [ "$(bashio::config 'custom_config')" != 'null' ]; then
	bashio::log.info 'Use custom config'
    cp "$(bashio::config 'custom_config')" '/etc/mpd.conf'
else
	bashio::log.info 'Set media folder'
	MEDIA_FOLDER=$(bashio::config 'media_folder')
	sed -i "s/music_directory.*/music_directory \"${MEDIA_FOLDER//\//\\/}\"/" /etc/mpd.conf

	bashio::log.info 'Set playlist folder'
	PLAYLIST_FOLDER=$(bashio::config 'playlist_folder')
	sed -i "s/playlist_directory.*/playlist_directory \"${PLAYLIST_FOLDER//\//\\/}\"/" /etc/mpd.conf

	if [ "$(bashio::config 'volume_normalization')" == 'true' ]; then
		sed -i "/^#volume_normalization/s/^#//" /etc/mpd.conf
	fi

	if [ "$(bashio::config 'verbose')" == 'true' ]; then
		sed -i "/^log_level/s/default/verbose/" /etc/mpd.conf
	fi

	if [ "$(bashio::config 'httpd_output')" == 'true' ]; then
		printf '\n
		audio_output { \n
			type		"httpd" \n
			name		"HTTPd Output" \n 
			port		"8000" \n
			bitrate		"192" \n
		}' >> /etc/mpd.conf
		bashio::log.info 'HTTPd output enabled'
	fi
fi
