function [fullDst, currentDir] = cloneSetupDir(dirName)

    currentDir = pwd();

    dirBase = fileparts(dirName);
    dstBase = dirBase;

    if isempty(dirBase)
        dirBase = pwd();
        dstBase = dirBase;
    end

    fullSrc = fullfile(dirBase, dirName);
    

    if ~exist(fullSrc, 'dir')
        dirBase = fileparts(which(dirName));
        fullSrc = fullfile(dirBase, dirName);
    end

    if exist(fullSrc, 'dir')
        [~, toClone] = fileparts(fullSrc);
        
        suffixNum = 0;
        while true
            if suffixNum == 0
                suffix = "";
            else
                suffix = append('.',num2str(suffixNum));
            end
            name = fullfile(dstBase, append(toClone, suffix));
            if exist(name)
                suffixNum = suffixNum + 1;
                continue;
            end
            try
                fullDst = name;
                mkdir(fullDst);
                
                % Copy files with different extensions
                copyAllExt(fullSrc, fullDst, 'm');
                copyAllExt(fullSrc, fullDst, 'png');
                copyAllExt(fullSrc, fullDst, 'jpg');
                copyAllExt(fullSrc, fullDst, 'ppm');
                copyAllExt(fullSrc, fullDst, 'mat');
                copyAllExt(fullSrc, fullDst, 'txt');
                copyAllExt(fullSrc, fullDst, 'avi');
                copyAllExt(fullSrc, fullDst, 'mp4');
                copyAllExt(fullSrc, fullDst, 'sh');
                copyAllExt(fullSrc, fullDst, 'c');
                copyAllExt(fullSrc, fullDst, 'cu');
                copyAllExt(fullSrc, fullDst, 'cpp');
                copyAllExt(fullSrc, fullDst, 'hpp');
                copyAllExt(fullSrc, fullDst, 'h');
                copyAllExt(fullSrc, fullDst, 'mk');
                copyAllExt(fullSrc, fullDst, 'bat');
                copyAllExt(fullSrc, fullDst, 'zip');
                copyAllExt(fullSrc, fullDst, 'pgm');
                copyAllExt(fullSrc, fullDst, 'slx');
                copyAllExt(fullSrc, fullDst, 'JPEG');
                break;
            catch ME
                throwAsCaller(ME);
            end
        end
    else
        msg = sprintf("The directory '%s' does not exist.", fullSrc);
        error(msg);
    end
end

function copyAllExt(srcdir,dstdir,ext)
    allFiles = dir(fullfile(srcdir, ['*.' ext]));
    if ~isempty(allFiles)
        for i = 1:numel(allFiles)
            srcFile = fullfile(srcdir, allFiles(i).name);
            dstFile = fullfile(dstdir, allFiles(i).name);
            copyfile(srcFile, dstdir, 'f');
        end
    end
end


