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

options = {}

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

	o.on '--listened', 'enable listened sending' do
		options[:listened] = true
	end

	o.on '--now-playing', 'enable now playing sending' do
		options[:now_playing] = true
	end

	o.on '--love', 'enable love sending' do
		options[:love] = true
	end

	o.on '--unlove', 'enable unlove sending' do
		options[:unlove] = true
	end

	o.on '-c', '--current', 'work on the current song' do
		options[:current] = true
	end

	o.on '-t', '--title TITLE', 'title of the song' do |value|
		options[:song][:title] = value
	end

	o.on '-a', '--artist ARTIST', 'the artist of the song' do |value|
		options[:song][:artist] = value
	end

	o.on '-A', '--album ALBUM', 'the album of the song' do |value|
		options[:song][:album] = value
	end

	o.on '-l', '--length LENGTH', Integer, 'the length of the song in seconds' do |value|
		options[:song][:length] = value
	end

	o.on '-L', '--listened-at DATE', 'the time when the song has started playing' do |value|
		options[:song][:listened_at] = value
	end

	o.on '-p', '--path PATH', 'the path of the song' do |value|
		options[:song][:path] = value
	end

	o.on '-i', '--id ID', 'the MusicBrainz id of the song' do |value|
		options[:song][:id] = value
	end

	o.on '-u', '--use USE', 'the string for the checker' do |value|
		options[:use] = value
	end

	o.on '-u', '--commands COMMANDS', 'the string for the command library' do |value|
		options[:commands] = value
	end
end.parse!

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

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

if options[:use]
	client.send_command :use, options[:use].split(':')
elsif options[:commands]
	client.send_command :commands, options[:commands].split(':')
else
	if options[:now_playing]
		client.send_command :now_playing, options[:song]
	elsif options[:love]
		client.send_command :love, (options[:song].empty? ? (options[:current] ? :current : nil) : options[:song])
	elsif options[:unlove]
		client.send_command :unlove, (options[:song].empty? ? (options[:current] ? :current : nil) : options[:song])
	else
		client.send_command :listened, options[:song]
	end
end
