;;============================================================================
;; GetDrive
;;
;; Called by FullInstall ONLY
;;
;; Sets:
;;      [DriveLetterGood] := xxxCharacter (xxx = Yes or No)
;;      [DriveLetter]     := if [DriveLetterGood] = YesCharacter,
;;                           this is a letter of a drive w/enough space
;;      [InstallDir]      := [DriveLetter]:\PEOPLE if 'drive good'
;;
;; Current implementation uses [HDSpaceNeeded] instead of calling QueSize
;; Change ?????
;; If we DO change, then FullInstall will have to 'que' the appropriate files
;;
;;============================================================================

;; UNDONE:
;; A few vbles here can be changed to builtins [NumberN], [StringX]
;; e.g. [NumDrives], ? [GoodDrivePos] ?, [DriveLetterMegs], [DriveLetterSpace],
;;      [DriveMenuLine],


;; Procedure       GetDrive


	LetButton <Esc> Do nothing
	LetButton <Esc> Do GetDriveEscape

	[GetDriveUserEscape] := NoCharacter


	;; Make sure disk in installation drive is not write-protected !
	Do NoWriteProtect

	;; Thursday, 9/23/1993
	;; New:
	[SwapSpaceNeeded] := 640 * [OneK]
	[SwapDriveLetter] := ?

	;; end New Thursday, 9/23/1993


	[DriveLetter] := C


	;; Put up box saying "Checking drives - one moment, please..."
	;; or whatever
	;; Box longest string = 48 + 14 = 62
	;; Box width = 62 + 2 * 2 (border & one space on both sides)
	;; Hence, horiz slack = 14, so box shd be at col 14/2 = 7

	[CheckingDriveMsg] := "Checking drive"
	[FixedDriveList]   := "Fixed drives: "


	Do GetDriveProgressBox


	[NumDrives] := 0


	Do DeleteDriveMenu	;; make sure there ain't one already !
						;; (mostly useful during debugging, 
						;;  but ya never know)

	AppendFile [GetDriveMenuNameFull]
		RETVALSGIVEN
		{
	EndAppendFile


	[DriveLetterGood] := NoCharacter

	[GoodDrivePos] := 0


;; GetDrive main loop

	while [DriveLetter] <= Z

		;; ----- Update "Checking drive _" ------
		Do UpdateCheckDrive



		;; --------- Check drive type -----------
		;; Drive must exist !,

		if DriveIsPresent [DriveLetter]
		else
			GoTo GDContinue
		endif


		;; and not be a floppy
		if DriveIsRemoveable [DriveLetter]
			if DriveIsBernoulli [DriveLetter]
				GoTo GDDriveOK
			else
				GoTo GDContinue
			endif
		endif


;;              ;; and be a fixed drive,
;;              if DriveClassOf [DriveLetter] <> FIXED
;;                      GoTo GDContinue
;;              endif

		;; and not be a RAM drive,
		if DriveIsRAM [DriveLetter]
			GoTo GDContinue
		endif

		;; and not be a CD-ROM,
		if DriveIsCDROM [DriveLetter]
			GoTo GDContinue
		endif

:GDDriveOK
		;; --------------------------------------
		;; We got another;
		;; bump count, add drive to list on screen

		IncrementNumber [NumDrives]
		Do UpdateFixedDrives

		;; ------------------------------------------
		;; Set menu text and returnval for this drive
		;; ( if !enough space, -1, else [NumDrives] )

		[DriveLetterSpace] := FreeSpaceOnDrive [DriveLetter]
		[DriveLetterMegs]  := [DriveLetterSpace] / [OneMeg]


		;; Thursday, 9/23/1993
		;; New: 
		if  [SwapSpaceNeeded] <= [DriveLetterSpace]
			if [SwapDriveLetter] = ?

				;; We have the first drive with enough space to swap to.
				[SwapDriveLetter] := [DriveLetter]

			endif
		endif

		;; end New Thursday, 9/23/1993


		;; Drive letter, colon, local or fixed
		if DriveIsRemote [DriveLetter]
			[DriveMenuLine] := [DriveLetter]:   network fixed drive   (
		else
			[DriveMenuLine] := [DriveLetter]:   local fixed drive     (
		endif

		;; Append free space estimate
		if [DriveLetterMegs] = 0
			[DriveMenuLine] := [DriveMenuLine]less than one megabyte available)
		else
			if [DriveLetterMegs] > 1
				[DriveMenuLine] := [DriveMenuLine]about [DriveLetterMegs] megabytes available)
			else
				[DriveMenuLine] := [DriveMenuLine]about 1 megabyte available)
			endif
		endif


		;; --------- Add retval to line ------------
		if FreeSpaceOnDrive [DriveLetter] < [HDSpaceNeeded]
			[DriveMenuLine] := [DriveMenuLine] -1
		else
			[DriveMenuLine] := [DriveMenuLine] [NumDrives]

			if [GoodDrivePos] = 0
				[GoodDrivePos]   := [NumDrives]
				[FirstGoodDrive] := [DriveLetter]
			endif
		endif

		;; --------- Add new line(s) for drive -----
		AppendFile [GetDriveMenuNameFull]
			[DriveMenuLine]
		EndAppendFile


