Option Explicit
Dim DeployMachine:DeployMachine = "KCI-APP01"
Dim remoteDeployPath:remoteDeployPath = "C:\inetpub\tracking"
Dim ScriptDirectory:ScriptDirectory = Replace(WScript.ScriptFullName,WScript.ScriptName,"")
Dim DeployRoot:DeployRoot = "F:\Development\Tracking_Kits\CiCd\" '"C:\inetpub\wwwroot\"

Dim fso:Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Dim WshShell:Set WshShell = WScript.CreateObject("Wscript.Shell")
Dim objShell:Set objShell = CreateObject("Shell.Application")
Dim transferFolder:transferFolder = "\\KCI-SYN-CL01\PC Transfer\"
If fso.FolderExists(DeployRoot & "test") Then
	WshShell.Run "cmd.exe /c rmdir /s /q """ & DeployRoot & "test""" , 0,True
End If

CloneRepo
ZipRepo
Cleanup
Deploy

Sub Deploy
Debug.WriteLine ScriptDirectory & "PsExec64.exe -i \\" & DeployMachine & " -u ntp\daniel_admin -p BarkHornSlam4* cmd.exe /C " & _
"nircmd.exe elevatecmd runassystem C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:tracking & " & _
"nircmd.exe elevatecmd runassystem C:\Windows\System32\inetsrv\appcmd.exe stop site /site.name:tracking & " & _
"copy /Y """ & transferFolder & "\zzip.zip"" " & """" & remoteDeployPath & "\zip.zip""" & " & tar -xf ""C:\inetpub\tracking\zip.zip"" -C ""C:\inetpub\tracking"" & exit"

'WshShell.Run ScriptDirectory & "PsExec64.exe -i \\" & DeployMachine & " -u ntp\daniel_admin -p BarkHornSlam4* cmd.exe /C " & _
'"echo ""stopping app pool"" & " & _

'WshShell.Run ScriptDirectory & "PsExec64.exe -i \\" & DeployMachine & " -u ntp\daniel_admin -p BarkHornSlam4* ""nircmd.exe"" elevatecmd runassystem ""C:\Windows\System32\inetsrv\appcmd.exe"" stop apppool /apppool.name:tracking

'"nircmd.exe elevatecmd runassystem ""C:\Windows\System32\inetsrv\appcmd.exe"" stop apppool /apppool.name:tracking & " & _
'"echo ""stopping site"" & " & _
'"nircmd.exe elevatecmd runassystem  ""C:\Windows\System32\inetsrv\appcmd.exe"" stop site /site.name:tracking & " & _
'"echo ""removing files"" & " & _
'"nircmd.exe elevatecmd runassystem ""powershell"" Get-ChildItem -Path 'C:\inetpub\tracking\' -File -Recurse -Force | Remove-Item -WhatIf" & _

WshShell.Run ScriptDirectory & "PsExec64.exe -i \\" & DeployMachine & " -u ntp\daniel_admin -p BarkHornSlam4* cmd.exe /C " & _
	"copy /Y """ & transferFolder & "\zzip.zip"" " & """" & remoteDeployPath & "\zip.zip""" & _
	" & tar -xf ""C:\inetpub\tracking\zip.zip"" -C ""C:\inetpub\tracking"" & exit" _
	,1,True


'WshShell.Run ScriptDirectory & "PsExec64.exe -i \\" & DeployMachine & " -u ntp\daniel_admin -p BarkHornSlam4* " &_
'"nircmd.exe elevatecmd runassystem C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:tracking"
'"nircmd.exe elevatecmd runassystem C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:tracking & " & _
'"nircmd.exe elevatecmd runassystem C:\Windows\System32\inetsrv\appcmd.exe stop site /site.name:tracking & " & _
'"copy /Y """ & transferFolder & "\zzip.zip"" " & """" & remoteDeployPath & "\zip.zip""" & " & tar -xf ""C:\inetpub\tracking\zip.zip"" -C ""C:\inetpub\tracking"" & exit",1,True


End Sub


Sub Cleanup
	WshShell.CurrentDirectory = DeployRoot
	WshShell.Run "cmd.exe /c rmdir /s /q """ & DeployRoot & "test\""" , 0,True
	WScript.Sleep 2500
	WScript.Sleep 2500
	fso.DeleteFile DeployRoot & "\zzip.zip"

End Sub 

Sub ZipRepo
	WScript.Sleep 2500
	WScript.Sleep 2500
	fso.OpenTextFile(DeployRoot & "zzip.zip", 2, True).Write "PK" & Chr(5) & Chr(6) _
	  & String(18, Chr(0))
	  WScript.Sleep 2500
	WScript.Sleep 2500
	Dim objZip
	Set objZip = objShell.NameSpace(DeployRoot & "zzip.zip")
	Dim objFolder:Set objFolder = objShell.NameSpace(DeployRoot & "test\")
	
	objZip.CopyHere objFolder.Items 
	Do Until objZip.Items.Count = objFolder.Items.Count
	    WScript.Sleep 200
	Loop
	fso.CopyFile DeployRoot & "zzip.zip", transferFolder & "zzip.zip", True
	Set objFolder = nothing
End Sub

Sub CloneRepo
	WshShell.Exec("git init test")
	WScript.Sleep 2500
	WshShell.CurrentDirectory = DeployRoot & "test"
	WshShell.Exec("git remote add -f origin https://dcovington:_3ggUSA6YELP@onefortheroadgit.sytes.net/dcovington/tracking_kits")
	WshShell.Exec("git config core.sparseCheckout true")
	Dim objFile
	Set objFile = fso.CreateTextFile(".git/info/sparse-checkout")
		objFile.WriteLine("App/")
		objFile.WriteLine("Data/arrow ne.jpg")
		objFile.WriteLine("Data/Label_Report.rep")
		objFile.WriteLine("Data/png-transparent-arrow-arrow-angle-triangle-black-thumbnail.jpg")
		objFile.WriteLine("Data/purple_envelope_sample_Page_1.jpg")
		objFile.WriteLine("Data/purple_envelope_sample_Page_2.jpg")
		objFile.WriteLine("Data/Proofs.rep")
		objFile.WriteLine("dist/")
		objFile.WriteLine("MVC/")
  		objFile.WriteLine("Dependancies/")
  		objFile.WriteLine("ImportService/")
		objFile.WriteLine("index.asp")
		'objFile.WriteLine("web.config")
		WshShell.Exec("git pull origin master")
		objFile.Close
		Do until fso.FileExists(DeployRoot & "test\App\app.config.asp")
		WScript.Sleep 250
		Loop
	Set objFile = fso.OpenTextFile(DeployRoot & "test\App\app.config.asp",1)
	Dim readConfigFile:readConfigFile = objFile.ReadAll
	objFile.Close
	Dim devToFalseSetting:devToFalseSetting = Replace(readConfigFile,"dev = true","dev = false")
	Set objFile = fso.OpenTextFile(DeployRoot & "test\App\app.config.asp",2)
	objFile.Write(devToFalseSetting)
	objFile.Close
End Sub 

'"F:\Development\Tracking_Kits\CiCd\test\App\app.config.asp"
'C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:tracking
'C:\Windows\System32\inetsrv\appcmd.exe stop site /site.name:tracking

'C:\Windows\System32\inetsrv\appcmd.exe start apppool /apppool.name:tracking
'C:\Windows\System32\inetsrv\appcmd.exe start site /site.name:tracking

'nircmd.exe elevatecmd runassystem