 ################################################
 #
 # Example use:
-# var dir = wesabe.io.dir.open('/test');
-# if (dir.exists()) {
-#  alert(wesabe.io.dir.path(dir));
-#  var arr = wesabe.io.dir.read(dir, true), i;
-#  if (arr) {
-#    for (i = 0; i < arr.length; ++i) {
-#      alert(arr[i].path);
-#    }
-#  }
-# }
-# else {
-#  var rv = wesabe.io.dir.create(dir);
-#  alert('Directory create: ' + rv);
-# }
+# path = dir.open('/test')
+# if path.exists()
+#   alert dir.path path
+#   children = dir.read path, true
+#   if children
+#     for child in children
+#       alert child
+# else
+#   path = dir.create dir
+#   alert "Directory create: #{path}"
 #
 # ---------------------------------------------
 # ----------------- Nota Bene -----------------
 # ---------------------------------------------
 # Some possible types for get are:
 #  'ProfD'       = profile
-#  'DefProfRt'     = user (e.g., /root/.mozilla)
+#  'DefProfRt'   = user (e.g., /root/.mozilla)
 #  'UChrm'       = %profile%/chrome
 #  'DefRt'       = installation
-#  'PrfDef'        = %installation%/defaults/pref
-#  'ProfDefNoLoc'    = %installation%/defaults/profile
+#  'PrfDef'      = %installation%/defaults/pref
+#  'ProfDefNoLoc'= %installation%/defaults/profile
 #  'APlugns'     = %installation%/plugins
-#  'AChrom'        = %installation%/chrome
+#  'AChrom'      = %installation%/chrome
 #  'ComsD'       = %installation%/components
-#  'CurProcD'      = installation (usually)
+#  'CurProcD'    = installation (usually)
 #  'Home'        = OS root (e.g., /root)
 #  'TmpD'        = OS tmp (e.g., /tmp)
 
-wesabe.io.dir =
-  sep        : '/'
-  dirservCID : '@mozilla.org/file/directory_service;1'
-  propsIID   : Components.interfaces.nsIProperties
-  fileIID    : Components.interfaces.nsIFile
+{open} = require 'io/file'
 
+
+sep        = if navigator.platform.toLowerCase().indexOf('win') > -1 then '\\' else '/'
+dirservCID = '@mozilla.org/file/directory_service;1'
+propsIID   = Components.interfaces.nsIProperties
+fileIID    = Components.interfaces.nsIFile
+
+_read = (dirEntry, recursive) ->
+  list = []
+  while dirEntry.hasMoreElements()
+    list.push(dirEntry.getNext().QueryInterface(Components.interfaces.nsILocalFile))
+
+  if recursive
+    list2 = []
+    for item in list
+      if item.isDirectory()
+        files = item.directoryEntries
+        list2 = _read(files, recursive)
+
+    list = list.concat(list2)
+
+  return list
+
+
+$dir =
   get: (type) ->
     try
-      dir = Components.classes[@dirservCID].createInstance(@propsIID).get(type, @fileIID)
-      return dir
+      Components.classes[dirservCID].createInstance(propsIID).get(type, fileIID)
     catch e
       return false
 
   create: (dir) ->
     try
-      dir.create(0x01, 0774)
+      dir.create 0x01, 0774
       return true
     catch e
       return false
 
   read: (dir, recursive=false) ->
     list = []
-    wesabe.tryCatch "wesabe.io.dir.read(#{dir})", =>
+    wesabe.tryCatch "dir.read(#{dir})", =>
       if dir.isDirectory()
         files = dir.directoryEntries
-        list = @_read(files, recursive)
-    return list
-
-  _read: (dirEntry, recursive) ->
-    list = []
-    while dirEntry.hasMoreElements()
-      list.push(dirEntry.getNext().QueryInterface(Components.interfaces.nsILocalFile))
-
-    if recursive
-      list2 = []
-      for item in list
-        if item.isDirectory()
-          files = item.directoryEntries
-          list2 = @_read(files, recursive)
-
-      list = list.concat(list2)
-
+        list = _read files, recursive
     return list
 
   unlink: (dir, recursive=false) ->
     try
-      dir.remove(recursive)
+      dir.remove recursive
       return true
     catch e
       return false

    str.split(/\/|\\/).join(join)

  join: (str, split) ->
    str.split(split).join(@sep)


wesabe.io.dir.__defineGetter__ 'profile', ->
  @get('ProfD')

wesabe.io.dir.__defineGetter__ 'chrome', ->
  @get('AChrom')

wesabe.io.dir.__defineGetter__ 'root', ->
  file = wesabe.io.file.open(@chrome.path + '/../../')
  file.normalize()
  return file

wesabe.io.dir.__defineGetter__ 'tmp', ->
  @get('TmpD')


wesabe.io.dir.sep = '\\' if navigator.platform.toLowerCase().indexOf('win') > -1

################################################
################################################
#
