
% SDT Installation debugging file
% This should be started with the following files in the same directory
%  - *_dis.p      : distribution 
%  - t_install4.m : this script 
%  - Possibly the file sdtkey.p if instructed to do so by SDTools
%
%  You must then have local variable containing your license string.
%  A target directory if other than default. Then simply run this script
%
%  SDT_license_key = '12-00000-00000-00000-00000-00000'
%  %target='/APP/sdt'; % needed if other than default Matlabroot/toolbox/sdt
%  t_install4
%
%

if  ~exist('SDT_license_key','var')
 error('You must have a SDT_license_key variable in your workspace')
end

% create log file

TempFile=fullfile(tempdir,'sdt_install.log');
rehash('toolboxreset');
if exist(TempFile,'file'); delete(TempFile); end
diary(TempFile);
if exist('sdtcheck','file'); try; sdtcheck('pathnone');end;end

% find where the archive is
a=dir('sdt*_dis.p');a={a.name};b=dir('sdt*d_dis.p');b={b.name};
if length(a)>1&&~isempty(b); a=setdiff(a,b);end
if length(a)~=1; fprintf('Installing from %s',a{end}); end
[wd,arch]=fileparts(a{end}); caller=arch;
if isempty(wd); wd=pwd; end; arch=fullfile(wd,[arch '.p']);
a=dir(arch);st1='-';st1=st1(ones(60,1));
RunOpt.Warn={ ...
  sprintf('\n%s\nInstalling from %s (%i bytes)\n%s\n',st1,arch,a.bytes,st1)};
if any(exist(fullfile(wd,'sdtkey')','file')==[2 6]);
  pw0=pwd;cd(wd); eval('FKEY=@sdtkey;');cd(pw0);
else;[wd,fname]=fileparts(arch);eval(sprintf('FKEY=@%s;',fname));
end
echo on
warning backtrace
ver
whos
path

% Try to list pre existing things 

st1=fullfile(docroot,'toolbox');
if ~exist(st1,'dir')
 if isunix; ls('-l',st1); else ls(st1); end
end

try
 if isunix; ls('-l',fullfile(matlabroot,'toolbox','sdt'));
 else ls(fullfile(matlabroot,'toolbox','sdt')); end
catch; lasterr
end

try
 ls(fullfile(matlabroot,'toolbox','sdt','sdtdemos'));
catch; lasterr
end
try;feval(FKEY,SDT_license_key)
catch; lasterr
end
try;feval(FKEY,'hostid')
catch; lasterr
end

% Build the targe directory names

if ~exist('target','var') 
  target=fullfile(matlabroot,'toolbox','sdt');
end
if isempty(target); error('You must give a target directory');end
if strcmpi(fileparts(which(func2str(FKEY))),target)
   error(sprintf('The extraction function %s cannot be located %s\n%s', ...
    func2str(FKEY),'in the target directory',target)); %#ok<SPERR>
end
RunOpt.Warn{end+1}=sprintf('''%s'' target for SDT installation.\n',target);
fprintf('%s\n',RunOpt.Warn{:});
r=input('Is this correct [y]/n ?','s'); if strncmpi(r,'n',1); target=''; end 

htarget=strrep(fullfile(target,'help'),'\','/'); ptarget=htarget;

extract_com=sprintf('extract -d %s -dh %s -dp %s  ',target,htarget,ptarget);
[wd,fun,ext]=fileparts(arch);
feval(FKEY,extract_com,SDT_license_key,arch,SDT_license_key)

try %#ok<TRYNC>
 cd(wd);
 [st1,st2,st3]=fileparts(which('sdtkey'));
 copyfile(fullfile(st1,[st2 st3]),target);
end

try %#ok<TRYNC>
 cd(target);
 sdtcheck('path');
end

cd(wd);
eval('sdtcheck check')

diary off

fprintf('\n\n\n\n\nPlease e-mail the %s file to support@sdtools.com\n\n\n\n',TempFile);




