function [ mission ] = getmission( syslog, xst, varargin )


global vehicle DeploymentName

time = xst.time;
z = xst.z;


% pl = plot missions (true/false)
if nargin>2
    pl = true;
else
    pl = false;
end


if ~isempty(syslog)
    
    mission_start = syslog(contains(syslog,'Started mission'));
    
    stime = syslog2datenum(mission_start);
    n = size(stime,1);
    
    % Add end of log timestamp
    while size(stime,1)~=n+1
        lastsize = size(stime,1);
        try
            stime(n+1,:)  = syslog2datenum(syslog(end));
        catch
            syslog(end) = [];
            continue
        end
    end; clear n;
    
    mname = cell(size(mission_start));
    for k=1:numel(mission_start)
        
        dataS = mission_start{k};
        mi = strfind(dataS,'Started mission') + 15; % length('Started mission')
        mname{k} = strtrim(dataS(mi:end));
        
    end
    mname{end+1} = mname{end};
    
    % Prepend "Unknown" mission if the first mission starts mid-log
    if time(1)<stime(1)
        
        stime = vertcat(time(1),stime);
        mname = vertcat({'Unknown'},mname);
        
    end
    
    
    u = unique(mname, 'stable');
    Xname = cell(size(z));
    X = NaN(length(u),length(z));
    m = mname(1);
    for q = 2:length(stime)
        
        ti = (time>=stime(q-1) & time<=stime(q));
        
        Xname(ti) = m;
        X(strcmp(u,m),ti) = z(ti);
        m = mname(q);
        
    end
    
    
    mission.time  = time;
    mission.z     = X;
    mission.name  = Xname;
    mission.namelist = u;
    
else
    
    warning('Could not retrieve mission names: syslog is empty or no server connection')
    mission.time  = time;
    mission.z     = z;
    mission.name  = [];
    mission.namelist = [];
    
end

% plot missions
%
if pl && ~isempty(mission.name)
    plotmissions(time, mission, vehicle, DeploymentName);
    %{
    if sv==1
        % Script source water-mark
        %-----------------------------------------------------------------
        uicontrol('Style', 'text','String', [fname ' - ' datestr(clock)],...
            'Units','normalized',...
            'Position', [0 0 1 0.025]);
        print2pdf(gcf,['~/Desktop/' Deployment '_missions'])
    end
    %}
end
%}
end
