#! /usr/bin/env ruby
require 'optparse'
require 'LOLastfm/client'

options = {}

OptionParser.new do |o|
	options[:socket] = '~/.LOLastfm/socket'

	o.on '-t', '--template TEMPLATE', 'the template to use' do |value|
		options[:template] = value
	end
end.parse!

if ARGV.first
	options[:socket] = ARGV.first
end

client = LOLastfm::Client.new(options[:socket])
client.send_command :now_playing?

exit 1 if (response = client.read_response).nil?

if options[:template]
	track  = response['track']
	title  = response['title']
	artist = response['artist']
	album  = response['album']

	puts eval("%Q{#{options[:template]}}")
else
	%w[track title artist album].each {|name|
		puts "#{name} #{response[name]}" if response[name]
	}
end
