function runprop = lsfSingleZippedTask(runprop)
% lsfSimpleParallelTask - decode function used for independent jobs on a non-shared filesystem.

% Copyright 2005-2019 The MathWorks, Inc.

% We are being called with no shared file system - so we must unwrap and
% make the relevant directories for the file storage - we assume we are in
% something like /tmp where we have write access
JOB_ID  = getenv('LSB_JOBID');
TASK_ID = getenv('LSB_JOBINDEX');

% Get a function handle to set status messages for lsf on this particular
% task - this will set the default message handler to be lsfSet
if ~isempty(parallel.internal.logging.getWorkerDebugSetting())
    aHandler = parallel.internal.apishared.LsfUtils.messageHandler( JOB_ID, TASK_ID );
    setSchedulerMessageHandler(aHandler);
end
dctSchedulerMessage(2, 'In lsfSingleZippedTask with JOB_ID : %s and TASK_ID : %s', JOB_ID, TASK_ID);

% Create a new directory to store the job and task files needed to run this
% specific task.  This directory should not be shared between tasks.
storageLocation = [JOB_ID '.' TASK_ID '.mps'];
jobZipFile  = [JOB_ID '.' TASK_ID '.Job.zip'];
taskZipFile = [JOB_ID '.' TASK_ID '.Task.zip'];

% Make the storageLocation.
dctSchedulerMessage(2, 'About to create local storage location : %s and unzip Job zip file : %s', storageLocation, jobZipFile);
mkdir(storageLocation);

% Unzip the Job info into the storage location.
dctSchedulerMessage(2, 'About to unzip Job zip file %s into storage location %s', jobZipFile, storageLocation);
unzip(jobZipFile, storageLocation);

% Delete the job zip file.
iTryToDeleteZipFileNoError(jobZipFile);

% Now add the tasks to storage
dctSchedulerMessage(2, 'About to unzip Task zip file %s into storage location %s', taskZipFile, storageLocation);
unzip(taskZipFile, storageLocation);

% Delete the Task zip file.
iTryToDeleteZipFileNoError(taskZipFile);

% Need to tell the job runner where it's dependency directory is
dependencyDir = [tempdir JOB_ID '.' TASK_ID '.mps.temp'];

storageConstructor = parallel.internal.cluster.getParallelServerEnv('PARALLEL_SERVER_STORAGE_CONSTRUCTOR');
jobLocation        = parallel.internal.cluster.getParallelServerEnv('PARALLEL_SERVER_JOB_LOCATION');
taskLocation = [jobLocation filesep 'Task' TASK_ID];

% Create empty job and task files, if necessary
parallel.internal.cluster.FileStorage.createDefaultTaskFilesIfAbsent([storageLocation filesep taskLocation]);

set(runprop, ...
    'StorageConstructor', storageConstructor, ...
    'StorageLocation', storageLocation, ...
    'JobLocation', jobLocation, ....
    'TaskLocation', taskLocation, ...
    'DependencyDirectory', dependencyDir, ...
    'LocalSchedulerName', 'parallel.cluster.LSF', ...
    'FallbackSchedulerName', 'parallel.cluster.TaskRunner', ...
    'HasSharedFilesystem', false);
end

function iTryToDeleteZipFileNoError(zipFile)
try
    dctSchedulerMessage(2, 'About to delete zip file %s', zipFile);
    delete(zipFile);
catch err
    dctSchedulerMessage(1, 'Error occured whilst deleting zip file:\n%s', err.message);
end
end