:GDContinue
		;; Loop again for next drive
		IncrementString [DriveLetter]

	EndWhile


	;; -----------------------------------------
	;; Get rid of "checking drives, etc."


	ClearScreen


	if [GoodDrivePos] = 0   ;; there aren't any drives w/enough mem !

		LetButton <Esc> Do nothing

		Dialog PressAKey WhiteOnRed

			.cInstallation cannot proceed.

			You'll need [HDMegsNeeded] megabytes of disk space for
			this installation step.  However, there is no fixed drive
			with that much free space.

			Free some drive space by deleting unnecessary files, 
			and run SETUP again.

		EndDialog

		SetExitMessageTo "Try again later."
		Do GetDriveEscape
	endif


	;; Check for only one hard drive (which DOES have enough space)

	if [NumDrives] = 1

		ClearScreen

		Dialog 1500
			This computer has a single hard drive, [FirstGoodDrive]:
		EndDialog

		[DriveLetter] := [FirstGoodDrive]

		goto GDgood

	endif



	;; "Working" TextBox
	Do Working



	AppendFile [GetDriveMenuNameFull]
		}
	EndAppendFile


	Do BackgroundWriteBegin2

	TextBox @XY( Center, 6 )
		Select a Disk Drive for Installation
	EndTextBox

	TextBox @XY( Center, 7 )
		You'll need [HDMegsNeeded] megabytes of disk space for this installation.
	EndTextBox
	TextBox @XY( Center, 9 )
		Drives with insufficient space cannot be selected.
	EndTextBox


	TextBox @XY( Center, 23 )
		[VArrowsMsg]
	EndTextBox

	TextBox @XY( Center, 25 )
		[EnterEscMsg]
	EndTextBox

	Do BackgroundWriteEnd2


