' 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\datawrhs"

blnNamed = False
blnMapped = False
bln98 = False
intLtr = 90	'The integer value of "Z".
strUNC = "\\softdist2.tss.oregonstate.edu\datawrhs"	'Must be all lower case.
strInstaller = ":" & "\biquery\9\installer\setup.exe"	'Must be all lower case.
str98Path = ""	'Must be all lower case. Reminder: 98 can only map to the sharepoint.
str98Installer = "\biquery\9\installer\setup.exe" '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 Lena Ferris and your password." & vbCr & vbCr &_
				"**(Please note: Although the password will appear in clear text, " &_
				"it will be encrypted when it is sent across the network.)**",vbOKCancel,"Authentication Error")

				'Get proper credentials to map the drive if their default credentials don't provide access.
			If intOKCancel = vbOK Then
				strUser = InputBox("Enter Logon ID","Logon ID")
				If strUser = "" then
					Wscript.Quit()
				End if
				strPasswd = InputBox("Enter Password (A blank password is not allowed.)","Password")
				If strPasswd = "" then
					Wscript.Quit()
				End if
				objNet.MapNetworkDrive (Chr(intLtr) & ":"),strUNC,,strUser,strPasswd
				'wscript.echo "3." & Err.number & vbCr & err.Source & vbCr & err.Description
				If Err.number = Can_Not_Map or Err.number = Logon_Failure Then
					MsgBox "The credentials you have entered do not provide you access to this site." & vbCr &_
					"You may need to contact Lena Ferris to verify your logon credentials.",,"Logon Failure"
					Wscript.Quit()
				End If
			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
