#!python
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
      
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)

# 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)

