'// CP4.04.04  fix for VB .Net change in handling of Set command on non-object variables
'// 2.01.27.05
'// PermFix for standalone
'// To adjust permissions on folders for standalone web server
Option Explicit
On Error Resume Next

Dim oWebService, oPotentialSite, oCpmgr, oApp

Dim gszWebRoot, gszAutoaddfilesPath, gszUploadPath
Dim gszSSLPath, gszOptionsPath, gszLogFile
Dim pathArray, pathpos
Dim WshShell, objEnv

gszLogFile = "c:\logfile.txt"

Set WshShell = CreateObject("WScript.Shell")
Set objEnv = WshShell.Environment("PROCESS")
pathArray = Split(objEnv("SystemDrive"), ":")

set oWebService = GetObject("IIS://LocalHost/W3SVC")
if (Err <> 0) then
	Log("Could not find W3SVC. Error: " & Err.Number)
	Wscript.Quit
end if

'// The list of web sites must be searched to find the match
Err = 0
for each oPotentialSite in oWebService
	if oPotentialSite.Class = "IIsWebServer" then
		set oCpmgr = GetObject("IIS://LocalHost/W3SVC/"&oPotentialSite.Name&"/root/cpmgr")
		if IsObject(oCpmgr) then
			pathArray = Split(oCpmgr.path,":")
			Exit for
		end if
	end if
next

gszWebRoot = pathArray(0) & ":\InetPub"
gszAutoaddfilesPath = pathArray(0) & ":\InetPub\wwwroot\cpmgr\AutoAddFiles"
gszUploadPath = pathArray(0) & ":\InetPub\wwwroot\cpmgr\Upload"
gszSSLPath = pathArray(0) & ":\InetPub\wwwroot\cpmgr\Ssl"
gszOptionsPath = pathArray(0) & ":\InetPub\wwwroot\cpmgr\Preferences\Options"

Set oApp = WScript.CreateObject("WScript.shell")

pathpos = InStrRev(WScript.ScriptFullName,"\")
If pathpos = 0 Then
	pathpos = InStr(WScript.ScriptFullName,":")
End If
oApp.run "cmd /c """& Left(WScript.ScriptFullName,pathpos) &"permfix_standalone.bat"" "&oWebService.AnonymousUserName&" "&gszWebRoot&" "&gszAutoaddfilesPath&" "&gszUploadPath&" "&gszSSLPath&" "&gszOptionsPath

'// Log()
'//
'// Write message to logfile.txt
'//
Sub Log(szText)
	On Error Resume Next
	Dim	oFS, oMsgFile, szFileName
	
	Debug.Write szText

	szFileName = gszLogFile

	set oFS = CreateObject("Scripting.FileSystemObject")
	set oMsgFile = oFS.OpenTextFile(szFileName,8,True)

	oMsgFile.WriteLine(szText)
	oMsgFile.Close
End Sub