'$DEFINE DEBUG

'$INCLUDE 'setupapi.inc'
'$INCLUDE 'msdetect.inc'

''Dialog ID's

CONST WELCOME                   = 100
CONST ASKQUIT                   = 200
CONST DESTPATH                  = 300
CONST EXITFAILURE               = 400
CONST EXITQUIT                  = 600
CONST EXITSUCCESS               = 700
CONST TOOBIG                    = 750

CONST VALIDATESIZE              = 1
CONST VALIDATECHIP              = 2
CONST INIDELETED                        = 3
CONST WINRESTART        = 4
CONST AUTODETECT                =6

'' Error IDs

CONST ERR_BADVGAMODE            = 8000
CONST ERR_BADCPU                        = 8001
CONST ERR_BADWINDOWSPATH= 8000
CONST ERR_BADPATH                       = 8000

''Bitmap ID

CONST LOGO                                      = 1

''Chip sets

CONST CIRRUS                            = 100
CONST EVEREX                            = 101
CONST PARADISE                          = 102
CONST TSENG                             = 103
CONST TRIDENT                           = 104
CONST T8900                             = 105
CONST ATIVGA                            = 106
CONST AHEADA                            = 107
CONST AHEADB                            = 108
CONST OAKTECH                           = 109
CONST VIDEO7                            = 110
CONST CHIPSTECH                 = 111
CONST TSENG4                            = 112
CONST GENOA                                     = 113
CONST NCR                                       = 114
CONST COMPAQ                            = 115
CONST UNKNOWN                           = -1

''GetDeviceCaps

CONST NUMCOLORS                 = 24
CONST SIZEPALETTE                       = 104

'' Global variable declaration

GLOBAL DEST$            ''Default destination directory.
GLOBAL WINDIR$          ''Windows directory.
GLOBAL INI$                     ''Windows directory.
GLOBAL CUIDLL$                  ''CUI DLL path

'' External function/procedure declaration

DECLARE SUB ConfirmQuit
DECLARE FUNCTION MakePath (szDir$, szFile$) AS STRING
DECLARE FUNCTION GetVideoInfo LIB "whatvga.dll" () AS INTEGER
DECLARE FUNCTION GetDC LIB "USER.EXE" (hwnd%) AS INTEGER
DECLARE FUNCTION ReleaseDC LIB "USER.EXE" (hwnd%, hdc%) AS INTEGER
DECLARE FUNCTION GetDeviceCaps LIB "GDI.EXE" (hdc%, iCapabilities%) AS INTEGER
DECLARE FUNCTION GetColors() AS INTEGER

INIT:

	'' Set up the error traps

	ON ERROR GOTO ERRQUIT

	'' Some common stuff used thru out the install

	CUIDLL$ = "mscuistf.dll"            ''Custom user interface dll

	'' Set up the way the install program looks

	SetBitmap CUIDLL$, LOGO
	SetTitle "Matinee Setup"

	'' Make sure proper hardware is there

	if GetProcessorType() < 3 then

		'' We aren't on a 386 or above. FATAL!

		error ERR_BADCPU

	end if

	if GetColors() < 256 then

		'' We don't have a VGA driver with 256 colors installed. FATAL!

		error ERR_BADVGAMODE

	end if

	'' Load in the INF file

	szInf$ = GetSymbolValue("STF_SRCINFPATH")
	IF szInf$ = "" THEN
		szInf$ = GetSymbolValue("STF_CWDDIR") + "MATINEE.INF"
	END IF
	ReadInfFile szInf$

	'' Set the size checking mode

	i% = SetSizeCheckMode(scmOnIgnore)

	'' Check that windows diectory is writable

	WINDIR$ = GetWindowsDir()
	i% = IsDirWritable(WINDIR$)
	if i% = 0 then

		'' Error: can't write to windows directory. exit.

	error ERR_BADWINDOWSPATH

	endif

	'' setup the ini path

	ini$ = MakePath(WINDIR$, "CONTROL.INI")

WELCOME:

	sz$ = UIStartDlg(CUIDLL$, WELCOME, "FInfoDlgProc", 0, "")
	IF sz$ = "CONTINUE" THEN
	UIPop 1
	ELSE
	ConfirmQuit
	GOTO WELCOME
	END IF

	'' Do the main thing

GETPATH:

	'' Set up symbols as parameters for dialog box

	SetSymbolValue "EditTextIn", "C:\MATINEE"
	SetSymbolValue "EditFocus", "ALL"

