#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright © 2007 Francisco Javier de la Peña
# Copyright © 2010 Francisco Javier de la Peña & Stefano Mazzucco
# Copyright © 2011 Francisco Javier de la Peña, Stefano Mazzucco & Michael Sarahan
#
# This file is part of Hyperspy.
#
# Hyperspy 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 2 of the License, or
# (at your option) any later version.
#
# Hyperspy 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 Hyperspy; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
# USA

import sys
import os, shutil
from distutils.version import StrictVersion
try:
    import argparse             # new in Python 2.7
    argp = True
except ImportError:
    argp = False
    import optparse

import IPython

import hyperspy
import hyperspy.Release

# First we split the argv items in two: the firsts for hyperspy, the rest for
# ipython
ipy_argv = [sys.argv[0],]
if '--ipython_args' in sys.argv:
    ipy_argv += sys.argv[sys.argv.index('--ipython_args') + 1:]
    sys.argv = sys.argv[:sys.argv.index('--ipython_args')]
    
hyperspy_module_path=os.path.dirname(hyperspy.__file__)
ipy_hspy = os.path.join(hyperspy_module_path, 'ipython_profile')
ipy_version = StrictVersion(IPython.__version__)
ipy_011 = StrictVersion('0.11')
if ipy_version < ipy_011:
    from IPython.genutils import get_home_dir, get_ipython_dir
    from IPython.iplib import user_setup
    ipy_dir = get_ipython_dir()
    to_copy = [os.path.join(ipy_hspy, 'ipy_profile_hyperspy.py') ,]
    destination = ipy_dir
    if not os.path.isdir(ipy_dir):
        # Platform-dependent suffix.
        if os.name == 'posix':
            rc_suffix = ''
        else:
            rc_suffix = '.ini'
        # Ise the IPython function to create the ipy_dir
        user_setup(ipy_dir, rc_suffix, mode='install', interactive=False)
        sys.argv.append('--overwrite_profile')
else:
    ipy_dir = IPython.utils.path.get_ipython_dir()
    to_copy = [ os.path.join(ipy_hspy, 'ipython_config.py') ,
                os.path.join(ipy_hspy, 'ipython_qtconsole_config.py'), ]
    destination = os.path.join(ipy_dir, 'profile_hyperspy')
    # The ipy_dir may not exist if it is the first time that the user runs
    # ipython, therefore we must create it, what is done automatically when 
    # creating a profile
    if not os.path.isdir(ipy_dir) or not os.path.isdir(destination):
        # Use the IPython routines to create the directory and profile
        import IPython.core.profileapp
        ipy_create_profile = IPython.core.profileapp.ProfileCreate()
        ipy_create_profile.parse_command_line(['hyperspy',])
        ipy_create_profile.init_config_files()
        sys.argv.append('--overwrite_profile')
        
if argp is True:
    parser = argparse.ArgumentParser(add_help=True, 
                                     version=hyperspy.Release.version,
                                     description = hyperspy.Release.description)
    parser.add_argument('--no_pylab', action="store_true", default=False)
    parser.add_argument('--overwrite_profile', action="store_true", 
    default=False, help = 'Overwrite the Ipython profile with the default one')
    parser.add_argument('--ipython_args', nargs='*', 
    help='Arguments to be passed to IPython. This option must be the last one.' 
    'Look at the IPython documentation for available options.')
    args = parser.parse_args()
    for f in to_copy:
        if not os.path.isfile(os.path.join(destination, os.path.split(f)[-1]))\
                or args.overwrite_profile:
            shutil.copy(f, destination)
else:
    parser = optparse.OptionParser( version=hyperspy.Release.version,
                                    description = hyperspy.Release.description)
    parser.add_option('--no_pylab', action="store_true", default=False)
    parser.add_option('--overwrite_profile', action="store_true", 
    default=False, help = 'Overwrite the Ipython profile with the default one')
    parser.add_option('--ipython_args', nargs='*', 
    help='Arguments to be passed to IPython. This option must be the last one.' 
    'Look at the IPython documentation for available options.')
    (options, args) = parser.parse_args()
    for f in to_copy:
        if not os.path.isfile(os.path.join(destination, os.path.split(f)[-1]))\
                or options.overwrite_profile:
            shutil.copy(f, destination)

# Now that the hspy arguments are parsed, we can delete them from sys.argv
sys.argv = ipy_argv

# By default Hyperspy uses the wx backend, but if the user tries to choose a 
# different one using --pylab= , then we grant him the control        
add_pylab = True
if argp is True:
    if args.no_pylab is True:
        add_pylab = False
else:
    if options.no_pylab is True:
        add_pylab = False

if ipy_version < ipy_011:
    if add_pylab is True:
        sys.argv.append('-pylab')
    add_wx = True
    for arg in sys.argv:
        if 'thread' in arg:
            add_wx = False
            break
    if add_wx is True:
        sys.argv.append('-wthread') 
    sys.argv.extend(('-p', 'hyperspy'))       
    from IPython.ipapi import launch_new_instance
    sys.exit(launch_new_instance())
else:
    from IPython.frontend.terminal.ipapp import launch_new_instance
    sys.argv.append('--profile=hyperspy')
    for arg in sys.argv:
        if 'pylab' in arg:
            add_pylab = False
    if add_pylab is True:
        sys.argv.append('--pylab=wx')
    sys.exit(launch_new_instance())
    
