[ -z "$LIBMPDCONFSOURCED" ] || return 0
LIBMPDCONFSOURCED="true"
# unset LIBMPDCONFSOURCED to reload it (if MPDCONF changed, for example)

# libmpdconf - shell script library for reading and modifying the
# configuration file for Music Player Daemon.
#
# The MPDCONF variable must be set to an existing file (which may be empty)
# before sourcing this library.  /etc/mpd.conf for the system and ~/.mpdconf
# for regular users are the configuration file names MPD checks.
#
# This was originally written to ease the configuration of the Debian package,
# but may be used by others (GPL v2 or later license)
#
# Required Dependencies: grep, sed, awk, any POSIX shell
#
# For the Debian init script:
# Optional Dependencies: pgrep, netstat
#
# Please report any incompatibilities with your system you may find with the
# dependencies, I want these shell functions to be as portable as possible
#
# Copyright 2004 by Eric Wong <eric@petta-tech.com>
#
# This project's homepage is: http://www.MusicPD.org
#
# 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 2 of the License, or (at your option) any later
# version.

ws="[:space:]"

# usage: enable_mpdconf_key $key
# uncomments a key if it exists
# use set_mpdconf_key to enable a non-existant key
enable_mpdconf_key () {
	[ -n "$MPDCONF" ] || exit 1
	[ -w "$MPDCONF" ] || exit 1
	local tmp="`mktemp $MPDCONF.XXXXXX`"
	sed -e "s#^[#$ws]\+\($1\)\([$ws]\+\)\+#\1\2#" < "$MPDCONF" > "$tmp"
	mv "$tmp" "$MPDCONF"
}

# usage: set_mpdconf_key $key $value
set_mpdconf_key () {
	[ -n "$MPDCONF" ] || exit 1
	[ -w "$MPDCONF" ] || exit 1
	# check if the variable is already set:
	if ! grep "^[$ws]*$1[$ws]\+\"$2\"" "$MPDCONF" >/dev/null; then
		enable_mpdconf_key "$1"
		if ! grep "^[$ws]*$1[$ws]\+" "$MPDCONF" >/dev/null; then
			echo "$1  \"\"" >> "$MPDCONF"
		fi
	fi
	local tmp="`mktemp $MPDCONF.XXXXXX`"
	sed -e "s#^[$ws]*\($1\)\([$ws]\+\).*#\1\2\"$2\"#" < "$MPDCONF" > "$tmp"
	mv "$tmp" "$MPDCONF"
}

# usage: get_mpdconf_key $key
# returns 0 and echos the key value to STDOUT
# returns 1 if it can't find the key
# example: PORT=`get_mpd_conf_key port || echo 6600`
get_mpdconf_key () {
	[ -n "$MPDCONF" ] || exit 1
	[ -r "$MPDCONF" ] || exit 1
	local _tmp_="`grep ^[$ws]*$1[$ws] $MPDCONF | tail -n 1 | awk '{print $2}' | sed -e 's#"##g'`"
	if [ -n "${_tmp_}" ]; then
		echo "${_tmp_}"
		return 0
	else
		return 1
	fi 
}

# usage: disable_mpdconf_key $key
# this comments an option out 
disable_mpdconf_key () {
	[ -n "$MPDCONF" ] || exit 1
	[ -w "$MPDCONF" ] || exit 1
	local tmp="`mktemp $MPDCONF.XXXXXX`"
	sed -e "s#^[$ws]*$1\([$ws]\+\)#\# $1\1#" < "$MPDCONF" > "$tmp"
	mv "$tmp" "$MPDCONF"
}

# The next two are Debian maintainer script specific:
# mpd_running: simple check to see if MPD is running
mpd_running () {
	if pgrep -x mpd >/dev/null; then
		return 0
	else
		return 1
	fi
}

# mpd_portused - returns 0 if the port MPD using is taken.  Also
# echoes the PIDs that MPD is running on.
mpd_portused () {
	local port="`get_mpdconf_key port || echo 6600`"
	# |grep " LISTEN "\
	local run="`netstat -plunt 2>/dev/null | grep '[0-9]/mpd ' \
		|grep -E :${port}[${ws}] |grep '^tcp'|grep ' LISTEN ' || true`"
	if [ -n "${run}" ]; then
		# not sure how stable the netstat output format is
		echo "${run}" | awk '{print $7}' | sed "s#/mpd##"
	  	return 0
	else
		return 1
	fi
}

# end of libmpdconf
