#!/usr/bin/env python
#
# Copyright (C) 2007 daelstorm. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Based on code from PySoulSeek and Nicotine

"""
Contact info:
daelstorm@gmail.com
"""
import platform, os
import sys
win32 = sys.platform.startswith("win")
LOAD_PSYCO = False
if win32 and platform.architecture()[0] == "32bit":
	LOAD_PSYCO = True
else:
	try:
		if os.uname()[4] in ("i386", "i586", "i686") and platform.architecture()[0] == "32bit":
			LOAD_PSYCO = True
		
	except AttributeError, error:
		pass
		
if LOAD_PSYCO:
	try:
		import psyco
		psyco.profile()
	except ImportError:
		print """Nicotine supports \"psyco\", an inline optimizer for python
code, you can get it at http://sourceforge.net/projects/psyco/"""


from gettext import gettext as _

def checkenv():

	import sys, string
	ver = sys.version_info[0]*100+sys.version_info[1]*10+sys.version_info[2]
	if ver < 220:
		return _("""You're using an old version of Python interpreter (%s).
You should install Python 2.2.0 or newer.""") %(string.split(sys.version)[0])

	try:
		import pynicotine
	except ImportError,e:
		return _("""Can not find Nicotine modules. 
Perhaps they're installed in a directory which is not 
in an interpreter's module search path. 
(there could be a version mismatch between 
what version of python was used to build the Nicotine 
binary package and what you try to run Nicotine with.)""")

    # EEK! Scary Win32 stuff
	if win32:
		# Fetchs gtk2 path from registry
		try: import dbhash
		except: print _("Warning: the Berkeley DB module, dbhash, could not be loaded.")
		import _winreg
		import msvcrt
		try:
			k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "Software\\GTK\\2.0")
		except EnvironmentError:
			print _("You must install the Gtk+ 2.2 Runtime Environment to run this program")
			while not msvcrt.kbhit():
				pass
			sys.exit(1)
		else:
			gtkdir = _winreg.QueryValueEx(k, "Path")
			import os
			os.environ['PATH'] += ";%s/lib;%s/bin" % (gtkdir[0], gtkdir[0])

	try:
		if win32:
			try:
				import gtk
			except:
				import pygtk
		else:
			import pygtk
			pygtk.require("2.0")
	except:
		import sys
		return _("Can not find required PyGTK. The current search path is \n%s") % (sys.path)

	import gtk
	major, minor, micro = gtk.pygtk_version
	v = (major<<16) + (minor<< 8) + micro
	if v < ((1<<16) + (99<<8) + 16):
		return _("Your PyGTK is too old, upgrade to at least PyGTK 1.99.16")

	try:
		import ogg.vorbis
	except:
		try:
			import _vorbis
		except ImportError:
			print _("""You do not have Python Vorbis bindings installed. 
Others will not be able to see the lengths and the bitrates 
of Ogg Vorbis files that you share. You can get the from
http://www.andrewchatham.com/pyogg/. 
If you're using Debian, install the python-pyvorbis package.
""")

	try:
		import GeoIP
	except ImportError:
		try:
			import _GeoIP
		except ImportError:
			print _("""Nicotine supports a country code blocker but that
	requires a (GPL'ed) library called GeoIP. You can find it here:
	C library:       http://www.maxmind.com/app/c
	Python bindings: http://www.maxmind.com/app/python
	(the python bindings require the C library)
	""")
	
	return None
def version():
	try:
		import pynicotine.utils
		print _("Nicotine-Plus version %s" % pynicotine.utils.version )
	except ImportError, error:
		print _("Cannot find the pynicotine.utils module.")

def usage():
	print _("""Nicotine-Plus is a Soulseek client.
Usage: nicotine [OPTION]...
  -c file,	--config=file	Use non-default configuration file
  -t,		--enable-trayicon
  -d,		--disable-trayicon
  -v,		--version	Display version and exit
  -h,		--help		Display this help and exit

Please report any problems to our bugtracker :
http://www.nicotine-plus.org/newticket""")

if __name__ == '__main__':
	import locale
	try:
		locale.setlocale(locale.LC_ALL, '')
	except:
		print "Cannot set locale"

	import gettext
	gettext.textdomain("nicotine")

	import sys, getopt, os.path
	try:
		opts, args = getopt.getopt(sys.argv[1:], "hc:tdv", ["help", "config=", "profile", "enable-trayicon", "disable-trayicon", "version"])
	except getopt.GetoptError:
		# print help information and exit:
		usage()
		sys.exit(2)

	if win32:
		mydir,x = os.path.split(sys.argv[0])
		config = os.path.join(mydir, "config", "config")
	else:
		config = os.path.join(os.path.expanduser("~"),'.nicotine','config')
	profile = 0
	trayicon = 1
	for o, a in opts:
		if o in ("-h", "--help"):
			usage()
			sys.exit()
		if o in ("-c", "--config"):
			config = a
		if o == "--profile":
			profile = 1
		if o in ("-t", "--enable-trayicon"):
			trayicon=1
		if o in ("-d", "--disable-trayicon"):	
			trayicon=0
		if o in ("-v", "--version"):
			version()
			sys.exit()
	result = checkenv()
	if result is None:
		from pynicotine.gtkgui import frame

		app = frame.MainApp(config, trayicon)
		if profile:
			import hotshot
			log = os.path.expanduser(config) + ".profile"
			profiler = hotshot.Profile(log)
			print _("Starting using the profiler (saving log to %s)") % log
			profiler.runcall(app.MainLoop)
		else:
			app.MainLoop()
	else:
		print result
