/**
 * @fileoverview <p>Package loading utility module. In the top-level namespace
 * for Tusk compatibility.</p>
 */

var system = require('system');
var fs = require('fs');

module.shared = true;

export('load', 'normalize', 'catalog');

var catalog;

function load() {
    if (catalog) {
        return;
    }

    catalog = {};
    // loop through packages and configure resources
    var packages = fs.join(system.prefix, 'packages');

    if (!fs.isDirectory(packages)) {
        print("No packages directory, skipping package setup");
        return;
    }

    for each (var pkg in fs.list(packages)) {

        var directory = fs.join(packages, pkg);
        if (!fs.isDirectory(directory)) {
            continue;
        }

        try {
            var jsonPath = fs.join(directory, "package.json");
            var package = JSON.parse(fs.read(jsonPath));
            package.directory = directory;

            // check engine specific libs
            handleResource(package.engines || "engines", function(engines) {
                var dir = fs.join(directory, engines);
                for each (var engine in system.engines) {
                    var path = fs.join(dir, engine, "lib");
                    if (fs.isDirectory(path)) {
                        require.paths.push(path);
                    }
                }
            });

            var lib = (package.directories && package.directories.lib) || package.lib || "lib";
            handleResource(lib, function(lib) {
                require.paths.push(fs.join(directory, lib));
            });

            var jars = (package.directories && package.directories.jars) || package.jars;
            handleResource(jars, function(res) {
                var path = fs.join(directory, res);
                var files = fs.isDirectory(path)
                    ? [fs.join(path, x) for each (x in fs.list(path)) if (x.match(/\.jar$/))]
                    : [path]
                ;
                for each (var file in files)
                    addToClasspath(file);
            });

            catalog[pkg] = package;

        } catch (error) {
            system.stderr.print("Error configuring package " + pkg + ": " + error);
        }
    }
}

function normalize(catalog) {
    // TODO
}

// helper function to set up package resources
function handleResource(res, callback) {
    if (Array.isArray(res)) {
        res.forEach(callback);
    } else if (res) {
        callback(res);
    }
}

@@ -167,16 +167,54 @@
 e('fs');
+%0Avar engine = require('ringo/engine');
 %0A%0Amodule
@@ -439,40 +439,58 @@
 s = 
-fs.join(system.prefix, '
+engine.getRingoHome().getChildRepository(%22
 packages
 ');%0A
@@ -485,17 +485,17 @@
 packages
-'
+%22
 );%0A%0A    
@@ -503,31 +503,16 @@
 f (!
-fs.isDirectory(
 packages
 )) %7B
@@ -503,24 +503,32 @@
 f (!packages
+.exists(
 )) %7B%0A       
@@ -547,25 +547,25 @@
 ackages 
-directory
+installed
 , skippi
@@ -629,49 +629,8 @@
 var 
-pkg in fs.list(packages)) %7B%0A%0A        var 
 dire
@@ -639,26 +639,19 @@
 ory 
-= fs.jo
 in
-(
+ 
 packages
 , pk
@@ -650,89 +650,29 @@
 ages
-, pkg);%0A        if (!fs.isDirectory(directory)) %7B%0A            continue;%0A        %7D
+.getRepositories()) %7B
 %0A%0A  
@@ -706,32 +706,24 @@
  jsonPath = 
-fs.join(
 directory, %22
@@ -719,18 +719,29 @@
 irectory
-, 
+.getResource(
 %22package
@@ -790,25 +790,89 @@
 rse(
-fs.read(jsonPath)
+jsonPath.getContent());%0A            var name = package.name %7C%7C directory.getName(
 );%0A 
@@ -1059,24 +1059,16 @@
 r dir = 
-fs.join(
 director
@@ -1072,17 +1072,37 @@
 tory
-, 
+.getChildRepository(%22
 engines
+%22
 );%0A 
@@ -1193,30 +1193,42 @@
 h = 
-fs.join(dir, 
+dir.getChildRepository(
 engine
-,
+ +
  %22
+/
 lib%22
@@ -1564,24 +1564,16 @@
 hs.push(
-fs.join(
 director
@@ -1573,18 +1573,36 @@
 irectory
-, 
+.getChildRepository(
 lib));%0A 
@@ -1780,88 +1780,113 @@
 var 
-path = fs.join(directory, res);%0A                var files = fs.isDirectory(path)
+files;%0A                var path = directory.getChildRepository(res);%0A                if (path.exists()) %7B
 %0A   
@@ -1906,27 +1906,18 @@
     
-? %5Bfs.join(path, x)
+files = %5Bp
  for
@@ -1927,33 +1927,49 @@
 ch (
-x
+p
  in 
-fs.list(path)) if (x
+path.getResources()) if (p.getName()
 .mat
@@ -1982,16 +1982,17 @@
 jar$/))%5D
+;
 %0A       
@@ -2004,20 +2004,74 @@
     
-    : %5Bpath%5D
+%7D else %7B%0A                    files = %5Bdirectory.getResource(res)%5D;
 %0A   
@@ -2083,17 +2083,17 @@
         
-;
+%7D
 %0A       
@@ -2209,19 +2209,20 @@
 catalog%5B
-pkg
+name
 %5D = pack
@@ -2320,11 +2320,17 @@
 %22 + 
-pkg
+directory
  + %22

Use repository instead of filesystem API to make packages work with non-file resources