# -*- Mode: Shell-Script -*-  Not really, but shows comments correctly
#_PYTHON_INSERT_SAO_COPYRIGHT_HERE_(2007,2009)_
#_PYTHON_INSERT_GPL_LICENSE_HERE_
#
# SHERPA_VERSION 40401


#***************************************************************************
#
# Configuration file for ipython -- ipythonrc format
#
# The format of this file is one of 'key value' lines.
# Lines containing only whitespace at the beginning and then a # are ignored
# as comments. But comments can NOT be put on lines with data.
#***************************************************************************


# include base config and only add some extras
include ipythonrc

# Move current working directory from front to back of sys.path
# so that loading sherpa, etc. not fooled if file names like
# "sherpa.py" exist in the current working directory
execute import sys; sys.path.append(sys.path[0]); del sys.path[0]

# Import os. Necessary for the load preference call below
import_mod os

# Version checks for ChIPS and CRATES
execute sherpa_version = 0
execute chips_version = 0
execute chips_version_str = '0'
execute crates_version = 0
execute crates_version_str = '0'
execute sherpa_path = ''
execute exec("try:\n\tfrom pycrates import __version__ as crates_version\nexcept:\n\tpass") 
execute exec("try:\n\tfrom pycrates import __versionstr__ as crates_version_str\nexcept:\n\tpass")
execute exec("try:\n\tfrom pychips import __version__ as chips_version\nexcept:\n\tpass") 
execute exec("try:\n\tfrom pychips import __versionstr__ as chips_version_str\nexcept:\n\tpass")
execute exec("try:\n\tfrom sherpa import __version__ as sherpa_version\nexcept:\n\tpass")
execute exec("try:\n\tfrom sherpa import __file__ as sherpa_path\n\tsherpa_path = os.path.dirname(sherpa_path)\nexcept:\n\tpass") 
execute site_path = sherpa_path.replace('/sherpa','',-1)

# Warn of out-of-date CRATES
execute if sherpa_version > 0 and crates_version < 40401: print "Warning: Importing CRATES version " + crates_version_str +"; CRATES version 4.4.1 is now available,\n consider upgrading.\n"

# Warn of out-of-date ChIPS
execute if sherpa_version > 0 and chips_version < 40401: print "Warning: Importing ChIPS version " + chips_version_str +"; ChIPS version 4.4.1 is now available,\n consider upgrading.\n"

# Import CRATES and ChIPS, including HLUI for each
import_all pychips pycrates pychips.hlui pycrates.hlui

# Load in chips preferences
execute set_preference_autoload(True)

# Import Sherpa UI -- do it this way to fix ./sherpa.py problem
execute from sherpa.astro.ui import *

# Restore current working directory to front of sys.path
execute sys.path = [sys.path.pop()] + sys.path

# Behave a bit more like old Sherpa
autocall 2
banner 0
confirm_exit 0
nosep 1
xmode Plain

# Make the prompt look like old Sherpa
prompt_in1 'sherpa-\#> '
prompt_in2 '       '
prompt_out ''
colors NoColor


# Register a custom exception handler
execute __ciao_ahelp_context__ = 'py.sherpa'
execute __ciao_app_name__ = "sherpa"

# For Sherpa v4.2 and higher- load ipython_cxc.py and ahelp_interface.py
# but for older versions of Sherpa load ciao-config.py
execute exec("if sherpa_version >= 40201:\n\ttry:\n\t\texecfile(site_path+'/ipython_cxc.py')\n\texcept:print 'Warning: unable to load ipython_cxc.py from '+site_path\n\ttry:\n\t\t\texecfile(site_path+'/ahelp_interface.py')\n\texcept:print 'Warning: unable to load ahelp_interface.py from '+site_path\nelse:\n\ttry:\n\t\texecfile(sherpa_path+'/ciao-config.py')\n\texcept:print 'Warning: unable to load ciao-config.py from '+sherpa_path")
execute del sherpa_version; del chips_version; del crates_version
execute del chips_version_str; del crates_version_str
execute del sherpa_path; del site_path; 

# Replace native help text, for Sherpa HLUI functions, with pointer to
# ahelp text for these functions.
execute for func in get_functions(): exec('try:\n\teval(\''+func+'\').__doc__="Type ahelp(\''+func+'\') for more details."\nexcept:\n\tpass')