GETPATHL1:

	sz$ = UIStartDlg(CUIDLL$, DESTPATH, "FEditDlgProc", 0, "")
	DEST$ = GetSymbolValue("EditTextOut")

	IF sz$ = "CONTINUE" THEN

		'' Make sure our target is writable

	IF IsDirWritable(DEST$) = 0 THEN
	error ERR_BADPATH
	GOTO GETPATHL1
	END IF

	UIPop 1
	ELSEIF sz$ = "REACTIVATE" THEN
	GOTO GETPATHL1
	ELSE
	ConfirmQuit
	GOTO GETPATH
	END IF

	'' prepare directories and symbols for them

	SrcDir$ = GetSymbolValue("STF_SRCDIR")
	CreateDir DEST$, cmoNone

	'' Add standard files to the copy list

'' i% = DoMsgBox( "Files", "Matinee", MB_OK )
	AddSectionFilesToCopyList "Files", SrcDir$, DEST$
'' i% = DoMsgBox( "Background", "Matinee", MB_OK )
	AddSectionFilesToCopyList "Background", SrcDir$, MakePath(DEST$, "backgrnd")
'' i% = DoMsgBox( "ScreenSaver", "Matinee", MB_OK )
	AddSectionFilesToCopyList "ScreenSaver", SRCDir$, WinDir$

'' Determine if there is enough disk space for the installation.
'' DiskSp& will be > 0 if there is NOT enough space. It will be 0 if there
'' is sufficient free disk space for the installation
   DiskSp& = GetCopyListCost( szExtra$, szCosts$, szNeeded$ )

'' Tell user how much more space is needed.
   if ( DiskSp& > 0 ) then
		kbytes% = DiskSp& / 1000
		SPC$ = str$( kbytes% )
		SetSymbolValue "SpaceNeeded", SPC$
     sz$ = UIStartDlg(CUIDLL$, TOOBIG, "FTooBigDlgProc", 0, "")
     UIPop 1
     error STFQUIT
   endif

	'' Set up the defaults before video DLL selection

	VgaType$ = "dib"
	IsDIB$ = "1"

	'' Ask the user and see if they want to use auto detect

CHECKAUTODETECT:

	AddListItem "CheckItemsIn", "ON"
	AddListItem "OptionsGreyed", ""
	sz$ = UIStartDlg(CUIDLL$, AUTODETECT, "FCheckDlgProc", 0, "")

	if sz$ = "CONTINUE" then
	UIPop 1
	ELSE
	ConfirmQuit
	GOTO CHECKAUTODETECT
	END IF

	if GetListItem("CheckItemsOut", 1) = "ON" then

		'' Attempt to detect the VGA chipset type. GetVideoInfo() is in WHATVGA.DLL

		VideoType% = GetVideoInfo()

	else
		VideoType = 0
	endif

	select case VideoType
		case TSENG
			Index$ = "1"
		case TSENG4
			Index$ = "1"
		case TRIDENT
			Index$ = "2"
		case VIDEO7
			Index$ = "3"
		case CIRRUS
			Index$ = "4"
		case COMPAQ
			Index$ = "5"
		case else
			Index$ = "6"
	end select

GetType:

	'' verify the chip type

	SetSymbolValue "RadioDefault", Index$

GetTypeL1:

	sz$ = UIStartDlg(CUIDLL$, VALIDATECHIP, "FRadioDlgProc", 0, "")
	NewIndex% = val(GetSymbolValue("ButtonChecked"))

	IF sz$ = "CONTINUE" THEN
	UIPop 1
	ELSEIF sz$ = "REACTIVATE" THEN
	GOTO GetTypeL1
	ELSEIF sz$ = "BACK" THEN
	UIPop 1
	GOTO GETPATH
	ELSE
	ConfirmQuit
	GOTO GetType
	END IF

	Index$ = str$(NewIndex%)

	select case NewIndex%
		case 1
			IsDIB$ = "0"
			VgaType$ = "tsg"
		case 2
			IsDIB$ = "0"
			VgaType$ = "tri"
		case 3
			IsDIB$ = "0"
			VgaType$ = "v7"
		case 4
			IsDIB$ = "0"
			VgaType$ = "cir"
		case 5
			IsDIB$ = "0"
			VgaType$ = "cpq"
		case else
			IsDIB$ = "1"
			VgaType$ = "dib"
	end select

	'' Determine the speed of the machine. For the moment ignore this and
	'' just use default for rate and simple size selection

	'' default for speed

	Rate$ = "30"

	'' select the size of the player

	if VgaType$ = "dib" then

		Size$ = "160"
		Small$ = "1"
		Index2$ = "2"

	else

		Size$ = "320"
		Small$ = "0"
		Index2$ = "1"

	end if

