#!/usr/bin/env python
##    Fonty Python Copyright (C) 2006, 2007, 2008 Donn.C.Ingle
##    Contact: donn.ingle@gmail.com - I hope this email lasts.
##
##    This file is part of Fonty Python.
##    Fonty Python 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.
##
##    Fonty Python 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 Fonty Python.  If not, see <http://www.gnu.org/licenses/>.

"""
Dec 2007
This wraps fp and catches the segfaults that kill it.
A window displays the situation and gives the user some hope.
"""

import subprocess, sys, os


## find the directory from where THIS script 
## is actually being run.
root = __file__
if os.path.islink(root):
    root = os.path.realpath(root)
fontyroot = os.path.dirname(os.path.abspath(root)) 

import fontypythonmodules.i18n as i18n

## Get the dir to run this from:
#root = os.path.dirname(sys.argv[0]) # note:
# this returns /usr/bin when we run from an installed 
# version of fonty. 
# Since I am testing from my dev directory, and sometimes
# make a link in /usr/bin to fake things:
# (sudo ln -s `pwd`/fontypython /usr/bin/fontypython)
# I want the actual site-root where fontypythonmodules/ is
# so I will use fontyroot from above

##os.chdir( fontyroot ) # note:
# Not changing dir anymore because this makes relative
# directory stuff like:
# fontypython .
# not work properly. User should be able to open a view
# on the current directory.
# Thus:
# Point to the absolute path of the 'fp' script.
c1 = [os.path.join(fontyroot,'fp') ]

## Append any args
for arg in sys.argv[1:]: c1.append( arg )

p1 = subprocess.call( c1 )
if p1 == 0:
    print
    print _("Thanks for trying Fonty Python!")
elif p1 == 1:
    print "Some other error happened"
else: # err code -11
    #print "ERROR CODE:", p1
    ## This actually works!
    import sys, os, locale
    import fontypythonmodules.pathcontrol as PC
    import wx, fontypythonmodules.dialogues as D

    ## Setup wxPython to access translations : enables the stock buttons.
    localedir = "fontypythonmodules/locales"
    langid = wx.LANGUAGE_DEFAULT
    mylocale = wx.Locale( langid )

    iPC = PC.PathControl() 

    def getSegfaulter():
        """
        What was the last font to segfault PIL?
        """
        paf = os.path.join( iPC.appPath(),"lastFontBeforeSegfault")
        culprit = None
        try:
            f = open( paf, "r" )
            culprit = f.readline()[:-1]
            f.close()
        except:
            raise
        return culprit
        
    ## Start the main frame and then show it.
    class App(wx.App) :
        def OnInit(self) :
            culprit = getSegfaulter()
            dlg = D.SegfaultDialog(None, culprit)
            val = dlg.ShowModal()
            if val == wx.ID_OK:
                print _("Please restart Fonty Python after you have moved:\"%s\" to some other place.") % culprit
            dlg.Destroy()
            return True
    app = App(0) 
    app.MainLoop() 

