# look for octave installations
%pkg load windows

keypath = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';
rootpaths = {'HKLM', 'HKCU'};
for r = 1:length(rootpaths)
  rootpath = rootpaths{r};
  
  [ok,txt] = system(sprintf('reg query %s\\%s', rootpath, keypath));
  keys = strsplit(txt, {"\n", "\r"}, 'COLLAPSEDELIMITERS', true);
  for i=1:length(keys)
    key = keys{i};
    if length(key) > 1
      tmp = strsplit(key, '\');
      if length(tmp) > 1 && strncmpi(tmp{end}, "octave", 6) 
        ver = [];
        ver.version = tmp{end};
        ver.regkey = key;
      
        installpath = "<none>";
        try
          installpath = winqueryreg(rootpath,[keypath '\' ver.version], 'InstallLocation');
          ver.installpath = installpath;
        catch
          installpath = winqueryreg(rootpath,[keypath '\' ver.version], 'UninstallString');
          # some versions of UninstallString has /AllUsers or /CurrentUser, remove that
          if index(installpath, '/') != 0
            [installpath,~] = fileparts(installpath);
          endif
          [installpath,~] = fileparts(installpath);
          installpath = strrep(installpath ,'"', '');
          
        end_try_catch      
        
        ver.installrootpath = installpath;

        ver.uninstallstr = winqueryreg(rootpath,[keypath '\' ver.version], 'UninstallString');

        ver
      endif
    endif
  endfor
endfor


