function str = sptfileheader(varargin)
%SPTFILEHEADER Utility for generating a file header
%   SPTFILEHEADER Generates a file header with no custom annotations for
%   the signal processing toolbox using '%' as the commenting character.
%
%   SPTFILEHEADER(DESC) Generates a file header and adds the description
%   DESC before the toolbox information.  If DESC is less than 10
%   characters it will be on the same line as the toolbox information.
%
%   SPTFILEHEADER(DESC, TBX) Generates a file header for the toolbox
%   specified in the string TBX.
%
%   SPTFILEHEADER(DESC, TBX, COM) Generates a file header using the
%   COM as the commenting character.
%
%   SPTFILEHEADER(DESC, TBX, COM, DATEFORM) Generates a file header using
%   the specified DATEFORM.  See DATESTR for more information.  DATEFORM is
%   0 by default.
%
%   If DESC or TBX is '', the defaults will be used.

%   Copyright 1988-2012 The MathWorks, Inc.

desc     = '';
tbx      = 'signal';
comment  = '%';
dateform = 0;

if ~isempty(varargin)
    if isnumeric(varargin{end})
        dateform      = varargin{end};
        varargin(end) = [];
    end
end
if ~isempty(varargin)
    desc = varargin{1};
end
if length(varargin) > 1
    tbx = varargin{2};
end
if length(varargin) > 2
    comment = varargin{3};
end

% Get the toolbox info which includes the name and version.
minfo = ver('matlab');
tinfo = ver(tbx);
tinfo = tinfo(1); % In case there are multiple toolboxes on the path.

% If the description is short and does not end with a '.' we want to
% attach it to the 'generated by' line.  Otherwise it goes on its own line.
if isempty(desc)
    descStr=desc;
    str1 = sprintf('Generated by MATLAB(R) %s and %s %s.', ...
        minfo.Version, tinfo.Name, tinfo.Version);
elseif length(desc) < 10 && desc(end) ~= '.'
    descStr = '';
    str1 = sprintf('%s generated by MATLAB(R) %s and %s %s.',...
        desc, minfo.Version, tinfo.Name, tinfo.Version);
else
    descStr = sprintf(' %s\n%s', desc,comment);
    str1 = sprintf('Generated by MATLAB(R) %s and %s %s.', ...
        minfo.Version, tinfo.Name, tinfo.Version);
end


% Make sure the "generated on/at/in" makes sense.
switch dateform
    case {3, 4, 5, 10, 11, 12, 17, 18, 27, 28}
        str2 = sprintf('%s Generated in: %s', comment, ...
            datestr(now, dateform));
    case {13, 14, 15, 16}
        str2 = sprintf('%s Generated at: %s', comment, ...
            datestr(now, dateform));
    otherwise
        str2 = sprintf('%s Generated on: %s', comment, ...
            datestr(now, dateform));
end

str = sprintf('%s%s %s\n%s', comment, descStr, str1, str2);

