;;============================================================================
;; ConfigurePrinter
;;
;; Called from FullInstall
;; OR
;; from ChangePrinter
;;
;; Screen is cleared; DO have to clear before exit
;;
;; Backs up Printer.Cfg (sets flag vbles);
;; Get files from group 3;
;; Run PrnMenus ...
;;
;;   PrnMenus.EXE replaces Instalit script: does branching menus, etc.
;;   Exits w/nonzero if err or Escape
;;
;; If it returns OK (0), call SavePrinterConfig
;;


;; Procedure	ConfigurePrinter


;; DEBUG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;; Popup PRINTER.INF script now executing.
;; END DEBUG.


	[PrinterErrOrEscape] := YesCharacter

	if [FullInstallAttempted] = NoCharacter
		Do BackupPrinterCfg
	endif


	LetButton <Esc> Do Nothing
	LetButton <Esc> Do PrinterChildEscape

	;;////////////////////////////////////////////////////////////////////

	;; -------------------------------------
	;; Uncompress/copy temp prn config files

	;;Do BackgroundWriteBegin
	SetShadows Off
	SetPopupBorderTo NONE
	SetPopupAttrTo  BlackOnCyan

	TextBox @xy(Center, 4)
		Copying temporary files for printer configuration.
	EndTextBox

	TextBox @xy(Center, 5)
		One moment, please...
	EndTextBox


	TextBox @xy(Center, 25)
		[EscMsg]
	EndTextBox

	;;Do BackgroundWriteEnd
	SetPopupAttrTo   LightCyanOnBlue
	SetPopupBorderTo SINGLE
	SetShadows On


	;; Que printer config files in Group 3:
	;; Mfr menu, printer type menus (one for each mfr), 
	;; PrnMenus.EXE, CfgPrn.EXE, 
	;; PRINTER.INF,
	;; BUT NOT PRINTERS.PDB, which is the only file in Group 4
	;; (we que and get *that* only if/when we're really going to
	;; configure the printer).

	QueFileGroup 3 to [InstallationDirectory]

;; DEBUG
;;	Popup Size of 'qued' files in bytes = QueSize
;; end DEBUG



;; Thursday, 10/28/1993
;; In case user does Ctrl-X at disk swap
;;
		SetExitMessageTo "Run Setup again to configure your printer."


;; Thursday, 10/28/1993
;;
;; Remove Esc handler for duration of GetQuedFiles
;; Surround with "SetAllowExit On/ ... Off"
;; THIS PERMITS ESCaping out of file xfer (!)
;;
;; Commented out 'cause Instalit doesn't handle properly:
;; If Esc hit in GetQuedFiles, Instalit puts up a box asking:
;;     Wanna quit installation? Yes/No/Ctrl-X
;; Hitting Ctrl-X does nothing; hitting N works properly;
;; hitting Y terminates the installation, but 9 times out of 10
;; gives rise to an errmsg box:
;;     A DOS critical error has occured (sic!)
;;     Hit Esc to stop trying
;; Hitting Esc causes the Instalit to exit w/a clear screen and
;; no exit msg.
;;
;;	LetButton <Esc> Do Nothing
;;	SetAllowExit On

	GetQuedFiles OnePerLine							;; NoInterruption

;; Thursday, 10/28/1993
;; Restore SetAllowExit [Off]
;; Restore Esc handler
;;
;;	SetAllowExit Off
;;	LetButton <Esc> Do PrinterChildEscape



	ClearScreen


	if QueSize <> 0					;; Fatal! (cleanup: PrinterChildEscape)

		Dialog PressAKey WhiteOnRed
			We were unable to extract or copy 
			all of the printer configuration files.
			Installation connot continue.

		EndDialog

		Do PrinterChildEscape
	endif



	;;------------------
	;; Look busy:

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

	;;////////////////////////////////////////////////////////////////////

;; DEBUG
;; LetButton <Esc> Do Nothing
;; SetTraceMode On
;; end DEBUG

;; DEBUG
;; Popup Free run memory before "RUN PrnMenus ..." = FreeRunMemory
;; end DEBUG

	;; ------------------------------------------------------------
	;; Run PrnMenus.  Program exitcode nonzero IFF error or Escape.
	;;
	;; -E:y                        :    EscapeAllowed:yn
	;; -A:0x1B                     :    popup attr = 0x1b = LightCyanOnBlue
	;; -a:0x30                     :    background attr = BlackOnCyan
	;; -!:0x4f                     :    attention attr  = BlackOnCyan
	;; -U:![FullInstallAttempted]  :    -UseExistingCfgFile:yn
	;; [PrinterConfigFileName]     :    fn
	;; -M:[PrinterMfrMenuName]     :    -MfrMenuName:fn
	;; -P:[NumParallelPorts]       :    -ParallelPorts:n


	;; Use String20 because at least we know it's "defined" (goddam language)

	if [FullInstallAttempted] = YesCharacter
		[String20] := n
	else
		[String20] := y
	endif
	

	;; Set global flags.  If Esc occurs, it WILL be in PrnMenus.EXE
	[NewPrinterConfigFileExists] := NoCharacter
	[PrnMenusEscape] := YesCharacter


	Run PrnMenus [MyProgFixedOptions] -a:0x30 -!:0x4f -U:[String20] -P:[NumParallelPorts] [PrinterConfigFileName] -M:[PrinterMfrMenuName] [RunOptions]


