/*********************************************************/
/* BASIC REPOSITORY CONFIGURATION.
/* To add a repository, create a new shared-folders and choose
/*   the access with web-based management interface
/*********************************************************/

{
use esmith::AccountsDB;

my $a = esmith::AccountsDB->open_ro();

my $homedir = $pydio{'HomeDir'} || 'none';
my $metastore = $pydio{'Metastore'} || 'serial';

if ($metastore eq 'xattr'){
    $metastore = '"metastore.xattr" => array(),';
}
else {
    $metastore =<<'EOF';
"metastore.serial" => array(
                                "METADATA_FILE_LOCATION" => "outside",
                        ),
EOF
}

my $adm = $pydio{'AdminInterface'} || 'disabled';

if (($homedir eq 'enabled') || ($homedir eq 'users')){
    $OUT .=<<"EOF";

\$REPOSITORIES["home"] = array(
        "DISPLAY" => "Perso",
        "AJXP_SLUG" => "home",
        "DRIVER" => "fs",
        "DRIVER_OPTIONS" => array(
                "PATH" => "/home/e-smith/files/users/AJXP_USER/home",
                "CREATE" => false,
                "RECYCLE_BIN" => '',
                "CHMOD_VALUE" => '0660',
                "DEFAULT_RIGHTS" => "",
                "PAGINATION_THRESHOLD" => 500,
                "PAGINATION_NUMBER" => 200,
                "META_SOURCES" => array(
                )
        ),
);

EOF

}


foreach my $share ($a->get_all_by_prop(type=>'share')){
  my $name = $share->key;
  my $access = $share->prop('Pydio') || 'disabled';
  next unless ($access eq 'enabled');
  my $desc = $share->prop('Name') || $name;
  my $recycledir = (($share->prop('RecycleBin') || 'disabled') eq 'disabled') ? 
      '' : $share->prop('RecycleBinDir') || 'Recycle Bin';
  my $history = $share->prop('PydioHistory') || 'disabled';
  $history = ($history eq 'enabled') ? '"meta.git" => array(),':'';
  my $content = $share->prop('PydioIndexContent') || 'disabled';
  $content = ($content eq 'enabled') ? 'true':'false';
  $OUT .=<<"EOF";

\$REPOSITORIES["$name"] = array(
        "DISPLAY" => "$name",
        "AJXP_SLUG" => "$name",
        "DRIVER" => "fs",
        "DRIVER_OPTIONS" => array(
                "PATH" => "/home/e-smith/files/shares/$name/files/",
                "USER_DESCRIPTION" => "$desc",
                "CREATE" => false,
                "RECYCLE_BIN" => '$recycledir',
                "CHMOD_VALUE" => '0660',
                "DEFAULT_RIGHTS" => "",
                "PAGINATION_THRESHOLD" => 500,
                "PAGINATION_NUMBER" => 200,
                "META_SOURCES" => array(
                       $metastore
                       $history
                       "meta.user" => array (
                                "meta_fields" => "tags,css_label,anything_area",
                                "meta_labels" => "Tags,Type,Note",
                        ),
                       "meta.filehasher" => array(),
                       "meta.watch" => array(),
                       "index.lucene" => array(
                                "index_content" => $content,
                                "index_meta_fields" => 'tags,anything_area'
                       )
                )
        ),
);

EOF

}

if (($pydio{'PublicShares'} || 'enabled') eq 'enabled'){
    $OUT .=<<'EOF';

$REPOSITORIES["ajxp_shared"] = array(
        "DISPLAY"               =>      "Shared Elements",
        "DISPLAY_ID"            =>      "363",
        "DRIVER"                =>      "ajxp_shared",
        "DRIVER_OPTIONS"=> array(
                "DEFAULT_RIGHTS" => ""
        )
);

EOF

}

$OUT .=<<'EOF';

// ADMIN REPOSITORY
/*

the configuration is managed by the SME Server templates system
New repositories can be added using the smeserver-shared-folders
contrib. Use the admin interface of Pydio with care, and expect
your customization to be lost next time tempaltes are expanded.
Only usefull for debuging
EOF

$OUT .= '*/' if ($adm eq 'enabled');

$OUT .=<<'EOF';

$REPOSITORIES["ajxp_conf"] = array(
        "DISPLAY" => "Settings",
        "DISPLAY_ID" => "165",
        "DRIVER" => "ajxp_conf", 
        "DRIVER_OPTIONS"=> array()      
);

EOF
$OUT .= '*/' unless ($adm eq 'enabled');

$OUT .=<<'EOF';

$REPOSITORIES["ajxp_user"] = array(
        "DISPLAY" => "Dashboard",
        "DISPLAY_ID" => "166",
        "DRIVER" => "ajxp_user",
        "DRIVER_OPTIONS"=> array()
);

EOF
}
