#! python
"""
Copyright (C) 2003 Association of Universities for Research in Astronomy
(AURA)
See LICENSE.txt in the docs directory of the source distribution for the
terms of use.

Usage: pyraf [options] [savefile]

where savefile is an optional save file to start from, and options are:
  -c cmd  Command passed in as string (any valid PyRAF command)
  -e      Turn on ECL mode
  -h      Print this message
  -i      No command line wrapper, just run standard interactive Python shell
  -m      Run command line wrapper to provide extra capabilities (default)
  -n      No splash screen during startup (also see -x)
  -s      Silent initialization (does not print startup messages)
  -V      Print version info and exit
  -v      Set verbosity level (may be repeated to increase verbosity)
  -x      No graphics will be attempted/loaded during session
  -y      Run the IPython shell instead of the normal PyRAF command shell

Long versions of options:
  -c  --comand=<cmd>
  -e  --ecl
  -h  --help
  -i  --commandwrapper=no
  -m  --commandwrapper=yes
  -n  --nosplash
  -s  --silent
  -V  --version
  -v  --verbose
  -x  --nographics
  -y  --ipython
"""

# $Id$
#
# R. White, 2000 January 21

from __future__ import division
import sys, os, shutil

# set search path to include directory above this script and current directory
# ... but do not want the pyraf package directory itself in the path, since
# that messes things up by allowing direct imports of pyraf submodules
# (bypassing the __init__ mechanism.)

# follow links to get to the real executable filename
executable = sys.argv[0]
while os.path.islink(executable):
    executable = os.readlink(executable)
pyrafDir = os.path.dirname(executable)
del executable
try:
    sys.path.remove(pyrafDir)
except ValueError:
    pass
del pyrafDir

if "." not in sys.path: sys.path.insert(0, ".")

# allow the use of PYRAF_ARGS
extraArgs = os.getenv('PYRAF_ARGS', '').strip()
extraArgsList = [x for x in extraArgs.split(' ') if len(x)]
if len(extraArgsList):
    sys.argv.extend(extraArgsList)
x = None
del extraArgs, extraArgsList, x

# handle any warning supression right away, before any more imports
if '-s' in sys.argv or '--silent' in sys.argv:
    import warnings
    warnings.simplefilter("ignore")
    del warnings

# allow them to specifiy no graphics, done before any imports
if '-x' in sys.argv or '--nographics' in sys.argv:
    os.environ['PYRAF_NO_DISPLAY'] = '1' # what the rest of PyRAF triggers on
    if '-x' in sys.argv: # keep the option parsing simpler when we get to it
        sys.argv.remove('-x')
    else:
        sys.argv.remove('--nographics')

# read the user's startup file (if there is one)
if 'PYTHONSTARTUP' in os.environ and \
                os.path.isfile(os.environ["PYTHONSTARTUP"]):
    exec(compile(open(os.environ["PYTHONSTARTUP"]).read(), os.environ["PYTHONSTARTUP"], 'exec'))

#------------------------------------------------------------------------------
# In next line we get into pyraf's __init__.py - this does ALL KINDS OF THINGS
#------------------------------------------------------------------------------
from pyraf import doCmdline, _use_ipython_shell, runCmd, __version__
# By now, the bulk of the startup work is done
from pyraf import iraf
from pyraf.irafpar import makeIrafPar
from stsci.tools.irafglobals import yes, no, INDEF, EOF
logout = quit = exit = 'Use ".exit" to exit'

# IPython Pyraf profile RC installation - this is only done so that the next
# step (below) can find/run the rc file (to execute some pyraf magic setup)
if _use_ipython_shell:
    # the following should be removed soon, it is only used for versions
    # of IPython older than 0.11
    import pyraf
    home = None
    ip = os.getenv("IPYTHONDIR")
    if not ip:
        home = os.getenv("HOME") or ""
        ip = os.path.join(home, ".ipython")

    pyrafrc_dest = os.path.join(ip,"ipythonrc-pyraf")
    pyrafrc_source = os.path.join(os.path.split(pyraf.__file__)[0],
                                  "ipythonrc-pyraf")
    if os.path.exists(ip):
        if not os.path.exists(pyrafrc_dest):
            shutil.copy(pyrafrc_source, pyrafrc_dest)
    else:
        os.mkdir(ip)
        shutil.copy(pyrafrc_source, pyrafrc_dest)
    del pyraf, home, ip, pyrafrc_dest, pyrafrc_source
else:
    if '-s' not in sys.argv and '--silent' not in sys.argv:
       print("PyRAF"+' '+__version__+' '+"Copyright (c) 2002 AURA")
       # just print first line of Python copyright (long in v2.0)
       print("Python"+' '+sys.version.split()[0]+' '+sys.copyright.split('\n')[0])

# Run given command
if runCmd:
    iraf.task(cmd_line=runCmd, IsCmdString=1)
    iraf.cmd_line()
    sys.exit()
else:
    del runCmd

# Start command line
if doCmdline:
    del doCmdline
    # Start up command line wrapper keeping definitions in main name space
    # Keep the command-line object in namespace too for access to history
    if _use_ipython_shell:
        import sys
        import IPython

        # rewrite sys.argv
        new_argv = ["ipython",]
        if hasattr(IPython, 'Shell'): # old versions
           if '-s' in sys.argv or '--silent' in sys.argv: new_argv.append("-nobanner")
           new_argv.append("-p")
           new_argv.append("pyraf")
        else:
           if '-s' in sys.argv or '--silent' in sys.argv: new_argv.append("--no-banner")
        sys.argv = new_argv[:]; del new_argv

        if hasattr(IPython, 'Shell'): # old IPython versions
           IPython.Shell.start(user_ns=globals()).mainloop()
        else:
           # Start the interactive shell.  Also, see IPython.embed() here:
           #     http://ipython.org/ipython-doc/stable/interactive/ \
           #     reference.html#embedding-ipython
           try:
              # new location of terminal as of v1.*
              from IPython.terminal.ipapp import TerminalIPythonApp
           except:
              from IPython.frontend.terminal.ipapp import TerminalIPythonApp
# was:     from ..... import TerminalInteractiveShell
           app = TerminalIPythonApp.instance()
           app.initialize()
           # import pyraf to write this shell obj to its namespace
           # we have already fully imported it above
           import pyraf
           # create and run the Ipython shell - it takes over from here
           pyraf._ipyshell = app.shell
# was:     pyraf._ipyshell = TerminalInteractiveShell(user_ns=globals())
           # in the end, with this run_code(), all of this is the equivalent
           # of simply running ipython standalone with these imports
           pyraf._ipyshell.run_code('from pyraf import iraf, ipython_api; from stsci.tools.irafglobals import INDEF, Verbose, yes, no')
           app.start()
# was:     pyraf._ipyshell.mainloop()
        sys.exit()
    else:
        import pyraf.pycmdline
        del _use_ipython_shell
        _pycmdline = pyraf.pycmdline.PyCmdLine(locals=globals())
        if '-s' in sys.argv or '--silent' in sys.argv:
           _pycmdline.start('') # use no banner
        else:
           _pycmdline.start() # use default banner
else:
    del doCmdline
    # run the standard Python interpreter
    import code
    code.interact(local=locals())
