function installiris(varargin)
%
% INSTALLIRIS  The IRIS Toolbox installer.
%
% Syntax:
%   installiris
%   installiris(irisfolder)
%   installiris(irisfolder,texbinfolder)
%   installiris(irisfolder,texbinfolder,x12folder)
%
% IRIS Toolbox 2006/10/11 

home;
disp(' ');

thisfolder = strrep(cd(),'\','/');
irisarchive = sprintf('iris-toolbox-*.zip');

% Check that exactly one iris archive exists in the current folder
x = dir(sprintf('%s/%s',thisfolder,irisarchive));
if length(x) == 0
  error('No IRIS Toolbox archive found in the current folder.');
elseif length(x) > 1
  disp('  Multiple IRIS Toolbox archives found in the current folder:');
  disp(' ');
  for i = 1 : length(x)
    disp(sprintf('  [%g]\t%s',i,x(i).name));
  end
  disp(' ');
  j = NaN;
  while ~isnumeric(j) || j < 1 || j > length(x) || j ~= round(j)
    j = input('  Select the desired IRIS Toolbox archive by its number: ');
  end
  x = x(j);
end

irisarchive = x.name;
irisversion = regexp(x.name,'iris-toolbox-(.*?)\.zip','tokens');
while iscell(irisversion)
  irisversion = irisversion{1};
end

% specify iris folder
if nargin > 0
  irisfolder = varargin{1};
else
  disp(' ');
  irisfolder = input('  Enter the desired IRIS Toolbox folder, e.g. C:/IRIS-TOOLBOX: ','s');
end

disp(' ');
disp(sprintf('  IRIS Toolbox Version %s will be installed in %s.',irisversion,upper(irisfolder)));

% iris folder already exists
if exist(irisfolder) == 7
  contents = dir(irisfolder);
  remove = strcmp({contents(:).name},'.') | strcmp({contents(:).name},'..');
  contents = contents(~remove);
  index = find([contents(:).isdir]);
  index = transpose(index(:));
  if ~isempty(index)
    disp(' ');
    disp(sprintf('  There are %g subfolder(s) in %s. These will be deleted.',length(index),upper(irisfolder)));
    x = input('  Do you wish to continue? [Y]es or [N]o: ','s');
    if strcmpi(x,'n')
      disp(' ');
      disp('  Installation terminated. The IRIS Toolbox has not been installed.');
      disp(' ');
      return
    end
    disp(' ');
    disp('  Removing the existing folder structure. Please wait.'); 
    for i = index
      rmdir(fullfile(irisfolder,contents(i).name),'s');
    end
  end
end

% try to create iris folder
if exist(irisfolder) ~= 7 && ~isempty(irisfolder)
  success = mkdir(irisfolder);
elseif isempty(irisfolder)
  success = false;
else
  success = true;
end
if success == false
  error('Unable to create folder "%s".',irisfolder);
end

% check for existing irisxtools.m
preservextools = false;
xtools = '';
xtoolsfname = fullfile(irisfolder,'irisxtools.m');
if exist(xtoolsfname) == 2
  disp(' ');
  disp(sprintf('  There is an older IRISXTOOLS.M file in %s.',upper(irisfolder)));
  x = input('  Do you wish to preserve the links to external tools contained in this file? [Y]es or [N]o: ','s');
  if strcmpi(x,'y')
    preservextools = true;
    fid = fopen(xtoolsfname);
    xtools = native2unicode(transpose(fread(fid,'uint8=>uint8')));
    fclose(fid);
    xtools = strrep(xtools,char(13),'');
    xtools = regexprep(xtools,'%.*?\n','');
    xtools = sprintf('  %% x12Folder=''''; \n  %% texbinFolder='''';\n%s',xtools);
  end
end

% unzip iris arhive
disp(' ');
disp('  Unzipping. Please wait.'); 
unzip(irisarchive,irisfolder);

home;

if preservextools == false

  % specify tex bin folder
  if nargin > 1
    texbinfolder = varargin{2};
  else
    disp('  ');
    disp('  If you have LaTeX installed on your computer,');
    disp('  enter the folder where latex.exe and other executables are found, e.g. C:/TEXMF/MIKTEX/BIN');
    texbinfolder = input('  (otherwise leave blank): ','s');
  end
  if isempty(texbinfolder)
    disp(' ');
    disp(sprintf('  You may link LaTeX later by modifying the %s/irisxtools.m file.',irisfolder));
  end

  % specify x12 folder  
  if nargin > 2
    x12folder = varargin{3};
  else
    disp('  ');
    disp('  If you have X12 installed on your computer,');
    disp('  enter the folder where x12a.exe or x12axp.exe is found, e.g. C:/X12');
    x12folder = input('  (otherwise leave blank): ','s');
  end
  if isempty(x12folder)
    disp(' ');
    disp(sprintf('  You may link X12 later by modifying the %s/IRISXTOOLS.M file.',upper(irisfolder)));
  end

  % create irisxtools.m
  code = sprintf('  x12Folder = ''%s'';\n  texbinFolder = ''%s'';',upper(x12folder),upper(texbinfolder));
  fid = fopen(xtoolsfname,'w+');
  fwrite(fid,code,'char');
  fclose(fid);

else

  fid = fopen(xtoolsfname,'w+');
  fwrite(fid,xtools,'char');
  fclose(fid);
  
end

home;
disp(' ');
disp(sprintf('  IRIS Toolbox Version %s has been successfully installed in %s with the following IRISXTOOLS.M file:',irisversion,upper(irisfolder)));
type(xtoolsfname);
disp(' ');
disp('  Start each IRIS Toolbox session by the following commands typed in the Matlab command window:');
disp(sprintf('  >> addpath %s; irisstartup;',upper(irisfolder)));
disp(' ');
disp('  If you simultaneously use Dynare use the following option to prevent some function name conflicts:');
disp(sprintf('  >> addpath %s; irisstartup -noconflicts;',upper(irisfolder)));
disp(' ');
disp('  Important: Please NEVER use the File|Set Path menu to permanently keep the IRIS folder on the Matlab path.');
disp(' ');

end