syntax = "proto2";

package biomaj.download;

message File {
    // Name of the file
    required string name = 1;
    // Location on remote server, defaults to root directory
    optional string root = 2;
    // Save file under different name
    optional string save_as = 3;
    // Specific file url
    optional string url = 4;

    message MetaData {
        optional string permissions = 1;
        optional string group = 2;
        optional int64 size = 3;
        optional string hash = 4;
        optional int32 year = 5;
        optional int32 month = 6;
        optional int32 day = 7;
        optional string format = 8;
        optional string md5 = 9;
        optional int64 download_time = 10;
    }

    optional MetaData metadata = 5;

}

message FileList {
    repeated File files = 1;
}

message Operation {

    enum OPERATION {
        LIST = 0;
        DOWNLOAD = 1;
        PROCESS = 2;
    }

    required OPERATION type = 1;
    optional DownloadFile download = 2;
    optional Process process = 3;
    message Trace {
        required string trace_id = 1;
        required string span_id = 2;
    }

    optional Trace trace = 4;
}

message Process {
    required string exec =  1;
}

message DownloadFile {
    required string bank = 1;
    required string session = 2;

    required string local_dir = 3;

    optional int32 timeout_download = 4;

    enum Protocol {
        FTP = 0;
        FTPS = 1;
        HTTP = 2;
        HTTPS = 3;
        DIRECTFTP = 4;
        DIRECTHTTP = 5;
        DIRECTHTTPS = 6;
        LOCAL = 7;
        RSYNC = 8;
        IRODS = 9;
        DIRECTFTPS = 10;
    }

    message Param {
        required string name = 1;
        required string value = 2;
    }

    message HttpParse {
        required string dir_line = 1 [default = '<img[\\s]+src="[\\S]+"[\\s]+alt="\\[DIR\\]"[\\s]*/?>[\\s]*<a[\\s]+href="([\\S]+)/"[\\s]*>.*([\\d]{2}-[\\w\\d]{2,5}-[\\d]{4}\\s[\\d]{2}:[\\d]{2})'];
        required string file_line = 2 [default = '<img[\\s]+src="[\\S]+"[\\s]+alt="\\[[\\s]+\\]"[\\s]*/?>[\\s]<a[\\s]+href="([\\S]+)".*([\\d]{2}-[\\w\\d]{2,5}-[\\d]{4}\\s[\\d]{2}:[\\d]{2})[\\s]+([\\d\\.]+[MKG]{0,1})'];
        required int32 dir_name = 3 [default = 1];
        required int32 dir_date = 4 [default = 2];
        required int32 file_name = 5 [default = 1];
        required int32 file_date = 6 [default = 2];
        optional string file_date_format = 7;
        required int32 file_size = 8 [default = 3];
    }

    enum HTTP_METHOD {
        GET = 0;
        POST = 1;
    }

    message RemoteFile {
        repeated File files = 1;
        required Protocol protocol = 2;
        required string server = 3;
        required string remote_dir = 4;
        optional string save_as = 5;
        repeated Param param = 6;
        optional HttpParse http_parse = 7;
        optional string credentials = 8;
        repeated string matches = 9;
    }

    required RemoteFile remote_file = 5;


    message Proxy {
        required string proxy = 1;
        optional string proxy_auth = 2;
    }

    optional Proxy proxy = 6;

    optional HTTP_METHOD http_method = 8 [ default = GET];
    
    map<string, string> protocol_options = 9;

}
