WARNING: custom added metadata and sharing properties will be 
   overwritten! 
*/
$fileStorageDir = getConfig($config, 'file_storage_dir', TRUE);
$cachePath = getConfig($config, 'cache_dir', TRUE);
/* each user has their own data directory, easy to link files
   to users in case the index breaks, so at least the files 
   themselves are not lost, only the metadata added by the user and
   sharing groups.
 */
$dbName = getConfig($config, 'db_name', TRUE);
$s = new Sag();
$dbs = $s->getAllDatabases()->body;
/* if db already exists, delete it */
if (in_array($dbName, $dbs)) {
    $s->deleteDatabase($dbName);
}
$s->createDatabase($dbName);
$s->setDatabase($dbName);
/* load all the map/reduce js functions from mapReduce directory */
$views = array();
foreach (glob("docs/mapReduce/*") as $mrFiles) {
    list($name, $type) = explode(".", basename($mrFiles));
    $views[$name][$type] = file_get_contents($mrFiles);
}
$view = array("_id" => "_design/files", "type" => "view", "language" => "javascript", "views" => $views);
// Add the view
$s->post($view);
// Delete all cache entries as to not accumulate too many
foreach (glob($cachePath . "/*") as $cacheEntry) {
    unlink($cacheEntry);