GetSize:

	SetSymbolValue "RadioDefault", Index2$

GetSizeL1:

	sz$ = UIStartDlg(CUIDLL$, VALIDATESIZE, "FRadioDlgProc", 0, "")
	NewIndex% = val(GetSymbolValue("ButtonChecked"))

	IF sz$ = "CONTINUE" THEN
	UIPop 1
	ELSEIF sz$ = "REACTIVATE" THEN
	GOTO GetTypeL1
	ELSEIF sz$ = "BACK" THEN
	UIPop 1
	GOTO GetType
	ELSE
	ConfirmQuit
	GOTO GetSize
	END IF

	select case NewIndex%
		case 1
			Small$ = "0"
			Size$ = "320"
		case else
			Small$ = "1"
			Size$ = "160"
	end select

	'' Complete the file selection process.

	AddSectionKeyFileToCopyList "Drivers", Size$ + VgaType$, SrcDir$, WinDir$

	'' Copy the files

	CopyFilesInCopyList

	'' Set up the ini file

	CreateIniKeyValue ini$, "Screen Saver.Matinee", "MPEGPath", DEST$, cmoOverwrite
	CreateIniKeyValue ini$, "Screen Saver.Matinee", "ReelPath", MakePath(DEST$, "Reel.lst"), cmoOverwrite
	CreateIniKeyValue ini$, "Screen Saver.Matinee", "Rate", Rate$, cmoOverwrite
	CreateIniKeyValue ini$, "Screen Saver.Matinee", "Small", Small$, cmoOverwrite
	CreateIniKeyValue ini$, "Screen Saver.Matinee", "Bitmapped", IsDIB$, cmoOverwrite
	CreateIniKeyValue ini$, "Screen Saver.Matinee", "Type", Size$ + VgaType$, cmoOverwrite
	CreateIniKeyValue ini$, "Screen Saver.Matinee", "Playtype", "1", cmoOverwrite

   '' Specify the default backdrop as TV. 0=theater;1=TV;2=newspaper
	CreateIniKeyValue ini$, "Screen Saver.Matinee", "Background", "1", cmoOverwrite

	'' Set up the backdrop files
	if Small$ = "0" then
		CreateIniKeyValue ini$, "Screen Saver.Matinee", "Bitmap0", MakePath(DEST$, "backgrnd\closeup.bmp") + ",171,124", cmoOverwrite
		CreateIniKeyValue ini$, "Screen Saver.Matinee", "Bitmap1", MakePath(DEST$, "backgrnd\bigtv.bmp") + ",178,178", cmoOverwrite
		CreateIniKeyValue ini$, "Screen Saver.Matinee", "Bitmap2", MakePath(DEST$, "backgrnd\bigpaper.bmp") + ",147,129", cmoOverwrite
      CreateIniKeyValue ini$, "Screen Saver.Matinee", "BackgroundX", "178", cmoOverwrite
      CreateIniKeyValue ini$, "Screen Saver.Matinee", "BackgroundY", "178", cmoOverwrite
	else
		CreateIniKeyValue ini$, "Screen Saver.Matinee", "Bitmap0", MakePath(DEST$, "backgrnd\screen.bmp") + ",246,158", cmoOverwrite
		CreateIniKeyValue ini$, "Screen Saver.Matinee", "Bitmap1", MakePath(DEST$, "backgrnd\tinytv.bmp") + ",248,203", cmoOverwrite
		CreateIniKeyValue ini$, "Screen Saver.Matinee", "Bitmap2", MakePath(DEST$, "backgrnd\scoop.bmp") + ",224,107", cmoOverwrite
      CreateIniKeyValue ini$, "Screen Saver.Matinee", "BackgroundX", "248", cmoOverwrite
      CreateIniKeyValue ini$, "Screen Saver.Matinee", "BackgroundY", "203", cmoOverwrite
	end if

   '' Add NULL/default values for other INI file keys
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "Tiled", "0", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "BackgroundPath", "", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "History0", "", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "History1", "", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "History2", "", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "History3", "", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "InsertAfter", "26918", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "Random", "1", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "Overlap", "1", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "Interval", "10", cmoOverwrite
   CreateIniKeyValue ini$, "Screen Saver.Matinee", "PWProtected", "0", cmoOverwrite

	'' Add SCR to the executable list

	Programs$ = GetIniKeyString("win.ini", "Windows", "Programs")

	if instr(lcase$(Programs$), "scr") < 1 then
		Programs$ = Programs$ + " scr"
		CreateIniKeyValue "win.ini", "Windows", "Programs", Programs$, cmoOverwrite
	end if

	'' check to see if SCR is an acceptable extention

	SCRExt$ = GetIniKeyString("win.ini", "Extensions", "scr")

	'' If it is, kill it

	if not SCRExt$ = "" then
		RemoveIniKey "win.ini", "Extensions", "scr", cmoNone

		'' warn the user of the change

		sz$ = UIStartDlg(CUIDLL$, INIDELETED, "FInfoDlgProc", 0, "")
	UIPop 1

	end if

	''Create the progman groups and items

	MatineeExe$ = MakePath(WINDIR$, "matinee.scr")
	MatineeSetup$ = MatineeExe$
	MatineeRun$ = MatineeExe$ + " /s"

	CreateProgmanGroup "Matinee", "", cmoNone
	ShowProgmanGroup  "Matinee", 1, cmoNone
	CreateProgmanItem "Matinee", "Matinee Edit", MatineeSetup$, MatineeExe$ + ",0,24,0," + DEST$, cmoOverwrite
	CreateProgmanItem "Matinee", "Matinee Play", MatineeRun$, MatineeExe$ + ",0,80,0," + DEST$, cmoOverwrite

	AddDos5Help "Matinee", "A Windows 3.1 multimidea MPEG screen saver.", cmoNone

	'' Exit with the proper dialog box

