Use Windows.pkg
Use cApplication.pkg
Use RegCheck2.inc

Object oApplication is a cApplication
    Set pbPreserveEnvironment to False
    Set psCompany to "RDC Tools International"
    Set psProduct to "RegCheckChanger"
    Set psVersion to "2012.1"

    Property Boolean pbRevertRegistryValues False

    Procedure OnCreate
        Handle hoCmdLine
        String sArgument

        Get phoCommandLine to hoCmdLine
        Get Argument of hoCmdLine 1 to sArgument
        If (Uppercase(sArgument) = CS_DefaultRegSettings) Begin
            Set pbRevertRegistryValues to True
        End
    End_Procedure

End_Object

// *** Must be used after the cApplication object!
Use RegCheck2.pkg

// NOTE: There is a 'EmbedManifestFile.bat' in the programs folder
//       of the RegCheck2 workspace. What it does is to embed the
//       RegCheck2Changer.exe.manifest (the manifest file) into this
//       executable - RegCheck2Changer.exe.
//
//       Run the 'EmbedManifestFile.bat' after a recompile of this
//       program to embed the manifest into the exe file.
//       Then you don't need to deploy the manifest file!
Procedure DoChangeRegistrySettings
    Boolean bReportOnlyState bRegistryChangesNeeded
    Boolean bRegistryChangeFailed bRegistryWriteAccessDenied bRevertRegistryValues
    Integer iRetval

    Get pbReportOnlyState of ghoRegCheck2 to bReportOnlyState
    // This will allow us to check the registry settings
    // without actually change them:
    Set pbReportOnlyState of ghoRegCheck2 to True
    Send DoCheckRegistrySettings of ghoRegCheck2
    Set pbReportOnlyState of ghoRegCheck2 to bReportOnlyState

    Get pbRegistryChangesNeeded of ghoRegCheck2 to bRegistryChangesNeeded
    // If CS_DefaultRegSettings was passed on the command line
    // registry values shall be reset to their default values!
    Get pbRevertRegistryValues of ghoApplication to bRevertRegistryValues
    If (bRevertRegistryValues = True) Begin
        Set pbRevertRegistryValues of ghoRegCheck2 to True
        Move True to bRegistryChangesNeeded
    End

    // Yes, we need to change the registry:
    If (bRegistryChangesNeeded = True) Begin

        // *** Main write message ***
        Send DoWriteRegistrySettings of ghoRegCheck2

        // True if at least on of the registry changes failed.
        Get pbRegistryChangeFailed of ghoRegCheck2 to bRegistryChangeFailed
        // True if access was denied when trying to write to registry.
        Get pbRegistryWriteAccessDenied of ghoRegCheck2 to bRegistryWriteAccessDenied

        // Registry changes failed:
        If (bRegistryChangeFailed = True) Begin
            If (bRegistryWriteAccessDenied = True) Begin
                Send Stop_Box (CS_RegistryAccessDenied * csRegistryChangesNeeded + "\n" + csContactYourSystemAdministrator + csProgramWillBeTerminated)
            End
            // Else there was some error writing to the registry; see error log file.
            Else Begin
                Send Stop_Box (csErrorWritingToRegistry + "(" + CS_LogFileName + ")"  + "\n" + csProgramWillBeTerminated)
            End
        End

        // Registry changes succeeded:
        Else If (bRegistryWriteAccessDenied = False) Begin
            If (bRevertRegistryValues = False) Begin
                Get YesNo_Box csRegistryChangesDoneReboot to iRetval
                If (iRetval = MBR_Yes) Begin
                    Send RebootMachine of ghoRegCheck2
                End
                Else Begin
                    Send Info_Box (csManualRebootInfo + csProgramWillBeTerminated)
                End
            End
            Else If (bRevertRegistryValues = True) Begin
                Send Info_Box csRegistryValuesReset
            End
        End
    End

    // No registry changes necessary:
    If (bRegistryChangesNeeded = False) Begin
        Send Info_Box csNoRegistryChangesNeeded
    End
    Send Exit_Application
End_Procedure

Send DoChangeRegistrySettings
