% This matlab script will install the SpatialAnalysis software.
% The installation script will take the zip file, which contains all the
% necessary files, create a directory for them, and then unzip the files.
% It will also setup the preferences for the software.

% Define the location of the install folder
InstallDir = uigetdir(matlabroot,'Select a folder for Spatial Analysis 3D');

if(InstallDir==0)
    % Then the user canceled the installation directory choice

else
    % First check to see if the zip file is in the current directory which
    % should be the directory where this install script is being run
    if(exist('SpatialAnalysis.zip','file'))
        try
            % Attempt to unzip the file
            unzip([pwd,filesep,'SpatialAnalysis.zip'],InstallDir)
        catch
            % This will run if the zip file can not be unzipped properly
            errordlg({'SpatialAnalysis.zip could not be unipped';...
                lasterr},'Install Spatial Analysis Error')
            clear variables
            return
        end
            
    else
        % We could not find the zip file in the same directory as the
        % install file, so prompt the user and ask if they would like to
        % download the zip file from the website
        answer = questdlg({'SpatialAnalysis.zip could not be found in:';pwd;...
            'Would you like to download the file from the website?'},...
            'Install Spatial Analysis 3D','Yes','No','Cancel','Yes');

        switch(answer)
            case 'Yes'
                % Switch to the directory the user chose as the install directory, and
                % unzip the files.
                cd(InstallDir)

                % Attempt to download and unzip the file from the website
                try
                    unzip('http://www.nri.ucsb.edu/Labs/breese/Files/SpatialAnalysis.zip',InstallDir)
                catch
                    % This will run if the zip file can not be downloaded properly
                    %folder = urlwrite('http://www.nri.ucsb.edu/Labs/breese/Files/SpatialAnalysis.zip','SpatialAnalysis.zip');
                    errordlg(['SpatialAnalysis.zip could not be downloaded from: ',...
                        'http://www.nri.ucsb.edu/Labs/breese ',...
                        'You may need to enter a Proxy server host address in ',...
                        'File|Preferences|Web'],'Install Spatial Analysis Error')
                    clear variables
                    return
                end
            case 'No'
                % Simply exit from the install script
                return
            case 'Cancel'
                % Simply exit from the install script
                return
        end
    end

    % Now add the folders to the path and save the path
    addpath([InstallDir,filesep,'SpatialAnalysis3D'])
    addpath([InstallDir,filesep,'userfun'])
    savepath

    % Now setup the preferences
    setpref('SpatialAnalysis','User','user')
    setpref('SpatialAnalysisTips','Display',1)
    setpref('RecentFiles','File1','')
    setpref('RecentFiles','File2','')
    setpref('RecentFiles','File3','')
    setpref('RecentFiles','File4','')

    % Now run the in setup routine for the sofware
    cd([InstallDir,filesep,'SpatialAnalysis3D'])
    SpatialAnalysis -setup

    % Clear all the variables
    clear variables

    % Then launch the program
    SpatialAnalysis
end