;; DEBUG
;; LetButton <Esc> Do Nothing
;; SetTraceMode On
;; end DEBUG
	;; Make sure it ran & didn't return error
	[String20] := PrnMenus.EXE
	Do CheckStr20Ran			;; sets [EXEexitCode]


	;; Exit code is nonzero if error or user Esc'd
	;; OR user picked "Don't setup a printer" ( exit code == 1);
	;; in all but the last case, abort installation; in the last case,
	;; just continue but skip rest of printer config stuff.

	if [EXEexitCode] <> 0
		ClearScreen

		if [EXEexitCode] = 1			;; "Don't setup a printer" chosen

			;; In this case, Setup continues
			;; (this is not an error condition).
			goto PrinterRET
		else
			;; In this case, bail out of Setup
			Do PrinterChildEscape
		endif

	endif



	;; After this point, if Esc occurs, it won't be in PrnMenus.EXE
	[PrnMenusEscape] := NoCharacter


	;; This is a reality check, so we'll keep it,
	;; even though the "else" case should be impossible

	if FileExists [InstallationDirectory]\[PrinterConfigFileName]
	else
		Dialog PressAKey WhiteOnRed
			[InstallationDirectory]\[PrinterConfigFileName] does not exist !
			Setup cannot proceed.

		EndDialog

		ClearScreen
		Do PrinterChildEscape

	endif


	;; All went well.
	;; Monday, 8/23/1993, new:
	;; FIRST, clean up printer cfg files we no longer need
	;; (about 380K worth - minimize amount of disk space needed).

	DeleteFiles from [InstallationDirectory] Quietly
		*.MNU
		MENUTIL.EXE
		PRNMENUS.EXE
	EndDeleteFiles


	;; Set global flag, run CfgPrn.EXE, etc.
	[NewPrinterConfigFileExists] := YesCharacter

	Do SavePrinterConfig


:PrinterRET
	[PrinterErrOrEscape] := NoCharacter

	ClearScreen

	Do WorkingPrinter

	Return


;; EndProcedure	;; ConfigurePrinter


;;============================================================================
;; BackupPrinterCfg

Procedure       BackupPrinterCfg

	[PrinterConfigFileBackedUp] := NoCharacter

	;; Do nothing if config file doesn't exist
	if FileExists [InstallationDirectory]\[PrinterConfigFileName]
		[OldPrinterConfigFileExists] := YesCharacter
	else
		[OldPrinterConfigFileExists] := NoCharacter

		Exit
	endif

	;; Delete any existing backup file
	DeleteFiles from [InstallationDirectory] Quietly
		[PrinterConfigFileBKName]
	EndDeleteFiles

	CopyFiles from [InstallationDirectory] to [InstallationDirectory] Quietly
		[PrinterConfigFileName] as [PrinterConfigFileBKName]
	EndCopyFiles

	[PrinterConfigFileBackedUp] := YesCharacter

EndProcedure    ;; BackupPrinterCfg


;;----------------------------------------------------------------------------
;; BackupPsPdb

Procedure       BackupPsPdb

	[PsPdbBackedUp] := NoCharacter

	;; Do nothing if PS.PDB doesn't exist
	if FileExists [InstallationDirectory]\[PsPdbName]
		[PsPdbExists] := YesCharacter
	else
		[PsPdbExists] := NoCharacter

		Exit
	endif

	;; Delete any existing backup file
	DeleteFiles from [InstallationDirectory] Quietly
		[PsPdbBKName]
	EndDeleteFiles

	RenameFile [InstallationDirectory]\[PsPdbName] to [PsPdbBKName]

	[PsPdbBackedUp] := YesCharacter

EndProcedure    ;; BackupPsPdb


;;----------------------------------------------------------------------------
;; SavePrinterConfig
;;
;; Copy printers.pdb floppies->harddisk
;;
;; Backup PS.PDB
;;
;; Runs CfgPrn.EXE w/relevant args
;;
;; Screen is cleared; don't have to clear it before Exit.
;;----------------------------------------------------------------------------

Procedure       SavePrinterConfig

;; DEBUG
;;LetButton <Esc> Do nothing
;;SetTraceMode On
;; end DEBUG

	Do BackgroundWriteBegin3

	TextBox @xy(Center, 5)
		Looking up your printer in printer database...
	EndTextBox


	TextBox @xy(Center, 25)
		[EscMsg]
	EndTextBox

	Do BackgroundWriteEnd3



	QueFileGroup 4 to [InstallationDirectory]