:GDRunMenu

	;; Now run MENUTIL to get choice.
	;; Set up name of output file in [MenutilOutfile].
	;; Build its cmdline in [String1]

	[MenutilOutfileDir] := [InstallFromDirectory]
	[MenutilOutfile]    := [MenutilOutfileDir]\[MenutilOutfileNameExt]

	[String1] := [MyProgFixedOptions] -R:4 -y:12
	[String1] := [String1] -M:20 -S:[GoodDrivePos]
	[String1] := [String1] -O:[MenutilOutfile] [GetDriveMenuNameFull]


	;; Thursday, 9/23/1993
	;; New:
	;; There's a drive with enough space for install,
	;; HENCE there's a drive with at least 640K free,
	;; HENCE there's a SwapDrive.
	;; SO: save current drive/dir in [SaveDirectory];
	;;     change drive/dir to swapdrive root before "Run".

	[SaveDirectory] := CurrentDirectory

	ChangeDirectoryTo [SwapDriveLetter]:\

	;; end New Thursday, 9/23/1993


	Run [InstallFromDirectory]\MENUTIL [String1] [RunOptions]


	;; Thursday, 9/23/1993
	;; New:
	;; Now restore drive/dir:

	ChangeDirectoryTo [SaveDirectory]

	;; end New Thursday, 9/23/1993


	;; Make sure it ran !

	If OSErrorCode <> 0

		ClearScreen

		Do GetDOSErrCodeStr19

		Do DeleteDriveMenu

		Dialog  PressAKey WhiteOnRed       ;; OS was unable to Run program
			Unable to run [InstallFromDirectory]\MENUTIL.EXE.
			[String19]

			Installation cannot proceed.
		EndDialog

		Do GetDriveEscape
	EndIf


	;; Return val is asciified long in [MenutilReturnNum]
	;; If 0, error or user Esc'd; bail out.
	;; Also, if 0, MENUTIL did *not* write an output file
	;; (so we don't have to clean it up).

	if OSExitCode = 0

		ClearScreen

		;; Delete the script we created
		Do DeleteDriveMenu

		Do GetDriveEscape

	endif


	;; Get values returned in file [MenutilOutfile], and deletes file.
	;; Sets:
	;;    [MenutilReturnNum]
	;;    [MenutilReturnText]

	Do GetMenutilReturnValues

	if [MenutilReturnNum] = NoCharacter
		Do GetDriveEscape
	endif


	;; Get selected drive letter
	[DriveLetter] := SubstringOf [MenutilReturnText] from 1 to 1



;; THIS SHOULD BE IMPOSSIBLE:
	if FreeSpaceOnDrive [DriveLetter] < [HDSpaceNeeded]
		Popup Insufficient space on drive [DriveLetter], please choose another.
		Goto GDRunMenu
	endif


:GDgood
	;; Come here from:
	;;   either falling through,
	;;   or     if there's but one fixed drive, and it has enough space
	;;
	;; [DriveLetter] is set.

	[InstallDir] := [DriveLetter]:\PEOPLE


	[DriveLetterGood] := YesCharacter

	;; [DriveLetter] is a usable drive,
	;; and [InstallDir] holds it + :\PEOPLE (fwiw)

:GDGetOut

	;; Clean up screen
	ClearScreen

	;; Delete the script we created
	Do DeleteDriveMenu

	Return				;; to parent script


;; EndProcedure            ;; GetDrive


;; --------------------------------------------------------------------------

Procedure	GetDriveEscape

	[GetDriveUserEscape] := YesCharacter
	Return

EndProcedure	;; GetDriveEscape

;; --------------------------------------------------------------------------
;; NoWriteProtect
;; Make sure disk in installation drive is not write-protected !

Procedure       NoWriteProtect


	LetButton <Esc> Do nothing


	if DriveIsRemoveable [InstallFromDrive]

		while MediaIsWriteProtected [InstallFromDrive]

			Dialog PressAKey WhiteOnRed
				The disk in drive [InstallFromDrive] is write-protected.

				Please unprotect it now!

			EndDialog

		EndWhile

	endif


	LetButton <Esc> Do GetDriveEscape


EndProcedure    ;; NoWriteProtect

;; --------------------------------------------------------------------------

;; --------------------------------------------------------------------------
;; "Working" TextBox

Procedure       Working

	TextBox @XY(Center,Center)
		One moment, please...
	EndTextBox

EndProcedure    ;; Working


;; --------------------------------------------------------------------------
;; Get rid of "Working" TextBox

Procedure       NoWorking

	Do BackgroundWriteBegin

	TextBox @XY( Center, 11 )
		"                              "
		"                              "
		"                              "
		"                              "
		"                              "
	EndTextBox


	Do BackgroundWriteEnd


EndProcedure    ;; NoWorking


;; --------------------------------------------------------------------------
;; Assumes OSErrorCode <> 0

Procedure GetDOSErrCodeStr19

	if OSErrorCode = 8
		[String19] := Insufficient memory.
	else
		[String19] := ""	;; DOS error code was OSErrorCode.
	endif

	Exit

EndProcedure ;; GetDOSErrCodeStr19

;; --------------------------------------------------------------------------
;; DeleteDriveMenu
;; --------------------------------------------------------------------------

Procedure       DeleteDriveMenu

	DeleteFiles from [InstallFromDirectory] Quietly
		[GetDriveMenuName]
	EndDeleteFiles

EndProcedure    ;; DeleteDriveMenu


;; --------------------------------------------------------------------------
;; GetDriveProgressBox
;; --------------------------------------------------------------------------

Procedure       GetDriveProgressBox

	TextBox

		 Examining disk drives.  One moment, please...

		 [CheckingDriveMsg] [DriveLetter]
		 [FixedDriveList]

	EndTextBox

EndProcedure

;; --------------------------------------------------------------------------
;; UpdateCheckDrive
;; --------------------------------------------------------------------------

Procedure UpdateCheckDrive

	Do GetDriveProgressBox

EndProcedure

;; --------------------------------------------------------------------------
;; UpdateFixedDrives
;; --------------------------------------------------------------------------

Procedure UpdateFixedDrives

	[FixedDriveList] := "[FixedDriveList] [DriveLetter]"
	Do GetDriveProgressBox

EndProcedure

;;============================================================================
;; MENUTIL helper fns
;;============================================================================


;;----------------------------------------------------------------------------
;; Get values returned in file [MenutilOutfile] and deletes that file
;; from [MenutilOutfileDir].
;; Sets:
;;    [MenutilReturnNum]
;;    [MenutilReturnText]
;;

Procedure	GetMenutilReturnValues


	;; If user escaped, leave quietly
;;	If [NoPrinterEscapeRecover] = YesCharacter
;;		Exit
;;	EndIf


	[MenutilReturnNum]  := 0
	[MenutilReturnText] := BlankString


	if FileExists [MenutilOutfile]
	else
		Dialog PressAKey WhiteOnRed
			.cInternal Error

			Drive menu file [MenutilOutfile] does not exist.
			Installation cannot proceed.

		EndDialog

		Exit
	endif


	ExamineTextFile [MenutilOutfile]
	
	[MenutilReturnNum]  := SettingFor [MenutilReturnNumKey]
	[MenutilReturnText] := SettingFor [MenutilReturnTextKey]

	ForgetTextFile


	DeleteFiles from [MenutilOutfileDir] Quietly
		[MenutilOutfileNameExt]
	EndDeleteFiles

EndProcedure    ;; GetMenutilReturnValues


;; --------------------------------------------------------------------------
;; --------------------------------------------------------------------------

Procedure       BackgroundWriteBegin2

	;; Write top area and bottom area text
	SetShadows Off
	SetPopupBorderTo NONE
	SetPopupAttrTo  BlackOnCyan

	Exit

EndProcedure            ;; BackgroundWriteBegin2

;;----------------------------------------------------------------------------

Procedure       BackgroundWriteEnd2

	SetPopupAttrTo   LightCyanOnBlue
	SetPopupBorderTo SINGLE
	SetShadows On

	Exit

EndProcedure            ;; BackgroundWriteEnd2

;;----------------------------------------------------------------------------
;;----------------------------------------------------------------------------

