Const UMDESKTOP_EXE = "%ProgramFiles%\UltraMon\UltraMonDesktop.exe"

Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
	
reload = False
wpUm = ""
wpBmp = ""

If WScript.Arguments.Count = 1 Then
	wpUm = WScript.Arguments(0)
	i = InStrRev(wpUm, ".")
	wpBmp = Left(wpUm, i) & "bmp"
	reload = True
Else
	'get the location of the user and shared wallpaper folders
	Dim dirWps(1)
	dirWps(0) = sh.RegRead("HKCU\Software\Realtime Soft\UltraMon\Wallpaper\Wallpaper Directory")
	dirWps(1) = sh.RegRead("HKLM\Software\Realtime Soft\UltraMon\Wallpaper\All Users Wallpaper Directory")
	
	For i = 0 To UBound(dirWps)
		If Right(dirWps(i), 1) <> "\" Then dirWps(i) = dirWps(i) & "\"
	Next
	
	'get name of current wallpaper
	curWp = sh.RegRead("HKCU\Control Panel\Desktop\Wallpaper")
	i = InStrRev(curWp, ".")
	wpUm = Left(curWp, i) & "wallpaper"
	
	'check if this is one of ours
	Dim strLen
	For i = 0 To UBound(dirWps)
		strLen = Len(dirWps(i))
		If Left(curWp, strLen) = dirWps(i) Then
			'it's one of ours, reload it
			reload = True
			wpBmp = curWp
			Exit For
		End If
	Next
End If

If reload = True Then
	'delete the wallpaper bitmap, then regenerate it and set it as the current wallpaper
	On Error Resume Next 'if the file doesn't exist, DeleteFile throws an exception
	fso.DeleteFile wpBmp
	On Error GoTo 0
	sh.Run """" & UMDESKTOP_EXE & """ /load " & wpUm
End If