;; Thursday, 10/28/1993
;; It appears that if there's an Esc handler installed
;; and user hits Esc when prompted by Instalit to change disks
;; (or if prompted by Instalit for most anything, actually),
;; then a crash ensues.
;; Since disk change occurs in the middle of this file xfer,
;; we disable the Esc handler for the duration of the xfer.
;; 
;; NOTE:
;; Because SetAllowExit is ON for the duration of the GetQuedFiles,
;; the user CAN Esc out of the routine (!!!)
;; Unfortunately, Instalit doesn't handle this very well:
;;
;; If Esc hit in GetQuedFiles, Instalit puts up a box asking:
;;     Wanna quit installation? Yes/No/Ctrl-X
;; Hitting Ctrl-X does nothing; hitting N works properly;
;; hitting Y terminates the installation, but 9 times out of 10
;; gives rise to an errmsg box:
;;     A DOS critical error has occured (sic!)
;;     Hit Esc to stop trying
;; Hitting Esc causes the Instalit to exit w/a clear screen and
;; no exit msg.
;;
;; Because we need to allow the user to bail out at disk-swap,
;; we have to use this wrapper code (LetButton.../SetAllowExit...).
;; We just have to hope the user does NOT pay attention to the
;; prompt on the bottom line...
;;

;; Thursday, 10/28/1993
;;
;; Remove Esc handler for duration of GetQuedFiles
;;
	LetButton <Esc> Do Nothing

;; Thursday, 10/28/1993
;; Surround with "SetAllowExit On/ ... Off"
;; THIS PERMITS ESCaping out of file xfer (!)
;; Let's see if we can clean up properly...
;;
;; Sunday, 10/31/1993 - Since this is unreliable, we'll just omit it here too:
;;
;;	SetAllowExit On

	GetQuedFiles OnePerLine							;; NoInterruption

;; Sunday, 10/31/1993 "On" commented out above
;;	SetAllowExit Off
;;

;; Thursday, 10/28/1993
;;
;; Restore Esc handler
;;
	LetButton <Esc> Do PrinterChildEscape


	;;
	;; ??? Will this happen if user hits Esc when prompted
	;;     to change disks ???
	;;     HOPE SO !
	;;

	if QueSize <> 0					;; Fatal! (cleanup: PrinterChildEscape)

		Dialog PressAKey WhiteOnRed
			We were unable to extract or copy 
			all of the printer configuration files.

			Run Setup again to configure your printer.

		EndDialog

		Do PrinterChildEscape

	endif


	;; Backup PS.PDB
	Do BackupPsPdb


	;; -------------
	;; Paint screen

	ClearScreen

	Do BackgroundWriteBegin3

	TextBox @xy(Center, 6)
		Now configuring your printer.
	EndTextBox


	TextBox @xy(Center, 25)
		[EscMsg]
	EndTextBox

	Do BackgroundWriteEnd3


	Do WorkingPrinter


	;; ----------------------------------------------------------
	;; Run CfgPrn.  Program exitcode nonzero IFF error or Escape.

	Run CfgPrn [MyProgFixedOptions] -P:Printers.PDB [PrinterConfigFileName] [RunOptions]


	;; Make sure it ran & didn't return error
	[String20] := CfgPrn.EXE
	Do CheckStr20Ran			;; sets [EXEexitCode]

	;; Exit code is 0 if error or user Esc'd; bail out.

	if [EXEexitCode] <> 0
		ClearScreen
		Do PrinterChildEscape
	endif



	;; All went well.
	[PrinterConfigured] := YesCharacter

	Exit

EndProcedure    ;; SavePrinterConfig

;;============================================================================

Procedure	WorkingPrinter

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

EndProcedure	;; WorkingPrinter

;;----------------------------------------------------------------------------
;; CheckStr20Ran
;;
;; Assumes [String20] = progname
;;
;; Sets [EXEexitCode] := OSExitCode


Procedure	CheckStr20Ran

	If OSErrorCode <> 0

		ClearScreen

		Do GetDOSErrCodeStr19

		Dialog  PressAKey WhiteOnRed			;; OS was unable to run prog
			Unable to run [InstallationDirectory]\[String20] -
			[String19]

			Installation cannot proceed.
		EndDialog

		Do PrinterChildEscape
	EndIf


	[EXEexitCode] := OSExitCode


EndProcedure	;; CheckStr20Ran


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

Procedure GetDOSErrCodeStr19

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

	Exit

EndProcedure ;; GetDOSErrCodeStr19

;;============================================================================

Procedure       BackgroundWriteBegin3

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

	Exit

EndProcedure            ;; BackgroundWriteBegin3

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

Procedure       BackgroundWriteEnd3

	SetPopupAttrTo   LightCyanOnBlue
	SetPopupBorderTo SINGLE
	SetShadows On

	Exit

EndProcedure            ;; BackgroundWriteEnd3

;;============================================================================
;; [PrinterErrOrEscape] = YesCharacter

Procedure	PrinterChildEscape

	Return	;; to parent script


EndProcedure	;; PrinterChildEscape

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

