#!/bin/sh
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @author    OpenMediaVault Plugin Developers <plugins@omv-extras.org>
# @copyright Copyright (c) 2009-2013 Volker Theile
# @copyright Copyright (c) 2013-2015 OpenMediaVault Plugin Developers
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

SEDCMD=/bin/sed
GREPCMD=/bin/grep
MINIDLNA_CONFIG="/etc/minidlna.conf"

write_config() {
	PARAM=$1
	if [ "$2" != "" ] ; then
		#Uncomment first occurence of param if needed and put value
		VALUE=$2
		if ${GREPCMD} -q "^${PARAM}=" ${MINIDLNA_CONFIG} ; then
			# Found parameter: change it
			${SEDCMD} -i -e "s/^${PARAM}=.*/${PARAM}=${VALUE}/" ${MINIDLNA_CONFIG} 
		else
			# parameter is not found: uncomment it if exist
			if ${GREPCMD} -q "^#${PARAM}=" ${MINIDLNA_CONFIG} ; then
				# Parameter is commented: uncomment it with the good value
				${SEDCMD} -i -e "s/^#${PARAM}=.*/${PARAM}=${VALUE}/" ${MINIDLNA_CONFIG}
			else
				# Parameter doesn't exit: put it
				echo "# ${0} parameter ${PARAM} not found. Script add it" >> ${MINIDLNA_CONFIG}
				echo "${PARAM}=${VALUE}" >> ${MINIDLNA_CONFIG}
			fi
		fi
	else
		#Comment first occurent or param if exist
		${SEDCMD} -i -e "0,/^#?${PARAM}/ s/^#?${PARAM}=.*/#${PARAM}=/" ${MINIDLNA_CONFIG}
	fi
}

log_level=$(omv_config_get "//services/minidlna/loglevel")
name=$(omv_config_get "//services/minidlna/name")
port=$(omv_config_get "//services/minidlna/port")
rootcontainer=$(omv_config_get "//services/minidlna/rootcontainer")
strict=$(omv_config_get "//services/minidlna/strict")
tivo=$(omv_config_get "//services/minidlna/tivo")

if [ "${strict}" = "1" ]; then
    strict="yes"
else
    strict="no"
fi

if [ "${tivo}" = "1" ]; then
    tivo="yes"
else
    tivo="no"
fi

# Update minidlna config file
write_config "log_level" ${log_level}
write_config "friendly_name" ${name}
write_config "port" ${port}
write_config "root_container" ${rootcontainer}
write_config "strict_dlna" ${strict}
write_config "enable_tivo" ${tivo}

OMV_SHARE=""
# Process FTP shares
index=$(omv_config_get_count "//services/minidlna/shares/share")
while [ ${index} -gt 0 ]; do

    # Get the shared folder reference and path
    sfref=$(omv_config_get "//services/minidlna/shares/share[position()=${index}]/sharedfolderref")
    sfpath=$(omv_get_sharedfolder_path "${sfref}")
    mtype=$(omv_config_get "//services/minidlna/shares/share[position()=${index}]/mtype")

    if [ "${mtype}" != "" ]; then
        mtype="${mtype},"
    fi

    if [ "${sfpath}" != "" ]; then
	OMV_SHARE="${OMV_SHARE}media_dir=${mtype}${sfpath}\n"
    fi

    index=$(( ${index} - 1 ))
done

# Delete previous share
/bin/sed -i '/\#OMV_SHARE/,/\#END_OMV/d' ${MINIDLNA_CONFIG}
# Write the new configuration at the end of the file
echo "#OMV_SHARE
${OMV_SHARE}#END_OMV" >> ${MINIDLNA_CONFIG}

