From 302df7368680f42f6dd9b333843d0ede61f12554 Mon Sep 17 00:00:00 2001
From: John Benediktsson <mrjbq7@gmail.com>
Date: Sat, 16 Nov 2013 16:09:53 -0800
Subject: [PATCH] alien.libraries.finder.windows: more like MSDN search order.

---
 .../libraries/finder/windows/platforms.txt    |  1 +
 .../libraries/finder/windows/windows.factor   | 32 ++++++++++++-------
 2 files changed, 22 insertions(+), 11 deletions(-)
 create mode 100644 basis/alien/libraries/finder/windows/platforms.txt

diff --git a/basis/alien/libraries/finder/windows/platforms.txt b/basis/alien/libraries/finder/windows/platforms.txt
new file mode 100644
index 0000000000..8e1a55995e
--- /dev/null
+++ b/basis/alien/libraries/finder/windows/platforms.txt
@@ -0,0 +1 @@
+windows
diff --git a/basis/alien/libraries/finder/windows/windows.factor b/basis/alien/libraries/finder/windows/windows.factor
index cef5b54a05..1ebfa6ed34 100644
--- a/basis/alien/libraries/finder/windows/windows.factor
+++ b/basis/alien/libraries/finder/windows/windows.factor
@@ -1,22 +1,32 @@
-! Copyright (C) 2013 Björn Lindqvist
+! Copyright (C) 2013 Björn Lindqvist, John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: alien.libraries alien.libraries.finder arrays combinators
-kernel sequences system ;
+USING: alien.libraries.finder arrays combinators.short-circuit
+environment io.files io.files.info io.pathnames kernel sequences
+splitting system system-info.windows ;
 
 IN: alien.libraries.finder.windows
 
 <PRIVATE
 
-: candidate-paths ( name -- paths )
-    {
-        [ ".dll" append ]
-        [ "lib" ".dll" surround ]
-        [ "dlls/" ".dll" surround ]
-        [ "dlls/lib" ".dll" surround ]
-    } cleave 4array ;
+: search-paths ( -- seq )
+    "resource:" system-directory windows-directory 3array
+    "PATH" os-env [ ";" split ] [ f ] if* append ;
+
+: candidate-paths ( name -- seq )
+    search-paths over ".dll" tail? [
+        [ prepend-path ] with map
+    ] [
+        [
+            [ prepend-path ]
+            [ [ ".dll" append ] [ prepend-path ] bi* ] 2bi
+            2array
+        ] with map concat
+    ] if ;
 
 PRIVATE>
 
 M: windows find-library
-    candidate-paths [ dlopen dll-valid? ] map-find nip ;
+    candidate-paths [
+        { [ exists? ] [ file-info regular-file? ] } 1&&
+    ] map-find nip ;