#
# This setup file installs the main script and modules of the MS_Inspect
# software.
#
# Every CASA distribution uses its own Python (currently Python 2.7).
# This means that the imported modules must be installed into the CASA's
# Python subdirectory. For example, my CASA directory is
# /home/benkev/Downloads/Packages/casa-release-4.7.0-el7
# and installing of new modules is done by copying them to the special
# subdirectory "site-packages", namely
# /home/benkev/Downloads/Packages/ccasa/casa-release-4.5.0-el6/lib/ ...
#                   ... python2.7/site-packages/
# Since we usually use local copies of the CASA, we do not need any
# administrator's privileges to install. However, the python location under
# the CASA directory is not standard, and therefore needs to be indicated
# at the installation command with the use of its option '--prefix='.
#
# To install MS_Inspect, run the following command on your machine, replacing 
# my directory in --prefix with yours:
#
# $ python setup.py install --prefix=~/Downloads/Packages/casa-release-4.7.0-el7
#
# For example, Meagan on reu13.haystack.mit.edu, if she intends to install
# MS_Inspect for CASA 4.7.0, should issue the following:
#
# python setup.py install --prefix=/home/mcrowley/bin/casa-release-4.7.0-1-el6
#
# To install MS_Inspect for CASA 4.6.0, the command is
#
# python setup.py install --prefix=/home/mcrowley/bin/casa-release-4.6.0-el6
#
# The setup.py also copies the main script, ms_inspect.py, to the local
# directory  ~/bin. If ~/bin directory does not exist, it is created,
# and added to PATH. The startup file ~/.bashrc is modified. It is appended
# with the line
#
# export PATH=$PATH:/home/$USER/bin
#
# If PATH already has /home/$USER/bin, then ~/.bashrc is left intact.
#
# After setup.py finished, ms_inspect.py is accessible from any directory. For
# example, I do it as follows:
#
# $ cd arbitrary_dir
# $ casa
# $ execfile('/home/benkev/bin/ms_inspect.py')
#
#
# Author: Leonid Benkevitch
# Date: May 1, 2017
# Modified: Oct 23, 2017
#


import distutils
from   distutils.core import setup
import os, sys


setup(name='MS_Inspect',
      version='1.0',
      description='A GUI to inspect CASA Measurement Sets (MS)',
      author='Leonid Benkevitch',
      author_email='benkev@haystack.mit.edu',
      url='http://www.haystack.mit.edu/',
      py_modules=['Graph_Panel', 'Vis_Panel', 'Clopha_Panel', 'Cloamp_Panel', \
                  'RectSelect', 'CreateTips', 'Selection_Histogram', \
                  'phase_closures', 'amplitude_closures', 'selections', \
                  'closures', 'baselines'],
     )

user = os.path.expanduser('~')         # Smth like '/home/mcrowley'
user_bin = user + '/bin'               # Smth like '/home/mcrowley/bin'
#
# Create  ~/bin directory.
# If ~/bin directory already exists, do not create it.
#
if not os.path.isdir(user_bin):
    os.mkdir(user_bin)
    print user_bin, ' directory created'
#
# Add ~/bin directory to the PATH environmental variable and
# append ~/.bashrc with a line of code to add ~/bin to PATH,
# if $PATH does not already have it
#
path_list = os.environ['PATH'].split(':')
bash_userfile='.bashrc'
if sys.platform.count('darwin'): # I.e. if sys.platform contains word 'darwin'
    bash_userfile='.bash_profile'
    
    ## with open(init_py_name, 'a') as init_py:
    ##     init_py.write('\n')
    ##     init_py.write('import sys' + '\n')
    ##     init_py.write('sys.path.append("/Applications/CASA.app/lib/' \
    ##                                     'python2.7/site-packages")' + '\n')

    print
    print '=== to Darwin platform users ==='
    print 'Create file /Users/$USER/.casa/init.py, if it does not exist.'
    print 'Add to it the following two lines:'
    print 'import sys'
    print 'sys.path.append("/Applications/CASA.app/lib/' \
                                   'python2.7/site-packages")'
    print


if not (user_bin in path_list):
    with open(user + '/' + bash_userfile, 'a') as bashrc:
        bashrc.write('\n')
        bashrc.write('export PATH=$PATH:' + user_bin + '\n')
        

#
# Copy the main program ms_inspect.py to the ~/bin directory
#
os.system('cp ms_inspect.py ' + user_bin)
print     'cp ms_inspect.py ' + user_bin

#
# $ find ~ -type d -name "casa-release*"
#
## casa_dirs = []
## for root, dirs, files in os.walk(user):
##     for d in dirs:
##         if d.find('casa-release') <> -1:
##             casa_dirs.append(root + '/' + d)
##             print 'CASA release found at %s' % casa_dirs[-1]

#
# Or search the whole system:
# $ find / -type d -name "casa-release*" 2>/dev/null
#





