#!/usr/bin/ruby -w

# This is a very simple MPD client that just sends commands to the server
# from the command line
#
# Copyright 2006 Andrew Rader ( bitwise_mcgee AT yahoo.com | http://nymb.us )
#

def controlmpd(command,argopts = "")

require 'lib/librmpd'
require 'thread'

		mpd = MPD.new
		mpd.connect
		case command
			when 'seekid'
			         tmpstats = mpd.seekid( argopts.to_i , argopts.to_i)
				tmpresulthash = mpd.current_song
				tmpstats = ""
				tmpresulthash.each_pair do |key, value|
					
					tmpstats = tmpstats +  "#{key} => #{value}\n"
					end
				mpd.disconnect
				return tmpstats
			when 'albums'
			 temparrayresult = mpd.albums
			 tmpresult = ""
				  temparrayresult.each {|x|
                                         tmpresult = tmpresult + "\n#{x}"
                                       }
				       mpd.disconnect
                                  return    tmpresult
	
		when 'files'
			 temparrayresult = mpd.files
			 tmpresult = ""
				  temparrayresult.each {|x|
                                         tmpresult = tmpresult + "\n#{x}"
                                       }
				       mpd.disconnect
                                  return    tmpresult  
			when 'playlists'
			         tmpstats = mpd.playlists
				  totalresult = ""
				 tmpstats.each { |part| 
				 totalresult = totalresult + "#{part}\n"
				             }
				 mpd.disconnect
				return totalresult
			when 'load'
			         mpd.load( argopts.to_s)
				  mpd.play 
				tmpresulthash = mpd.current_song
				tmpstats = ""
				tmpresulthash.each_pair do |key, value|
					
					tmpstats = tmpstats +  "#{key} => #{value}\n"
					end
				mpd.disconnect
				return tmpstats
			when 'search_artist'
			         tmpstats = mpd.search( 'artist', argopts.to_s)
				 mpd.disconnect
			return tmpstats
			when 'play'
                                mpd.play argopts 
				tmpresulthash = mpd.current_song
				tmpstats = ""
				tmpresulthash.each_pair do |key, value|
					
					tmpstats = tmpstats +  "#{key} => #{value}\n"
					end
				mpd.disconnect
				return tmpstats
                        when 'pause'
				mpd.pause = !mpd.paused?
				tmpresulthash = mpd.current_song
				tmpstats = ""
				tmpresulthash.each_pair do |key, value|
					
					tmpstats = tmpstats +  "#{key} => #{value}\n"
					end
				mpd.disconnect
				return tmpstats
			when 'stop'
				mpd.stop
			when 'next'
				mpd.next
				tmpresulthash = mpd.current_song
				tmpstats = ""
				tmpresulthash.each_pair do |key, value|
					
					tmpstats = tmpstats +  "#{key} => #{value}\n"
					end
				mpd.disconnect
				return tmpstats
			when 'prev'
				mpd.previous
				tmpresulthash = mpd.current_song
				tmpstats = ""
				tmpresulthash.each_pair do |key, value|
					
					tmpstats = tmpstats +  "#{key} => #{value}\n"
					end
				mpd.disconnect
				return tmpstats
			when 'volume'
					mpd.volume = argopts.to_i
			
			when 'repeat'
				mpd.repeat = !mpd.repeat?
			when 'random'
				mpd.random = !mpd.random?
			when 'stats'
				hash = mpd.stats
				tmpstats = ""
				hash.each_pair do |key, value|
					
					tmpstats = tmpstats +  "#{key} => #{value}\n"
				end
				mpd.disconnect
				return tmpstats
			when 'status'
				hash = mpd.status
				tmpstats = ""
				hash.each_pair do |key, value|
					
					tmpstats = tmpstats +  "#{key} => #{value}\n"
				end
				mpd.disconnect
				return tmpstats
			else
				$stderr.puts "Unknown Command #{command}"
		end
		mpd.disconnect

end