//===========================================================================
//
//  File Name:    Setup.rul
//
//  Description:  Blank setup main script file
//
//  Comments:     Blank setup is an empty setup project. If you want to
//				  create a new project via. step-by step instructions use the
//				  Project Assistant.
//
//===========================================================================

// Included header files ----------------------------------------------------
#include "ifx.h"

prototype GetProductKeysToRemove();  
  
  
  
function GetProductKeysToRemove()
    // To Do:  Declare local variables.   
    string s,Tmp,PropertyKey,svString2,ProductRegFolder,ProductRegFolderFather,Product_name,szKey, szSubKey,szName,svSubStr ,svString ;
    number i,nResult2,nLength,nResult,ret,nvType, nvSize; 
    LIST listMyProducts,listResults,listUsers,listProducts;
begin   
        RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ); 
        GetEnvVar("TEMP",Tmp);
        nLength = 256;
        listUsers = ListCreate(STRINGLIST); 
        listProducts = ListCreate(STRINGLIST);
         
      	szKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData"; 
      	RegDBQueryKey ( szKey, REGDB_KEYS, listUsers ); 

        nResult = ListGetFirstString (listUsers, svString); 
      //  MessageBox( svString,1);

        // Loop while list items continue to be retrieved. 

       while (nResult != END_OF_LIST) 
         //    MessageBox( "Working on user:" + svString,1);
             
             ProductRegFolderFather = szKey ^ svString ^"Products" ;    
            // MessageBox( "Products for that user: " + ProductRegFolderFather,1);
             RegDBQueryKey ( ProductRegFolderFather, REGDB_KEYS, listProducts ); 
             nResult2 = ListGetFirstString (listProducts, svString2);
             while (nResult2 != END_OF_LIST) 
             //     MessageBox( "Products Name: " + svString2,1);
                  PropertyKey = ProductRegFolderFather ^ svString2 ^"InstallProperties";
             //     MessageBox( "InstallProperties key: " + PropertyKey,1);
                  szName =  "DisplayName";
                  ret = RegDBGetKeyValueEx(PropertyKey, szName, nvType, svSubStr, nvSize );  
            //      MessageBox("DisplayName:" + svSubStr,1);
                  if(   svSubStr %"RHEV-Network" ) then  
           //             MessageBox( "key to delete:" + ProductRegFolderFather ^ svString2,1);
                        ret = RegDBDeleteKey( ProductRegFolderFather ^ svString2); 
            //            MessageBox(FormatMessage(ret),1); 
                  endif;
                  nResult2 = ListGetNextString (listProducts, svString2);
             endwhile;
             nResult = ListGetNextString (listUsers, svString); 

        endwhile;    
        ListDestroy(listUsers);
        ListDestroy(listProducts);  
        
        listMyProducts = ListCreate(STRINGLIST);
        
        szKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"; 
      	RegDBQueryKey ( szKey, REGDB_KEYS, listMyProducts ); 
        svString2 =""; 
        svSubStr = "";
        nResult2 = ListGetFirstString (listMyProducts, svString2);
             while (nResult2 != END_OF_LIST) 
                //  MessageBox( "Products Name: " + svString2,1);
                  szName =  "DisplayName";
                  ret = RegDBGetKeyValueEx(szKey^svString2, szName, nvType, svSubStr, nvSize );  
                //  MessageBox("DisplayName:" + svSubStr,1);
                  if(   svSubStr % "RHEV-Network" ) then  
                  //      MessageBox( "key to delete:" + szKey^svString2,1);
                        ret = RegDBDeleteKey( szKey^svString2); 
                  //      MessageBox(FormatMessage(ret),1);   
                        NumToStr(s,i);
                        LaunchAppAndWait("msiexec"," /x  " + svString2 + " /q /L*v " + Tmp ^ s +"NetKvmUninst.log",LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN);
                        i++;
                  endif;
                   svString2 =""; 
                   svSubStr = "";
                  nResult2 = ListGetNextString (listMyProducts, svString2);
             endwhile;
       	//SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UpgradeCodes\\C94D69AF12E9E7945BCC9E6576C2D8B4
         ret = RegDBDeleteKey( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UpgradeCodes\\C94D69AF12E9E7945BCC9E6576C2D8B4"); 
                  
      	
    
end;
//---------------------------------------------------------------------------
// OnBegin
//
// The OnBegin event is called directly by the framework after the setup
// initializes. Note that this event will be called AFTER "Initialization"
// events (i.e.) OnSetTARGETDIR, OnCheckMediaPassword.
//
// Note: This event will not be called automatically in a
// program...endprogram style setup.
//---------------------------------------------------------------------------
function OnBegin()
begin
    // TODO: Perform custom initialization steps, check requirements, etc.   
    GetProductKeysToRemove();
    abort;
end;
  