classdef EmbedPayloadglnxa64 < compiler.internal.EmbedPayload
    properties (Access=protected, Constant)
        PrebuiltInstaller = fullfile(matlabroot, 'toolbox','compiler', lower(computer('arch')), 'setup');
    end
    methods (Access=public)
        function installerFile = embed(obj, installerFile, bundleFileName, installAgentLocator, ~, ~, ~, ~)
            % Zip up the payload
            files = {bundleFileName, installAgentLocator};
            if exist(installerFile, 'file')
                delete(installerFile);
            end
            loc = fileparts(installerFile);
            payloadFile = fullfile(loc, '__MW_INTERNAL_PAYLOAD_bundle.zip');
            zip(payloadFile, files);
            plf = onCleanup(@()deleteFileOrFolder(payloadFile));
            if ~exist(payloadFile, 'file')
                error(message('Compiler:installer:couldNotCreatePayloadFile'));
            end
            tempInstaller = tempname(loc);

            tif = onCleanup(@()deleteFileOrFolder(tempInstaller));
            % Append the payload to the installer file
            [a,b]=system(['cat "' obj.PrebuiltInstaller '" "' payloadFile '" > "' tempInstaller '"']);
            if a ~= 0
                error(message('Compiler:installer:failedToAppendPayload', b));
            end
            % Change the zip offset to account for the setup program at
            % the head of the zip file
            [a,~]=system(['zip -A "' tempInstaller '"']);
            if a ~= 0
                warning(message('Compiler:installer:unableToFixZipOffset'));
            end
            movefile(tempInstaller, installerFile);
            % make sure the installer is executable
            fileattrib(installerFile, '+x');
        end 
    end
end

