#!/usr/bin/env python
try:
	import findrox; findrox.version(2, 0, 0)
	import os, rox, sys

	__builtins__._ = rox.i18n.translation(os.path.join(rox.app_dir, 'Messages'))

	from optparse import OptionParser

	parser = OptionParser()
	parser.add_option("--options",
		action="store_true", dest="options", default=False,
		help="display options dialog without running application")

	parser.add_option("-l", "--load",
		action="store_true", dest="load_songs", default=False,
		help="replace the current playlist with listed songs and start playing")

	parser.add_option("-e", "--enqueue",
		action="store_true", dest="add_songs", default=False,
		help="add listed songs to the playlist, do not start playing")

	parser.add_option("-p", "--play",
		action="store_true", dest="play", default=False,
		help="start playing the current song")

	parser.add_option("--pause",
		action="store_true", dest="pause", default=False,
		help="pause playing")

	parser.add_option("--stop",
		action="store_true", dest="stop", default=False,
		help="stop playing")

	parser.add_option("--next",
		action="store_true", dest="next", default=False,
		help="skip to the next song and start playing.")

	parser.add_option("--prev",
		action="store_true", dest="prev", default=False,
		help="play the previous song")

	(options, args) = parser.parse_args()

	import gtk, musicbox, xsoap

	if options.options:
		rox.edit_options()
		rox.mainloop()
	else:
		#see if another instance is running and pass it our args
		window = xsoap.get_server("_ROX_MUSICBOX")
		if window:
			cmd = "load_songs"	#default action
			for opt in options.__dict__:		#probably abusing optparse here, but I can see no
				if options.__dict__[opt]:		#other way to enumerate the options
					cmd = opt
					break		#we don't support more than one, so stop at the first one.
			xsoap.send_message(window, cmd, args)
		else:
			#if not then start a new instance
				gtk.threads_init()
				gtk.threads_enter()
				box = musicbox.MusicBox()
				box.show()
				rox.mainloop()
				gtk.threads_leave()
except:
	rox.report_exception()
