#!/usr/bin/env python
import gtk, sys, gobject, gnome, os
from optparse import OptionParser
# TODO: put the window operation here
def parse_args (option, opt_str, value, parser):
	setattr (parser.values, option.dest,  list (parser.rargs))

parser = OptionParser()
parser.add_option ("-s", "--simulate", action="store_true", dest="simulate",
                   help="Recording operations will be done in simulation mode.",
                   default=False)
parser.add_option ("-p", "--preferences", action="store_true", dest="preferences",
                   help="Show preferences dialog and exit.", default = False)
                   
parser.add_option ("-w", "--write", action="callback", callback=parse_args,
                   help="Writes the filenames after this option to a CD. This "\
                   "will show a dialog suitable for embedding in other "\
                   "applications.", dest="write_files", default=None)
                   
(options, args) = parser.parse_args()

# Clean up argv before we load gst
sys.argv = sys.argv[:1]

# Data dir:
import os.path

bins_dir = os.path.dirname (os.path.abspath (sys.argv[0]))
prefix_dir = os.path.dirname (bins_dir)
lib_dir = os.path.join (prefix_dir, 'lib',
                        "python%d.%d" % sys.version_info[0:2],
                        'site-packages')
# Add our default lib dir to the path
if os.path.isdir (lib_dir) and lib_dir not in sys.path:
	sys.path.append (lib_dir)


from serpentine import SerpentineApplication, SerpentineError, operations, gtkutil, validate_music_list, HeadlessApplication
import serpentine

# gtkutil.traceback_main_loop ()

class OurListener (operations.OperationListener):
	def on_finished (self, event):
		gtk.main_quit ()
		sys.exit (0)
		
listener = OurListener ()
if options.write_files:
	app = HeadlessApplication ()
else:
	app = SerpentineApplication ()

app.listeners.append (listener)
app.preferences.simulate = options.simulate
gnome_app = gnome.init ('serpentine', app.preferences.version)

if app.preferences.simulate:
	gtkutil.dialog_warn ("Simulation mode", 
	                     "Serpentine is on simulation mode, no "\
	                     "actual writing will be done on inserted media.")

# Check if we want to write files
if options.write_files is not None:
	serpentine.write_files (app, options.write_files)

# Show main window
else:
	if options.preferences:
		app.preferences.dialog.set_transient_for (None)
		app.preferences.dialog.run ()
		app.preferences.dialog.hide ()
		sys.exit (0)
		
	else:
		app.show_window ()


try:
	app.start ()
except SerpentineError, e:
	print >> sys.stderr, e
	sys.exit (1)

gtk.main()
