Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")

appData = shell.ExpandEnvironmentStrings("%APPDATA%")
addons = appData & "\kingsoft\wps\jsaddons"
src = fso.GetParentFolderName(WScript.ScriptFullName)

WScript.Echo "=========================================="
WScript.Echo "  XG-Magic-Box v1.2.0 Unified Installer"
WScript.Echo "  Doc v1.1.1 + Sheet v1.2.0 + Presentation v1.0.0"
WScript.Echo "=========================================="
WScript.Echo ""

If Not fso.FolderExists(addons) Then fso.CreateFolder(addons)

' ============================================
' Part 1: Document Plugin (WPS Text)
' Original working mechanism: <jsaddins> in own folder
' ============================================
WScript.Echo "[1/3] Installing Document Plugin v1.1.1 (WPS Text)..."

docPlugin = addons & "\xg-magic-box_1.1.1"
docSrc = src & "\doc"

' Delete old doc plugin folder entirely and recreate fresh
If fso.FolderExists(docPlugin) Then
    fso.DeleteFolder docPlugin, True
    WScript.Echo "  Removed old: xg-magic-box_1.1.1"
End If
fso.CreateFolder(docPlugin)

Dim docFiles
docFiles = Array("main.js", "sidebar.html", "ribbon.xml", "logo.png", "index.html")
Dim i
For i = 0 To UBound(docFiles)
    srcFile = docSrc & "\" & docFiles(i)
    If fso.FileExists(srcFile) Then
        fso.CopyFile srcFile, docPlugin & "\" & docFiles(i), True
        WScript.Echo "  Copied: " & docFiles(i)
    End If
Next

' publish.xml INSIDE doc plugin folder (<jsaddins> format)
Dim docPub
docPub = docPlugin & "\publish.xml"
If fso.FileExists(docPub) Then fso.DeleteFile docPub, True
Set ds = CreateObject("ADODB.Stream")
ds.Type = 2
ds.Charset = "UTF-8"
ds.Open
ds.WriteText "<?xml version=""1.0"" encoding=""UTF-8""?>"
ds.WriteText "<jsaddins>"
ds.WriteText "  <jsaddin>"
ds.WriteText "    <name>xg-magic-box</name>"
ds.WriteText "    <version>1.1.1</version>"
ds.WriteText "    <entry>index.html</entry>"
ds.WriteText "  </jsaddin>"
ds.WriteText "</jsaddins>"
ds.SaveToFile docPub, 2
ds.Close
WScript.Echo "  Doc plugin -> " & docPlugin

WScript.Echo ""

' ============================================
' Part 2: Sheet Plugin (WPS Spreadsheet)
' Mechanism: <jsplugins> at jsaddons ROOT
' ============================================
WScript.Echo "[2/3] Installing Sheet Plugin v1.2.0 (WPS Spreadsheet)..."

etPlugin = addons & "\xg-magic-box_1.2.0"
etSrc = src & "\et"

' Clean old SHEET plugin versions ONLY (1.0.x and 1.1.0)
' Do NOT delete 1.1.1 - that's the doc plugin!
Dim oldEtFolders
oldEtFolders = Array("xg-magic-box_1.0.0", "xg-magic-box_1.0.1", "xg-magic-box_1.0.2", "xg-magic-box_1.0.3", "xg-magic-box_1.0.4", "xg-magic-box_1.0.5", "xg-magic-box_1.0.6", "xg-magic-box_1.0.7", "xg-magic-box_1.0.8", "xg-magic-box_1.0.9", "xg-magic-box_1.1.0")
Dim j
For j = 0 To UBound(oldEtFolders)
    oldPath = addons & "\" & oldEtFolders(j)
    If fso.FolderExists(oldPath) Then
        fso.DeleteFolder oldPath, True
        WScript.Echo "  Removed old: " & oldEtFolders(j)
    End If
Next

' Delete old et plugin folder entirely and recreate fresh
If fso.FolderExists(etPlugin) Then
    fso.DeleteFolder etPlugin, True
    WScript.Echo "  Removed old: xg-magic-box_1.2.0"
End If
fso.CreateFolder(etPlugin)

Dim etFiles
etFiles = Array("main.js", "sidebar.html", "ribbon.xml", "logo.png", "index.html", "template_center.html")
For i = 0 To UBound(etFiles)
    srcFile = etSrc & "\" & etFiles(i)
    If fso.FileExists(srcFile) Then
        fso.CopyFile srcFile, etPlugin & "\" & etFiles(i), True
        WScript.Echo "  Copied: " & etFiles(i)
    End If
Next

WScript.Echo ""

' ============================================
' Part 3: Presentation Plugin (WPS Presentation)
' Mechanism: <jsplugins> at jsaddons ROOT, type=wpp
' ============================================
WScript.Echo "[3/3] Installing Presentation Plugin v1.0.0 (WPS Presentation)..."

wppPlugin = addons & "\xg-magic-box_wpp_1.0.0"
wppSrc = src & "\wpp"

If fso.FolderExists(wppPlugin) Then
    fso.DeleteFolder wppPlugin, True
    WScript.Echo "  Removed old: xg-magic-box_wpp_1.0.0"
End If
fso.CreateFolder(wppPlugin)

Dim wppFiles
wppFiles = Array("main.js", "template_center.html", "ribbon.xml", "index.html")
For i = 0 To UBound(wppFiles)
    srcFile = wppSrc & "\" & wppFiles(i)
    If fso.FileExists(srcFile) Then
        fso.CopyFile srcFile, wppPlugin & "\" & wppFiles(i), True
        WScript.Echo "  Copied: " & wppFiles(i)
    End If
Next

If fso.FolderExists(wppSrc & "\templates") Then
    fso.CopyFolder wppSrc & "\templates", wppPlugin & "\templates", True
    WScript.Echo "  Copied: templates"
End If

' publish.xml at ROOT for all three plugins
Dim publishPath
publishPath = addons & "\publish.xml"
If fso.FileExists(publishPath) Then fso.DeleteFile publishPath, True
Set rs = CreateObject("ADODB.Stream")
rs.Type = 2
rs.Charset = "UTF-8"
rs.Open
rs.WriteText "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf
rs.WriteText "<jsplugins>" & vbCrLf
rs.WriteText "<jsplugin name=""xg-magic-box"" enable=""enable_dev"" url=""file://"" type=""et"" version=""1.2.0""/>" & vbCrLf
rs.WriteText "<jsplugin name=""xg-magic-box"" enable=""enable_dev"" url=""file://"" type=""wps"" version=""1.1.1""/>" & vbCrLf
rs.WriteText "<jsplugin name=""xg-magic-box_wpp"" enable=""enable_dev"" url=""file://"" type=""wpp"" version=""1.0.0""/>" & vbCrLf
rs.WriteText "</jsplugins>" & vbCrLf
rs.SaveToFile publishPath, 2
rs.Close
WScript.Echo "  Sheet plugin -> " & etPlugin
WScript.Echo "  PPT plugin -> " & wppPlugin

WScript.Echo ""
WScript.Echo "=========================================="
WScript.Echo "  Installed:"
WScript.Echo "    Doc Plugin v1.1.1:   " & docPlugin
WScript.Echo "    Sheet Plugin v1.2.0: " & etPlugin
WScript.Echo "    PPT Plugin v1.0.0:   " & wppPlugin
WScript.Echo ""
WScript.Echo "  Please restart ALL WPS apps."
WScript.Echo "=========================================="
