# Copyright 2016 ELIFE. All rights reserved.
# Use of this source code is governed by a MIT
# license that can be found in the LICENSE file.
from ctypes import CDLL

def get_libpath():
    """
    Get the library path of the the distributed inform binary.
    """
    import os
    import re
    from os.path import dirname, abspath, realpath, join
    from platform import system

    libre = re.compile(r"^inform-(\d+)\.(\d+)\.(\d+)$")

    root = dirname(abspath(realpath(__file__)))

    libdir = None
    major, minor, revision = 0, 0, 0
    for _, dirnames, _ in os.walk(root):
        for dirname in dirnames:
            match = libre.match(dirname) 
            if match:
                a, b, c = tuple(int(x) for x in match.group(1,2,3))
                if (major, minor, revision) < (a,b,c):
                    major, minor, revision = a, b, c
                    libdir = join(root, match.group())
        break

    if libdir is None:
        raise ImportError("cannot find libinform")

    if system() == 'Linux':
        return "{}/lib/libinform.so.{}.{}.{}".format(libdir,major,minor,revision)
    elif system() == 'Windows':
        return "{}/lib/inform.dll".format(libdir)
    else:
        raise RuntimeError("unsupported platform - \"{}\"".format(system()))

_inform = CDLL(get_libpath())

@@ -1031,31 +1031,61 @@
         
-return %22%7B%7D/lib/
+platform = %22linux-x86_64%22%0A        library = %22
 libinfor
@@ -1110,15 +1110,8 @@
 mat(
-libdir,
 majo
@@ -1154,15 +1154,14 @@
 == '
-Windows
+Darwin
 ':%0A 
@@ -1171,49 +1171,202 @@
     
-return %22%7B%7D/lib/inform.dll%22.format(libdir)
+platform = %22macosx-x86_64%22%0A        library = %22libinform.%7B%7D.%7B%7D.%7B%7D.dylib%22.format(major,minor,revision)%0A    elif system() == 'Windows':%0A        platform = %22win-amd64%22%0A        library = %22inform.dll%22
 %0A   
@@ -1450,16 +1450,75 @@
 em()))%0A%0A
+    return os.path.join(libdir, %22lib%22, platform, library)%0A%0A
 _inform 

Configure pyinform to support OS X at runtime