#!/usr/bin/python -i
"""
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 one
or more of:
  -s  Silent initialization (does not print startup messages)
  -n  No splash screen during startup
  -v  Set verbosity level (may be repeated to increase verbosity)
  -m  Run command line wrapper to provide extra capabilities (default)
  -i  Do not run command line wrapper, just run standard Python front end
  -h  Print this message

Long versions of options:
  -s  --silent
  -n  --nosplash
  -v  --verbose
  -m  --commandwrapper=yes
  -i  --commandwrapper=no
  -h  --help
"""

# $Id: pyraf 742 2006-05-17 13:19:27Z jmiller $
#
# R. White, 2000 January 21

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

absPyrafDir = os.path.abspath(os.path.join(pyrafDir,'..'))
if absPyrafDir not in sys.path: sys.path.insert(0, absPyrafDir)
del absPyrafDir, pyrafDir

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

# read the user's startup file (if there is one)

if os.environ.has_key("PYTHONSTARTUP") and \
                os.path.isfile(os.environ["PYTHONSTARTUP"]):
    execfile(os.environ["PYTHONSTARTUP"])

from pyraf import doCmdline, _use_ipython_shell, iraf, __version__
from pyraf.irafpar import makeIrafPar
from pyraf.irafglobals import yes, no, INDEF, EOF
logout = quit = exit = 'Use ".exit" to exit'

# IPython Pyraf profile RC installation
if _use_ipython_shell:
    import pyraf
    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:
    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]


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
	sys.argv = ["ipython","-p","pyraf"]
	IPython.Shell.start().mainloop()
	sys.exit()
    else:
        import pyraf.pycmdline
        del _use_ipython_shell
        _pycmdline = pyraf.pycmdline.PyCmdLine(locals=globals())
        _pycmdline.start()
else:
    del doCmdline
