# -*- coding: utf-8 -*-

import ctypes
import ctypes.util
import os
import sys


def find_libc():
    if sys.platform == 'win32':
        return ctypes.util.find_msvcrt()
    else:
        return ctypes.util.find_library('c')


def load_library(name):
    lname = platform_libname(name)
    sdirs = platform_libdirs()

    # First attempt to utilise the system search path
    try:
        return ctypes.CDLL(lname)
    # Otherwise, if this fails then run our own search
    except OSError:
        for sd in sdirs:
            try:
                return ctypes.CDLL(os.path.abspath(os.path.join(sd, lname)))
            except OSError:
                pass
        else:
            raise OSError('Unable to load {0}'.format(name))


def platform_libname(name):
    if sys.platform == 'darwin':
        return 'lib{0}.dylib'.format(name)
    elif sys.platform == 'win32':
        return '{0}.dll'.format(name)
    else:
        return 'lib{0}.so'.format(name)


def platform_libdirs():
    path = os.environ.get('PYFR_LIBRARY_PATH', '')
    dirs = [d for d in path.split(':') if d]

    # On Mac OS X append the default path used by MacPorts
    if sys.platform == 'darwin':
        return dirs + ['/opt/local/lib']
    # Otherwise just return
    else:
        return dirs

@@ -253,105 +253,299 @@
     
-lname = platform_libname(name
+# If an explicit override has been given then use it%0A    lpath = os.environ.get('PYFR_%7B0%7D_LIBRARY_PATH'.format(name.upper()))%0A    if lpath:%0A        return ctypes.CDLL(lpath
 )%0A
+%0A
     
-sdirs = platform_libdirs()%0A%0A    # First attempt to utilise 
+# Otherwise synthesise the library name and start searching%0A    lname = platform_libname(name)%0A%0A    # Start wi
 th
-e
  sys
@@ -613,18 +613,13 @@
   # 
-Otherwise,
+..and
  if 
@@ -695,13 +695,26 @@
  in 
-s
+platform_lib
 dirs
+()
 :%0A  

Enable library paths to be explicitly specified.