function [destModelH, errMsg] = copyModel(destName, srcModelH)
%copyModel - Create a copy of a model in a new file

%   Copyright 2006-2021 The MathWorks, Inc.
%     


    destModelH = [];
    errMsg = [];
    
    opts = sldvdefaultoptions;
    opts.OutputDir = '.';
    MakeOutputFilesUnique = 'off';
    
    destModelFullPath = Sldv.utils.settingsFilename(destName, MakeOutputFilesUnique, ...
        '$ModelExt$', srcModelH, false, true, opts);                

    if isempty(destModelFullPath)
        errMsg = message('Sldv:SldvMergeHarness:CannotGenerate',destName);
        return;
    end
        
    [~, destmodel] = fileparts(destModelFullPath); 
    destmodel  = sldvshareprivate('cmd_check_for_open_models', destmodel, MakeOutputFilesUnique, false);
    if isempty(destmodel)
        errMsg = message('Sldv:SldvMergeHarness:CannotGenerate',destName);
        return;
    end
    
    modelToCopyloc = get_param(srcModelH,'location');
    modelToCopyfileName = get_param(srcModelH,'filename'); 
    copyfile(modelToCopyfileName,destModelFullPath);       
    
    status = fileattrib(destModelFullPath,'+w');        
    if ~status
        delete(destModelFullPath);
        errMsg = message('Sldv:SldvMergeHarness:CannotGenerate',destName);
        return;
    end
    
    % Copy the multi simulation design study file (if present) for the
    % Signal Editor harness before loading the destination model to avoid
    % the warning.
    if Sldv.HarnessUtils.isMultiSimDesignStudyExist(srcModelH)
        [fileDir, srcName,~] = fileparts(modelToCopyfileName);
        srcHarnessDesignStudyFile = [fileDir filesep srcName '_ds.mldatx'];
        fileDir = fileparts(destModelFullPath);
        destHarnessDesignStudyFile = [fileDir filesep destName,'_ds.mldatx'];
        copyfile(srcHarnessDesignStudyFile,destHarnessDesignStudyFile);
        status = fileattrib(destHarnessDesignStudyFile,'+w');
        if ~status
            delete(destHarnessDesignStudyFile);
            errMsg = message('Sldv:SldvMergeHarness:CannotGenerate',destName);
            return;
        end
    end
    
    try
        Sldv.load_system(destModelFullPath);
    catch Mex
         delete(destModelFullPath);
         errMsg = Mex;         
    end
    
    if ~isempty(errMsg)
        return;
    end
    
    destModelH = get_param(destmodel,'Handle');
    
    set_param(destModelH, 'location', [modelToCopyloc(1),(modelToCopyloc(2)+modelToCopyloc(4))/2,...
        modelToCopyloc(3),modelToCopyloc(4)+(modelToCopyloc(4)-modelToCopyloc(2))/2]);
    
    %if this is a signaleditor block, make a copy of the MAT file as well
    %to avoid affecting the source model.
    [harnessSource,error] = Sldv.harnesssource.Source.getSource(destmodel);

    if isempty(error) && isa(harnessSource,'Sldv.harnesssource.SignalEditor')
        srcHarnessInputFile = get_param(harnessSource.blockH,'FileName');
        fileDir = fileparts(srcHarnessInputFile);
        destHarnessInputFileName = [destName,'_HarnessInputs.mat'];
        destHarnessInputFile = fullfile(fileDir,destHarnessInputFileName);
        copyfile(srcHarnessInputFile,destHarnessInputFile);
        status = fileattrib(destHarnessInputFile,'+w');
        if ~status
            delete(destHarnessInputFile);
            errMsg = message('Sldv:SldvMergeHarness:CannotGenerate',destName);
            return;
        end
        
        set_param(harnessSource.blockH,'FileName',destHarnessInputFile);
    end
    
end
