#!/usr/bin/env python3

#  -------------------------------------------------------------------
#  
#  Copyright (C) 2006-2020, Andrew W. Steiner
#  
#  This file is part of O2sclpy.
#  
#  O2sclpy 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 3 of the License, or
#  (at your option) any later version.
#  
#  O2sclpy 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 O2sclpy. If not, see <http://www.gnu.org/licenses/>.
#  
#  -------------------------------------------------------------------

import sys
import ctypes
from ctypes.util import find_library
import platform
import os
import numpy

o2scl_libdir=''
o2scl_cpplib=''
backend=''

def force_bytes2(obj):
  """
  In cases where we're unsure whether or not obj is
  a string or bytes object, we ensure it's a bytes
  object by converting if necessary.
  """
  if isinstance(obj,numpy.bytes_)==False and isinstance(obj,bytes)==False:
    return bytes(obj,'utf-8')
  return obj

yt_found=False
for i in range(1,len(sys.argv)):
  if sys.argv[i]=='-o2scl-libdir':
    if i>=len(sys.argv)-1:
      print('Option -o2scl-libdir specified with no value.')
    else:
      o2scl_libdir=sys.argv[i+1]
      print('Set o2scl-libdir to',o2scl_libdir)
  elif sys.argv[i]=='-o2scl-cpplib':
    if i>=len(sys.argv)-1:
      print('Option -o2scl-cpplib specified with no value.')
    else:
      o2scl_cpplib=sys.argv[i+1]
      print('Set o2scl-cpplib to',o2scl_cpplib)
  elif sys.argv[i]=='-backend':
    if i>=len(sys.argv)-1:
      print('Option -backend specified with no value.')
    else:
      backend=sys.argv[i+1]
      print('Set backend to',backend)

for i in range(1,len(sys.argv)):
  if sys.argv[i][0:4]=='-yt-' and yt_found==False:
      if backend!='' and backend!='agg' and backend!='Agg':
        print('Backend was not set to Agg but yt comments were found.')
      yt_found=True
      backend='Agg'
      print("yt commands found. Setting backend to 'Agg'.")

# Future: it appears we may need to fix the string comparisons in the
# code below and force conversion to byte strings (even though for now
# this code seems to work)
      
if platform.system()=='Darwin':

  if (o2scl_cpplib=='' and os.getenv('O2SCL_CPPLIB') is not None and
      force_bytes2(os.getenv('O2SCL_CPPLIB'))!=b'None'):
    o2scl_cpplib=os.getenv('O2SCL_CPPLIB')
    print('Set o2scl-cpplib to',o2scl_cpplib)

  if o2scl_cpplib!='':
    systcpp=ctypes.CDLL(o2scl_cpplib,mode=ctypes.RTLD_GLOBAL)

  rl=ctypes.CDLL('/usr/lib/libreadline.dylib',
                 mode=ctypes.RTLD_GLOBAL)
    
  if (o2scl_libdir=='' and os.getenv('O2SCL_LIB') is not None and
      force_bytes2(os.getenv('O2SCL_LIB'))!=b'None'):
    o2scl_libdir=os.getenv('O2SCL_LIB')
    print('Set o2scl-libdir to',o2scl_libdir)
    
  if o2scl_libdir!='':
    o2scl=ctypes.CDLL(o2scl_libdir+'/libo2scl.dylib',
                      mode=ctypes.RTLD_GLOBAL)
    o2scl_hdf=ctypes.CDLL(o2scl_libdir+'/libo2scl_hdf.dylib',
                          mode=ctypes.RTLD_GLOBAL)
  else:
    o2scl=ctypes.CDLL('libo2scl.dylib',mode=ctypes.RTLD_GLOBAL)
    o2scl_hdf=ctypes.CDLL('libo2scl_hdf.dylib',mode=ctypes.RTLD_GLOBAL)

else:

  stdcpp=ctypes.CDLL(find_library("stdc++"),mode=ctypes.RTLD_GLOBAL)
  
  if (o2scl_libdir=='' and os.getenv('O2SCL_LIB') is not None and
      force_bytes2(os.getenv('O2SCL_LIB'))!=b'None'):
    o2scl_libdir=os.getenv('O2SCL_LIB')
    print('Set o2scl-libdir to',o2scl_libdir)

  if o2scl_libdir=='':
    o2scl=ctypes.CDLL(find_library("o2scl"),mode=ctypes.RTLD_GLOBAL)
    o2scl_hdf=ctypes.CDLL(find_library("o2scl_hdf"),
                          mode=ctypes.RTLD_GLOBAL)
  else:
    o2scl=ctypes.CDLL(o2scl_libdir+'/libo2scl.so',mode=ctypes.RTLD_GLOBAL)
    o2scl_hdf=ctypes.CDLL(o2scl_libdir+'/libo2scl_hdf.so',
                          mode=ctypes.RTLD_GLOBAL)
    
if backend!='':
  import matplotlib
  matplotlib.use(backend)

import o2sclpy
gc=o2sclpy.o2graph_plotter()
gc.parse_argv(sys.argv,o2scl_hdf)