QUIT:

	'' choose a dialog depending on exit error condition

	IF ERR = 0 THEN
	dlg% = EXITSUCCESS
	ELSEIF ERR = STFQUIT THEN
	dlg% = EXITQUIT
	ELSE
	dlg% = EXITFAILURE
	END IF

QUITL1:

	'' display a dialog

	sz$ = UIStartDlg(CUIDLL$, dlg%, "FInfo0DlgProc", 0, "")
	IF sz$ = "REACTIVATE" THEN
	GOTO QUITL1
	END IF

	UIPop 1

	END

ERRQUIT:

	' error handler

	select case ERR
		case ERR_BADCPU
			i% = DoMsgBox("This machine does not have a 80386 or above CPU.", "Matinee Setup Error", MB_OK)
		case ERR_BADVGAMODE
			i% = DoMsgBox("This system is currently not capable of using 256 colors. Please install a 256 color device driver if available.", "Matinee Setup Error", MB_OK)
		case ERR_BADPATH
			i% = DoMsgBox("The destination path is invalid. Please re-enter.", "Matinee Setup Error", MB_OK)
			resume next
		case ERR_BADWINDOWSPATH
			i% = DoMsgBox("The Windows directory on this machine is unwritable.", "Matinee Setup Error", MB_OK)
		case STFQUIT
		case STFERR
		case else
			i% = DoMsgBox("Fatal error.", "Matinee Setup Error", MB_OK)
	end select

	goto QUIT

'*************************************************************************
'**
'** Helper routines
'**
'*************************************************************************

'**
'** Purpose:
'**     Asks the user whether or not they really want to exit
'** Arguments:
'**             None
'*************************************************************************

SUB ConfirmQuit STATIC

ConfirmQuitStart:

	sz$ = UIStartDlg(CUIDLL$, ASKQUIT, "FQuitDlgProc", 0, "")

	IF sz$ = "EXIT" THEN
	UIPopAll
	ERROR STFQUIT
	ELSEIF sz$ = "REACTIVATE" THEN
	GOTO ConfirmQuitStart
	ELSE
	UIPop 1
	END IF

END SUB

'**
'** Purpose:
'**     Appends a file name to the end of a directory path,
'**     inserting a backslash character as needed.
'** Arguments:
'**     szDir$  - full directory path (with optional ending "\")
'**     szFile$ - filename to append to directory
'** Returns:
'**     Resulting fully qualified path name.
'*************************************************************************

FUNCTION MakePath(szDir$, szFile$) STATIC AS STRING

    IF szDir$ = "" THEN
	MakePath = szFile$
    ELSEIF szFile$ = "" THEN
	MakePath = szDir$
    ELSEIF MID$(szDir$, LEN(szDir$), 1) = "\" THEN
	MakePath = szDir$ + szFile$
    ELSE
	MakePath = szDir$ + "\" + szFile$
    END IF

END FUNCTION

'' Determine size of the Windows color palette
FUNCTION GetColors() STATIC AS INTEGER

	hdc% = GetDC(0)

	i% = GetDeviceCaps(hdc%, SIZEPALETTE)
	
	dummy% = ReleaseDC(0, hdc%)

	GetColors = i%

END FUNCTION
