/**
 * Main process for searching for updates
 * 
 * @param type $params
 * 
 * @return type
 */
function searchForUpdates($params)
{
    // Initialisation du nom du répertoire de destination
    $catalogDir = transformPath($params['targetDir'], $params['version']);
    $modulesDir = $catalogDir . 'files/';
    if ($params['archiveModules']) {
        $archiveDir = transformPath($params['archiveDir'], $params['version']);
    }
    // Récupération du catalogue
    $catalog = getCatalog($params['catalogUrl'], $params['version']);
    // XMLisation du fichier de plugins
    $xml = simplexml_load_string($catalog);
    //debug($xml);
    // Recherche des modules
    $moduleGroups = array();
    // Recherche des modules sans groupes
    // On les place dans un tableau 'no_group'
    foreach ($xml->module as $xmlModule) {
        $moduleGroups['no_group']['modules'][] = getModule($xmlModule, $params['modulesUrl']);
    }
    // Recherche des groupes de modules
    foreach ($xml->module_group as $xmlModuleGroup) {
        $moduleGroups[] = getModuleGroup($xmlModuleGroup, $params['modulesUrl']);
    }
    // Récupération de la liste des plugins déjà téléchargés
    if ($params['archiveModules']) {
        $existingPlugins = glob($modulesDir . '*.{nbm,jar}', GLOB_BRACE);
    }
    //debug($existingPlugins);
    // Téléchargement des plugins
    $plugins = array();
    $plugins['downloaded'] = array();
    $plugins['not_downloaded'] = array();
    $plugins['seen'] = array();
    $plugins['removed'] = array();
    foreach ($moduleGroups as $moduleGroup) {
        foreach ($moduleGroup['modules'] as $module) {
            // Si le plugin existe déjà, on ne le télécharge pas
            if (!file_exists($modulesDir . $module['file']['folder'] . $module['file']['filename']) || filesize($modulesDir . $module['file']['folder'] . $module['file']['filename']) != $module['size']) {
                $plugins['downloaded'][] = $module;
            } else {
                $plugins['not_downloaded'][] = $module;
            }
            $plugins['seen'][] = $module['file']['folder'] . $module['file']['filename'];
        }
    }
    // Détermination des plugins qui ne sont plus dans le catalogue
    if ($params['archiveModules']) {
        foreach ($existingPlugins as $existingPlugin) {
            $existingPlugin = extractFileName($existingPlugin, $params['modulesUrl']);
            if (!in_array($existingPlugin['filename'], $plugins['seen'])) {
                $plugins['removed'][] = $existingPlugin['filename'];
            }
        }
    }
    //debug($plugins);
    return $plugins;
}
        function getCatalog($par = 0)
        {
            global $ST, $cat;
            $yml = '';
            $rs = $ST->select("SELECT * FROM sc_shop_catalog c WHERE parentid={$par} AND export=1");
            while ($rs->next()) {
                $yml .= '<category id="' . $rs->getInt('id') . '"' . ($rs->getInt('parentid') ? ' parentId="' . $rs->getInt('parentid') . '"' : '') . '>' . $rs->get('name') . '</category>
					';
                $cat[] = $rs->getInt('id');
                $yml .= getCatalog($rs->getInt('id'));
            }
            return $yml;
        }
    getCatalogItem();
    getMedia();
    getOrgVdc();
    getOrgNet();
    getTask();
    getEvent();
    getUser();
    getRole();
    buildSummary();
} elseif ($type == "orguser") {
    getVApp();
    getVAppTemplate();
    getVAppNetwork();
    getVM();
    getVMNetwork();
    getCatalog();
    getCatalogItem();
    getMedia();
    getOrgVdc();
    getOrgNet();
    getTask();
    getEvent();
    getUser();
    buildSummary();
} else {
    $service->logout();
    echo "Invalid report type!\n";
    exit(1);
}
// log out
$service->logout();