#!/usr/local/bin/python
# -*- coding: utf-8 -*-
#
#       gimagereader
#       
#       Copyright 2010 Sandro Mani <manisandro@gmail.com>
#       
#       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/>.

try:
    # Change the process name.
    import ctypes
    libc = ctypes.CDLL('libc.so.6')
    libc.prctl(15, 'gimagereader', 0, 0)
except:
    pass

import os
import sys

basedir=os.path.dirname(os.path.abspath(sys.argv[0]))

# Redirect stderr if frozen by p2exe
if hasattr(sys, 'frozen'):
	try:
		sys.stderr=open(os.path.join(basedir,'..','gimagereader.exe.log'),'w')
	except:
		sys.stderr=open(os.devnull,'a')

# Add gtklib directory to path for bundled GTK installation on windows
if os.path.isdir(os.path.join(os.path.dirname(basedir),'win32','gtklib','bin')):
	os.environ['DICTDIR']=os.path.join(os.path.dirname(basedir),'win32','gtklib','share','myspell','dicts')
	os.environ['PATH']=os.path.join(os.path.dirname(basedir),'win32','gtklib','bin')+';'+os.environ['PATH']
elif os.path.isfile(os.path.join(basedir,'gimagereader.exe')):
	os.environ['DICTDIR']=os.path.join(basedir,'..','share','myspell','dicts')
	os.environ['PATH']=basedir+';'+os.environ['PATH']

try:
	import poppler	# Needs to be loaded once _before_ gtk since otherwise python on windoze crash-exits (why??)
	import gtk
	import gobject
except Exception, e:
	print >> sys.stderr, "Could not load GTK modules: %s"%(str(e))
	sys.exit(0)

if __name__ == '__main__':
	if os.path.isfile(os.path.join(basedir,'..','src','gimagereader.xml')): # Running from source dir
		try:
			sys.path.append(os.path.join(basedir,'..','src'))
			import main
		except Exception, e:
			print "Error loading modules:", e
			sys.exit(0)
	else:	# Try running with installed modules
		try:
			import gimagereader.main as main
		except Exception, e:
			print "Failed to load gImageReader modules, check your installation:", e
			sys.exit(0)

	# Parse arguments
	if len(sys.argv) > 1:
		if sys.argv[1]=='--help' or sys.argv[1]=='-h':
			print 'Usage: '+sys.argv[0]+' [file1 [file2 [...] ] ]'
			sys.exit(0)
		else:
			gobject.threads_init()
			instance=main.gimagereader(os.path.dirname(main.__file__),sys.argv[1:])
	else:
		gobject.threads_init()
		instance=main.gimagereader(os.path.dirname(main.__file__))
	
	try:
		gtk.main()
	except KeyboardInterrupt:
		print '\b\bExiting on KeyboardInterrupt...'
		sys.exit(0)

