'RemoveContentProtection.vbs
'Strip all content protection related code from source tree.
'Run from /src/KMD folder

Option Explicit


'VBScript constants
Const ForReading = 1, ForWriting = 2, ReadOnly = 1

'script constants
Const STRIP_PATTERN_C = "/\*[ \t]*BEGIN NON-DISTRIBUTABLE CODE(?:.|[\r\n])*?\*/(?:.|[\r\n])*?/\*[ \t]*END NON-DISTRIBUTABLE CODE(?:.|[\r\n])*?\*/"
Const STRIP_PATTERN_XML = "(<File[ \t\r\n]+RelativePath=""\.\\(copp\.cpp|copp\.h|CoppHelper\.cpp|CoppHelper\.h|Certificates_xp32\.h)""[ \t\r\n]*>[ \t\r\n]*</File>)|xc_copp_x86_disarmed\.lib|xc_copp_x86_production\.lib"
Const SCRIPT_WARNING = "Warning!  Continuing with this script will modify and or delete files that have to do with Content Protection.  Do *not* check in any of the changed files.  You should run this on a copy of the tree to avoid source control sync issues.  Continue?"
Const SCRIPT_COMPLETE = "Removal complete!  Don't forget to verify that build still works for debug and release!  Do *not* check in any of the changed files."
Const ROOT_FOLDER = "..\..\..\src\miniport\"


Const STRIP_PATTERN_XML_DXVA = "(<File[ \t\r\n]+RelativePath=""\.\\(dxvacopp\.cpp)""[ \t\r\n]*>[ \t\r\n]*</File>)|xc_copp_x86_disarmed\.lib|xc_copp_x86_production\.lib"
Const ROOT_FOLDER_DXVA = "..\..\..\src\DxVA\"


'globals
Dim regExpStrip
Dim objFileSystem

Dim vszStripFiles
vszStripFiles = Array( _
    "XPOSS.cpp", _
    "vidMode.cpp", _
    "vidmini.cpp", _
    "vidIoctl.cpp")

Dim vszStripFilesXml
vszStripFilesXml = Array( _
    "miniport.vcproj" )

Dim vszDeleteFiles
vszDeleteFiles = Array( _
    "copp.cpp", _
    "copp.h", _
    "CoppHelper.cpp", _
    "CoppHelper.h", _
    "Certificates_xp32.h" )

Dim vszStripFilesDXVA
vszStripFilesDXVA = Array( _
    "dxvacaps.cpp", _
    "dxvaddi.cpp")

Dim vszStripFilesXmlDXVA
vszStripFilesXmlDXVA = Array( _
    "DxVA.vcproj" )

Dim vszDeleteFilesDXVA
vszDeleteFilesDXVA = Array( _
    "dxvacopp.cpp" )

Sub Initialize
    Set regExpStrip = New RegExp
    regExpStrip.Pattern = STRIP_PATTERN_C
    regExpStrip.IgnoreCase = True
    regExpStrip.Global = True
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
End Sub


Sub StripCode(szFilename)

    Dim bufBefore, bufAfter
    Dim objFile, objFileRead, objFileWrite
    Dim bReadOnly
    
    If not objFileSystem.FileExists(szFilename) Then
        Exit Sub
    End If
    
    Set objFile = objFileSystem.GetFile(szFilename)
    If objFile.Size <= 0 Then
        Err.Raise 1
    End If
    
    Set objFileRead = objFileSystem.OpenTextFile(szFilename, ForReading, False)
    bufBefore = objFileRead.ReadAll
    objFileRead.Close
    bufAfter = regExpStrip.Replace(bufBefore, "")
    
    bReadOnly = False
    If objFile.attributes and ReadOnly Then
        bReadOnly = True
        objFile.attributes = objFile.attributes - ReadOnly
    End If
    
    Set objFileWrite = objFileSystem.OpenTextFile(szFilename, ForWriting, True)
    objFileWrite.Write bufAfter
    objFileWrite.Close
    
    If bReadOnly Then
        'Set back to readonly so that if developer wants to later force a revision, the
        'stripped file can get clobbered by source control
        objFile.attributes = objFile.attributes + ReadOnly
    End If
    
End Sub


Sub StripCodeC(szFilename)

    regExpStrip.Pattern = STRIP_PATTERN_C
    StripCode(szFilename)

End Sub


Sub StripCodeXml(szFilename)

    regExpStrip.Pattern = STRIP_PATTERN_XML
    StripCode(szFilename)

End Sub

Sub StripCodeXmlDXVA(szFilename)

    regExpStrip.Pattern = STRIP_PATTERN_XML_DXVA
    StripCode(szFilename)

End Sub


'main

Initialize

Dim nResult
Dim szFilename

nResult = MsgBox(SCRIPT_WARNING, vbYesNo)
If nResult <> vbYes Then
    Err.Raise 1,"Terminate","Terminating script..."
End If

For Each szFilename in vszStripFiles
    StripCodeC ROOT_FOLDER & szFilename
Next

For Each szFilename in vszStripFilesXml
    StripCodeXml ROOT_FOLDER & szFilename
Next

For Each szFilename in vszDeleteFiles
    If objFileSystem.FileExists(ROOT_FOLDER & szFilename) Then
        objFilesystem.DeleteFile ROOT_FOLDER & szFilename, True
    End If
Next

For Each szFilename in vszStripFilesDXVA
    StripCodeC ROOT_FOLDER_DXVA & szFilename
Next

For Each szFilename in vszStripFilesXmlDXVA
    StripCodeXmlDXVA ROOT_FOLDER_DXVA & szFilename
Next

For Each szFilename in vszDeleteFilesDXVA
    If objFileSystem.FileExists(ROOT_FOLDER_DXVA & szFilename) Then
        objFilesystem.DeleteFile ROOT_FOLDER_DXVA & szFilename, True
    End If
Next

MsgBox SCRIPT_COMPLETE
