function PostInstallScript
clc
AddDropboxPath % add one folder up to the path

SetMaxLineLengthPref % sets to 99

if ~legacy('isOctave')
    schemer_import(create_prf_file);
end

DisablePhoneHome

try
    if isunix && ~ismac
        msgbox('don''t forget to set keyboard shortcuts to Windows')
    end
catch
end
end
function x=legacy(s)
persistent LEGACY_
if isempty(LEGACY_)
    %#ok<*JAPIMATHWORKS>
    LEGACY_.isOctave=exist('OCTAVE_VERSION', 'builtin');
    LEGACY_.settings=ifversion('>=','R2018a');
    LEGACY_.com_mathworks=ifversion('>=','R13');
    LEGACY_.R13=ifversion('==','R13');
    
    LEGACY_.split = ifversion('<','R2007b','Octave','<',4);
end
x=LEGACY_.(s);
end
function AddDropboxPath
p=regexp_outkeys(path,';','split').';
add_to_path=fileparts(fileparts(mfilename('fullpath')));
if ~ismember(add_to_path,p)
    path(path,add_to_path)
end
end
function DisablePhoneHome
try
    s=settings;
    w=warning('off','MATLAB:structOnObject');
    fn=fieldnames(struct(s.matlab.ddux));
    warning(w);
    fn=fn(contains(fn,'ddux'));
    for n=1:numel(fn)
        s.matlab.ddux.(fn{n}).chosen.PersonalValue=0;
    end
catch
end
end
function SetMaxLineLengthPref
line_length=99;
if legacy('isOctave')
    % As far as I can tell there is no way to set this by code.
    return
elseif legacy('settings')
    %This was introduced in R2018a and should keep working after com.mathworks is removed.
    s = settings;
    s.matlab.editor.displaysettings.linelimit.LineColumn.PersonalValue=line_length;
    s.matlab.editor.language.matlab.comments.MaxWidth.PersonalValue=line_length;
elseif legacy('com_mathworks')
    %This works at least from R2011, so presumably from R14 and up
    line_length=int16(line_length);
    com.mathworks.services.Prefs.setIntegerPref('EditorRightTextLineLimit',line_length);
    com.mathworks.services.Prefs.setIntegerPref('EditorMaxCommentWidth',line_length);
elseif legacy('R13')
    line_length=int16(line_length);
    com.mathworks.services.Prefs.setIntegerPref('EditorMaxCommentWidth',line_length);
else %If you are using R12 or before, you have a lot more issues than just this.
    return
end
end
function fn=create_prf_file
[ignore_path,fn]=fileparts(tempname); %#ok<ASGLU>
fn=['scheme_' fn(1:min(end,15)) '.prf'];
prf={...
    'ColorsUseSystem=Bfalse';...
    'ColorsUseMLintAutoFixBackground=Btrue';...
    'Editor.VariableHighlighting.Automatic=Btrue';...
    'Editor.NonlocalVariableHighlighting=Btrue';...
    'EditorCodepadHighVisible=Btrue';...
    'EditorCodeBlockDividers=Btrue';...
    'Editorhighlight-caret-row-boolean=Bfalse';...
    'EditorRightTextLineVisible=Btrue';...
    'EditorRightTextLimitLineWidth=I4';...
    'ColorsText=C-1118482';...
    'ColorsBackground=C-14474461';...
    'Colors_M_Keywords=C-26368';...
    'Colors_M_Comments=C-3355444';...
    'Colors_M_Strings=C-6881536';...
    'Colors_M_UnterminatedStrings=C-202417';...
    'Colors_M_SystemCommands=C-16725605';...
    'Colors_M_Errors=C-53398';...
    'Colors_HTML_HTMLLinks=C-6385153';...
    'Colors_M_Warnings=C-26368';...
    'ColorsMLintAutoFixBackground=C-11184811';...
    'Editor.VariableHighlighting.Color=C-4495617';...
    'Editor.NonlocalVariableHighlighting.TextColor=C-16725760';...
    'Editorhighlight-lines=C-16115438';...
    'Editorhighlight-caret-row-boolean-color=C-16777216';...
    'EditorRightTextLimitLineColor=C-8355712';...
    'Color_CmdWinWarnings=C-26368';...
    'Color_CmdWinErrors=C-53398'};
fid=fopen(fn,'w');
fprintf(fid,'%s\n',prf{:});
fclose(fid);
end

function varargout=regexp_outkeys(str,expression,varargin)
varargout=cell(size(varargin));%pre-allocate output
for param=1:(nargin-2)
    switch lower(varargin{param})
        case 'split'
            if legacy('split')
                %legacy implementation
                [s1,s2]=regexp(str,expression);
                split=cell(1,numel(s1)+1);
                start_index=[s1 numel(str)+1];
                stop_index=[0 s2];
                for n=1:numel(start_index)
                    split{n}=str((stop_index(n)+1):(start_index(n)-1));
                end
            else
                split= regexp(str,expression,'split');
            end
            varargout{param}=split;
    end
end
end
function tf=ifversion(test,Rxxxxab,Oct_flag,Oct_test,Oct_ver)
persistent  v_num v_dict octave
if isempty(v_num)
    octave=exist('OCTAVE_VERSION', 'builtin');
    v_num=version;
    ii=strfind(v_num,'.');if numel(ii)~=1,v_num(ii(2):end)='';ii=ii(1);end
    v_num=[str2double(v_num(1:(ii-1))) str2double(v_num((ii+1):end))];
    v_num=v_num(1)+v_num(2)/100;v_num=round(100*v_num);
    v_dict={'R13' 605;'R2007b' 705;'R2018a' 904;};
end
if octave
    if nargin==2
        L=ismember(v_dict(:,1),Rxxxxab);
        v=v_dict{L,2};
    elseif nargin==4
        [test,v]=deal(Oct_flag,Oct_test);
        v=0.1*v+0.9*fix(v);v=round(100*v);
    else
        [test,v]=deal(Oct_test,Oct_ver);
        v=0.1*v+0.9*fix(v);v=round(100*v);
    end
else
    if isnumeric(Rxxxxab)
        v=0.1*Rxxxxab+0.9*fix(Rxxxxab);v=round(100*v);
    else
        L=ismember(v_dict(:,1),Rxxxxab);
        v=v_dict{L,2};
    end
end
switch test
    case '==', tf= v_num == v;
    case '<' , tf= v_num <  v;
    case '<=', tf= v_num <= v;
    case '>' , tf= v_num >  v;
    case '>=', tf= v_num >= v;
end
end