' SoftDistSetup VBScript source code
Option Explicit
On Error Resume Next

dim objNet, objShell, objNetDrives, objWinShell
dim intIndex, intLtr, intOKCancel, intYesNo
dim blnNamed, blnMapped, bln98
dim strUNC,strInstaller, str98Path, str98Installer, strUser, strPasswd
Const Reserved_Net_Drive = -2147023694
Const Used_Locally = -2147024811
Const Can_Not_Map = -2147024843
Const Logon_Failure = -2147023570
Const NetName_Not_Found = -2147024829
Const UNC_98 = "\\softdist2\oracle"

blnNamed = False
blnMapped = False
bln98 = False
intLtr = 90	'The integer value of "Z".
strUNC = "\\softdist2.tss.oregonstate.edu\oracle"	'Must be all lower case.
strInstaller = ":" & "\10\installer\remove_oracle_9.2.0.1.bat"	'Must be all lower case.
str98Path = ""		'Must be all lower case. Reminder: 98 can only map to the sharepoint.
str98Installer = "\10\installer\remove_oracle_9.2.0.1.bat"	'Must be all lower case. Must include the entire path from the sharepoint.

Set objShell = Wscript.CreateObject("Wscript.Shell")
Set objNet = Wscript.CreateObject("Wscript.Network")
Set objNetDrives = objNet.EnumNetworkDrives
Set objWinShell = CreateObject("Shell.Application")

	'Check to see if the UNC is already mapped
For intIndex = 1 to objNetDrives.Count - 1 Step 2
	If LCase(objNetDrives.Item(intIndex)) = strUNC or LCase(objNetDrives.Item(intIndex)) = UNC_98 Then
		intLtr = ASC(Left(objNetDrives.Item(intIndex - 1),1))
		blnNamed = True
		blnMapped = True
		If LCase(objNetDrives.Item(intIndex)) = UNC_98 Then
			bln98 = True
			strUNC = UNC_98
			strInstaller = str98Installer
		End If
	End If
Next
intIndex = 0

	'Starting with "Z" check to see if the letter is in use. If it is,decrement one letter until 
	'you find a drive letter that is free.
While not blnNamed
	For intIndex = 0 to objNetDrives.Count - 1 Step 2
		If Left(objNetDrives.Item(intIndex),1) = Chr(intLtr) Then
			intLtr = intLtr - 1
		End If
	Next
	blnNamed = True
Wend

	'Once you have a drive letter, map the UNC to the drive.
If not blnMapped Then
	objNet.MapNetworkDrive (Chr(intLtr) & ":"),strUNC
	If Err.number <> 0 Then
		'wscript.echo "1." & Err.number & vbCr & err.Source & vbCr & err.Description
		If Err.number = NetName_Not_Found Then
			Err.Clear
			blnNamed = False
			bln98 = True
			strUNC = UNC_98
			strInstaller = str98Installer
		End If
		If Err.number = Reserved_Net_Drive or Err.number = Used_Locally Then
			blnNamed = False
			Err.Clear
		End If
		While not blnNamed
		'wscript.echo "1a." & Err.number & vbCr & err.Source & vbCr & err.Description & "blnNamed: " & blnNamed
			intLtr = intLtr - 1
			Err.Clear
			objNet.MapNetworkDrive (Chr(intLtr) & ":"),strUNC
			If Err.number <> Reserved_Net_Drive and err.number <> Used_Locally Then
				blnNamed = True
			End If
		Wend
		'wscript.echo "2." & Err.number & vbCr & err.Source & vbCr & err.Description

		If Err.number = Can_Not_Map or Err.number = Logon_Failure Then
			Err.Clear
			intOKCancel = MsgBox("An error occurred connecting you to the installation site. " &_
				"Your current credentials have not provided you access to the site. " & vbCr &_
				"You will next be asked to enter the logonid (<domainname>\<logonid>) you provided to the Software Licensing Manager and your password." & vbCr & vbCr &_
				"**(Please note: A DOS window will appear asking for your password. Although no characters appear on the screen as you type," &_
				"your password is being entered.)**",vbOKCancel,"Authentication Error")

				'Get proper credentials to map the drive if their default credentials don't provide access.
			If intOKCancel = vbOK Then
				blnMapped = False
				While Not blnMapped
					strUser = InputBox("Enter Logon ID","Logon ID")
					If strUser = "" then
						Wscript.Quit()
					End if
					objShell.Run ("net use " & Chr(intLtr) & ": " & """" & strUNC & """" & " * /user:" & strUser),,True
					Set objNetDrives = objNet.EnumNetworkDrives
					For intIndex = 1 to objNetDrives.Count - 1 Step 2
						If LCase(objNetDrives.Item(intIndex)) = strUNC or LCase(objNetDrives.Item(intIndex)) = UNC_98 Then
							blnMapped = True
							Exit For
						End If
					Next
					If Not blnMapped Then
						MsgBox "An error has occurred and your connection to the server was not successful. " &_
						"This may be due to an incorrect user name or password. Please try again keeping in mind that " &_
						"your logonid consists of three parts: domain name (CN,ONID,etc.), a '\' (backslash), and your logonid (ex: SmithJ). " &_
						"If you continue to have problems, please call the Software Licensing Manager.",,"Error!"
					End If
				Wend
			Else
				Wscript.Quit()
			End IF
		End If
		If Err.number <> 0 Then
		'wscript.echo "4." & Err.number & vbCr & err.Source & vbCr & err.Description
			MsgBox "An Unexpected error has occurred. To resolve this problem please provide the" & vbCrLf &_ 
				"Campus IT Software Licensing Manager with the following information:" & vbCrLf & vbCrLf &_
				"Error Number: " & Err.number & vbCrLf & "Description: " & Err.Description,,"Unexpected Error"
			Wscript.Quit()
		End If
	End If
End If
objWinShell.MinimizeAll

	'Run the setup program.
If bln98 Then
	'wscript.echo "Drive: " & Chr(intLtr)& ":\" & str98Path
	objShell.Run Chr(intLtr)& ":\" & str98Path & "\" & str98Installer
Else
	objShell.Run Chr(intLtr) & strInstaller
End If

Set objNetDrives = Nothing
Set objShell = Nothing
Set objNet = Nothing
SetobjWinShell = Nothing
Wscript.quit
