function install_R2011aJaDoc(zip_file)
%
% INSTALL_R2011AJADOC Installs the R2011a+ Japanese Documentation Web
% Download package.
%
% INSTALL_R2011AJADOC(ARCHIVE) installs the R2011a+ Japanese Documentation Web
% Download package ARCHIVE.


%   Copyright 2011 The MathWorks, Inc.
%   $Revision: 1.1 $  $Date: 2011/03/12 16:34:22 $

% first check if the jvm is available
if (~usejava('jvm'))
    error('install_R2011aJaDoc requires Java to run.')
end

% check args
if nargin < 1
    zip_file = 'R2011a_ja_doc.r2011a.zip';
end

% check if archive exists
if (exist(zip_file, 'file') ~= 2)
    error('Archive %s does not exist.\nInstallation failed.', zip_file)
end

% obtain addon name, ver and arch from zip_file name
[zip_file_path, zip_file_name, zip_file_ext] = fileparts(zip_file);
[addon_name, remain] = strtok(zip_file_name, '.');
[addon_rel, remain] = strtok(remain, '.');
addon_arch = strtok(remain, '.');
% strip off 'r' prefix from addon_rel
addon_rel = strtok(addon_rel, 'r');

msg = sprintf('Installing %s...', addon_name);
disp(msg)


% check addon ver against matlab ver
matlab_rel = version('-release');
if (~strcmpi(matlab_rel, addon_rel))
    error('Archive release (%s) does not match the MATLAB release (%s).\nInstallation of %s failed.', addon_rel, matlab_rel, addon_name)
end

% installing to matlabroot
install_dir = matlabroot;

% save lastwarn state
[lwMessage,lwID] = lastwarn('');

% unzip zip file to install_dir
msg = sprintf('Extracting archive %s to %s...', zip_file, install_dir);
disp(msg)
unzipped_files = unzip(zip_file, install_dir);

% check if there were permission problems during extraction from zip file
[lwUnzip,theID] = lastwarn;
if strcmpi(theID,'MATLAB:unzip:UnableToOverwrite')
    error(['\nInstallation Failed.\nYou do not have permission to overwrite some of the files in the archive.\n',...
        'Run \n\nsystem(''chmod -R +w %s'')\n\nto set write permission for these files. If this does not work, contact your system administrator.'],...
        fullfile(install_dir,'help','ja_JP'));
end

% reset lastwarn state if no warnings
if isempty(lwUnzip)
    lastwarn(lwMessage,lwID);
end

% check if files were extracted from zip file
if (isempty(unzipped_files))
    error('No files were extracted from archive %s.\n%s installation failed.', zip_file, addon_name)
end

% fix permissions on extracted files - doc files generally are not writable
for i = 1:length(unzipped_files)
    file = unzipped_files{i};
    fileattrib(file, '-w');
end

% Refresh docroot in case new doc directories were added
docroot(docroot);

msg = sprintf('Installation of %s complete.', addon_name);
disp(msg)

msg = sprintf('\nTo view the updated Japanese documentation, restart MATLAB then type \"doc \".');
disp(msg)
