'**************************************************************************
'**************************************************************************
DECLARE SUB InitialIzation
DECLARE SUB BadOpSysNT
DECLARE SUB BadOpSys30
DECLARE SUB WAutoFig
DECLARE SUB Notadmini
DECLARE SUB Disincentive
DECLARE SUB FindOldSoftWare
DECLARE SUB ASKEXIT
DECLARE SUB GetPath
DECLARE SUB QuitInstall
DECLARE SUB FillCopyList
DECLARE SUB FillIniFile
DECLARE FUNCTION WriteDisToFile LIB "mscuistf.dll" (Direct$) AS INTEGER
DECLARE FUNCTION ReadIniDisin   LIB "mscuistf.dll" (Direct$) AS INTEGER
DECLARE FUNCTION MakePath (szDir$, szFile$) AS STRING
DECLARE FUNCTION IsWinNT LIB "mscuistf.dll" AS INTEGER
DECLARE FUNCTION FindWindow LIB "USER" (classname$, title$) AS INTEGER
DECLARE FUNCTION GetBasicStr LIB "string.dll" (x%) AS INTEGER
DECLARE FUNCTION SetRunLine LIB "mscuistf.dll" (lpNewLine$) AS INTEGER
DECLARE FUNCTION RegisterWindowMessage LIB "USER" (sz$) AS INTEGER

DECLARE FUNCTION NTInit LIB "MSCOMSTF" AS INTEGER
DECLARE FUNCTION NTIsDirWritable LIB "MSCOMSTF" (DirName$) AS INTEGER
DECLARE FUNCTION NTCreateDir LIB "MSCOMSTF" (DirName$) AS INTEGER
DECLARE FUNCTION NTGetWinDir LIB "MSCOMSTF" () AS INTEGER
DECLARE FUNCTION NTGetSysDir LIB "MSCOMSTF" () AS INTEGER
DECLARE FUNCTION NTCheckSecurity LIB "MSCOMSTF" AS INTEGER
DECLARE FUNCTION NTSetRegistry LIB "MSCOMSTF" (RegInfo$, lang%) AS INTEGER
DECLARE FUNCTION NTReboot LIB "MSCOMSTF" (Reboot%) AS INTEGER
DECLARE FUNCTION NTFindDirs LIB "MSCOMSTF" (t1$, t2$, szDevice$) AS INTEGER

CONST WM_REBOOT    = 1536
CONST WM_WTYPE     = 1537
CONST WM_LANG      = 1538
CONST WM_NONWINKBD = 1539
CONST WM_KEYBOARD  = 1540

CONST DIS_NOT_INSTALLED = 101
CONST DIS_INSTALLED = 202
CONST DIS_CORUPTED = 303

'**************************************************************************
'**************************************************************************
SUB InitialIzation STATIC

	SetBitmap "string.dll", 1

	x% = GetBasicStr(8002)
	sz$ = GetSymbolValue("SetupString")
	SetTitle sz$
	RemoveSymbol "SetupString"

	'' Get windows type
	WINTYPE% = IsWinNT

	IF WINTYPE% = -1 THEN
		BadOpSysNT
	ELSEIF WINTYPE% = 0 THEN
		WINTYPE% = GetWindowsMajorVersion
		IF WINTYPE% = 3 THEN
			x% = GetWindowsMinorVersion
			IF x% = 0 THEN
				BadOpSys30
			ENDIF
			IF x% > 60 THEN
				WINTYPE% = 4
			ENDIF
		ENDIF
	ENDIF

	'Find out if Autoexec.bat and Config.sys are writeable
	i% = AutoFigWriteable()
	if i% = 0 then
		'Autoexec/Config.sys are read-only
		WAutoFig
	ENDIF

	'' Read setup.inf file
	szSTR$ = GetSymbolValue("STF_SRCINFPATH")
	IF szSTR$ = "" THEN
		szSTR$ = GetSymbolValue("STF_CWDDIR")
	END IF
	SETUPINF$ = szSTR$

	ReadInfFile MakePath(szSTR$, "setup.inf")

	'' Find the Mother ship
	HWIN% = FindWindow("STUFF-BOOT", NULL)
	
	'' Get source dir
	SRCDIR$ = GetSymbolValue("STF_SRCDIR")

	'' Disincentive
	RetCode% = ReadIniDisin(MakePath(SRCDIR$, "setup.ini"))

	''Make Our Default drive and path string
	IF WINTYPE% = 3 THEN
		WINDIR$ = GetWindowsDir
		WINSYSDIR$ = GetWindowsSysDir
	ELSE
		'32-Bit Windows
		x% = NTInit()
		if WINTYPE% = 1 then
			'Basic does both tests on an AND... duh!
			if NTCheckSecurity() = 0 then
				Notadmini
			endif
		endif
		'Windows and windows/System32 may use 
		'long filenames (get 8.3)
		x%= NTGetWinDir()
		WINDIR$ = GetSymbolValue("SetupString")
		RemoveSymbol "SetupString"
		x%= NTGetSysDir()
		WINSYSDIR$ = GetSymbolValue("SetupString")
		RemoveSymbol "SetupString"
	ENDIF
	
	'Default MSINPUT directory
	SHMSE$ = MID$(WINDIR$, 1, 1) + ":\MSINPUT"

	'' Set /O switch if on comand line
	SETUPTYPE$ = GetIniKeyString(MakePath(SRCDIR$, "setup.lst"), "SetupType", "OEM")

	IF SETUPTYPE$ = "TRUE" THEN
		OEMTYPE% = 1
	ELSE
		OEMTYPE% = 0
	ENDIF
END SUB
'**************************************************************************

'**************************************************************************
'**************************************************************************
SUB BadOpSysNT STATIC

BNT:
	sz$ = UIStartDlg(COMDLL$, BADOSNT, "FQuitDlgProc", 0, "")
	IF sz$ = "HELP" THEN
		sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
		UIPop 1
		GOTO BNT
	ENDIF
	UIPopAll
	ERROR STFQUIT

END SUB
'**************************************************************************

'**************************************************************************
'**************************************************************************
SUB BadOpSys30 STATIC

B30:
	sz$ = UIStartDlg(COMDLL$, BADOS30, "FQuitDlgProc", 0, "")
	IF sz$ = "HELP" THEN
		sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
		UIPop 1
		GOTO B30
	ENDIF
	UIPopAll
	ERROR STFQUIT

END SUB
'**************************************************************************

'**************************************************************************
'**************************************************************************
SUB WAutoFig STATIC

WAF:
	sz$ = UIStartDlg(COMDLL$, WRITEAUTOFIG, "FQuitDlgProc", 0, "")
	IF sz$ = "HELP" THEN
		sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
		UIPop 1
		GOTO WAF
	ENDIF
	UIPopAll
	ERROR STFQUIT

END SUB
'**************************************************************************

'**************************************************************************
'**************************************************************************
SUB Notadmini STATIC

NA:
	sz$ = UIStartDlg(COMDLL$, NOTADMIN, "FQuitDlgProc", 0, "")
	IF sz$ = "HELP" THEN
		sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
		UIPop 1
		GOTO NA
	ENDIF
	UIPopAll
	ERROR STFQUIT

END SUB
'**************************************************************************

'**************************************************************************
'**************************************************************************
SUB Disincentive STATIC

	IF RetCode% = DIS_NOT_INSTALLED THEN
REDOO:
		sz$ = UIStartDlg(COMDLL$, CDGETNAME, "FNameOrgDlgProc", 0, "")
		szName$ = GetSymbolValue("NameOut")

		IF sz$ = "CONTINUE" THEN
			IF szName$ = "" or szName$ = " " THEN
BN:
				sz$ = UIStartDlg(COMDLL$, CDBADNAME, "FConfirmDlgProc", 0, "")
				IF sz$ = "CONTINUE" THEN
					UIPop 1
					GOTO REDOO
				ELSEIF sz$ = "CANCEL" THEN
					UIPop 1
					GOTO REDOO
				ELSEIF sz$ = "HELP" THEN
					sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
					UIPop 1
					GOTO BN
				ELSE
					UIPop 1
					GOTO REDOO
				ENDIF
			ENDIF			
			AddListItem "ConfirmTextIn", szName$

CDCONFIRM:
			sz$ = UIStartDlg(COMDLL$, CDCONFIRMINFO, "FConfirmDlgProc", 0, "")

			RemoveSymbol "ConfirmTextIn"								
			
			IF sz$ = "BACK" OR sz$ = "CANCEL" THEN
				UIPop(1)
				GOTO REDOO
			ELSEIF sz$ = "HELP" THEN
				sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
				UIPop 1
				GOTO CDCONFIRM
			ELSE
				UIPop(2)
			ENDIF

		ELSEIF sz$ = "EXIT" OR sz$ = "CANCEL" THEN
			ASKEXIT
			GOTO REDOO
		ELSEIF sz$ = "HELP" THEN
			sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
			UIPop 1
			GOTO REDOO
		ELSE
			GOTO REDOO
		ENDIF

		SetSymbolValue "INSTALLEDNAME", szName$ 
		SetSymbolValue "INSTALLEDORG", ""
		sx% = WriteDisToFile(MakePath(SRCDIR$, "setup.ini"))
		
	ELSEIF RetCode% = DIS_INSTALLED THEN

DISINSTALLED:
		szName$ = GetSymbolValue("INSTALLEDNAME")
		szSer$ = GetSymbolValue("INSTALLEDSER")

		AddListItem "ConfirmTextIn", szName$
		AddListItem "ConfirmTextIn", szSer$

		sz$ = UIStartDlg(COMDLL$, CDALREADYUSED, "FConfirmDlgProc", 0, "")
		RemoveSymbol "ConfirmTextIn"								

		IF sz$ = "EXIT" OR sz$ = "CANCEL" THEN
			ASKEXIT
			GOTO DISINSTALLED
		ELSEIF sz$ = "HELP" THEN
			sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
			UIPop 1
			GOTO DISINSTALLED
		ELSE
			UIPop(1)
		ENDIF

	ELSEIF RetCode% = DIS_CORUPTED THEN

DISCORUPTED:
		sz$ = UIStartDlg(COMDLL$, CDBADFILE, "FInfoDlgProc", 0, "")

		IF sz$ = "EXIT" OR sz$ = "CANCEL" THEN
			ASKEXIT
			GOTO DISCORUPTED
		ELSEIF sz$ = "HELP" THEN
			sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
			UIPop 1
			GOTO DISCORUPTED
		ELSE
			UIPop(1)
		ENDIF

	ENDIF

END SUB
'**************************************************************************

'**************************************************************************
'**************************************************************************
SUB FindOldSoftWare STATIC

	DLG% = DESTPATH
	Cursor% = ShowWaitCursor()
		
	FOUNDOTHER% = 0
	sz$ = GetEnvVariableValue("MSINPUT")
	IF sz$="" AND WINTYPE% <> 3 THEN
		'Win95 and WinNT
		i% = NTFindDirs("", "", "JOYSTICK\SIDEWNDR")
		IF i% <> 0 THEN
			sz$ = GetSymbolValue("SetupString")
		ENDIF
	ENDIF
		
	IF sz$ <> "" THEN
		FOUNDOTHER% = 1
		SHMSE$ = sz$
		IF FindFileUsingFileOpen(MakePath(sz$,"JOYSTICK\SIDEWNDR\UNINSTAL.EXE")) <> "" THEN
			DLG% = FOUNDSOFT
		ENDIF
	ENDIF

	RestoreCursor Cursor

END SUB
'**************************************************************************

'**************************************************************************
'**************************************************************************
SUB GetPath STATIC

FOUND:
	IF FOUNDOTHER% = 1 THEN
		AddListItem "ConfirmTextIn", SHMSE$
		'' Do you realy want to change the directory?
DET:
		sz$ = UIStartDlg(COMDLL$, DETECT, "FConfirmDlgProc", 0, "")
		IF sz$ = "CONTINUE" THEN
			UIPop 1
			GOTO XSTALL
		ELSEIF sz$ = "CANCEL" THEN
			ASKEXIT
			GOTO DET
		ELSEIF sz$ = "HELP" THEN
			sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
			UIPop 1
			GOTO DET
		ENDIF
		UIPop 1

		AddListItem "ConfirmTextIn", SHMSE$
		'' Do you REALY REALLY want to change the directory?
WARN:
		sz$ = UIStartDlg(COMDLL$, WARNING, "FConfirmDlgProc", 0, "")
		IF sz$ = "CONTINUE" THEN
			UIPop 1
			GOTO XSTALL
		ELSEIF sz$ = "CANCEL" THEN
			ASKEXIT
			GOTO WARN
		ELSEIF sz$ = "HELP" THEN
			sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
			UIPop 1
			GOTO WARN
		ENDIF
		UIPop 1
	ENDIF

	SetSymbolValue "EditTextIn", SHMSE$
	SetSymbolValue "EditFocus", "ALL"
GETPATHL1:
	sz$ = UIStartDlg(COMDLL$, DLG%, "FEditDlgProc", 0, "")
	IF sz$ = "CONTINUE" THEN
		SHMSE$ = GetSymbolValue("EditTextOut")
		if SHMSE$ = "" then
			goto BADPATH
		endif
RMBACK:
		IF MID$(SHMSE$, LEN(SHMSE$), 1) = "\" THEN
			IF LEN(SHMSE$) > 3 THEN
				SHMSE$ = MID$(SHMSE$, 1, LEN(SHMSE$)-1)
				GOTO RMBACK
			ENDIF
		ENDIF

		IF IsDirWritable(SHMSE$) = 0 THEN
BADPATH:
			sz$ = UIStartDlg(COMDLL$, BADPATH, "FInfo0DlgProc", 0, "")
			IF sz$ = "REACTIVATE" THEN
				GOTO BADPATH
			ELSEIF sz$ = "HELP" THEN
				sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
				UIPop 1
				GOTO BADPATH
			END IF
			UIPop 1

			GOTO GETPATHL1

		END IF
		UIPop 1

		'' Is there room on the drive?
		IF WINTYPE% = 1 OR WINTYPE% = 4 THEN
			   ''   INPUT	USER
			need& = 347957 + 422923
		ELSEIF WINTYPE% = 3 THEN
			   ''   INPUT	USER
			need& = 271697 + 345995
		ENDIF
		drive$ = MID$(SHMSE$, 1, 1)

		IF need& > GetFreeSpaceForDrive(drive$) THEN
NOSPACE:
			sz$ = UIStartDlg(COMDLL$, NOSPACEEXPRESS, "FInfo0DlgProc", 0, "")
			IF sz$ = "CONTINUE" THEN
				UIPop 1
				GOTO GETPATHL1
			ELSEIF sz$ = "BACK" OR sz$ = "CANCEL" THEN
				UIPop 1
				ASKEXIT
				GOTO GETPATHL1
			ELSEIF sz$ = "REACTIVATE" THEN
				GOTO NOSPACE
			ELSEIF sz$ = "HELP" THEN
				sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
				UIPop 1
				GOTO NOSPACE
			ELSE
				GOTO GETPATHL1
			END IF
		END IF
	ELSEIF sz$ = "EXIT" OR sz$ = "CANCEL" THEN
		ASKEXIT
		GOTO GETPATHL1
	ELSEIF sz$ = "HELP" THEN
		sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
		UIPop 1
		GOTO GETPATHL1
	ELSE
		GOTO GETPATHL1
	END IF

XSTALL:

END SUB
'**************************************************************************

'*************************************************************************
'*************************************************************************
SUB QuitInstall STATIC

	IF ERR = 0 THEN
ES:
		sz$ = UIStartDlg(COMDLL$, EXITSUCCESS, "FInfoDlgProc", 0, "")
		IF sz$ = "EXIT" OR sz$ = "CANCEL" THEN
			UIPop 1
			x% = PostMessage(HWIN%, 1536, 0, 0)
		ELSEIF sz$ = "BACK" THEN
			UIPop 1
			x% = PostMessage(HWIN%, 1536, 1, 0)
		ELSEIF sz$ = "HELP" THEN
			sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
			UIPop 1
			GOTO ES
		ELSE
			GOTO ES
		END IF
		END

	ELSEIF ERR = STFQUIT THEN
		dlg% = EXITQUIT
	ELSE
		dlg% = EXITFAILURE
	END IF
QUITL1:
	sz$ = UIStartDlg(COMDLL$, dlg%, "FInfo0DlgProc", 0, "")
	IF sz$ = "REACTIVATE" THEN
		GOTO QUITL1
	ELSEIF sz$ = "HELP" THEN
		sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
		UIPop 1
		GOTO QUITL1
	ENDIF
	UIPop 1
	x% = PostMessage(HWIN%, 1536, 0, 0)
	
	END
 
	
END SUB
'*************************************************************************

'*************************************************************************
'*************************************************************************
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
'*************************************************************************

'*************************************************************************
'*************************************************************************
SUB ASKEXIT STATIC
ASKQUIT:
	sz$ = UIStartDlg(COMDLL$, ASKQUIT, "FQuitDlgProc", 0, "")
	IF sz$ = "EXIT" THEN
		UIPopAll
		ERROR STFQUIT
	ELSEIF sz$ = "REACTIVATE" THEN
		GOTO ASKQUIT
	ELSEIF sz$ = "HELP" THEN
		sz$ = UIStartDlg(COMDLL$, NOHELP, "FHelpDlgProc", 0, "")
		UIPop 1
		GOTO ASKQUIT
	ELSE
		UIPop 1
	END IF

END SUB
'*************************************************************************

'*************************************************************************
'*************************************************************************
SUB FillCopyList STATIC

	ClearCopyList

	'[Setup] section keeps track of installations
	szIni$ = MakePath(SHMSE$,"MSINPUT.INI")
	
	LangType = SendMessage(HWIN%, WM_LANG, 0, 0)
	
	IF WINTYPE% = 3 THEN
		CreateIniKeyValue szIni$, "Setup", "Joystick.Win16", SHDST$, cmoOverwrite
	ELSE
		'Win95/NT/Daytona
		IF WINTYPE% = 4 THEN
			'Win95
			CreateIniKeyValue szIni$, "Setup", "Joystick.Win95", SHDST$, cmoOverwrite
			AddSectionKeyFileToCopyList "Files", "SIDEWNDR.VXD", SRCDIR$, WINSYSDIR$
		ELSE
			'NT/Daytona
			CreateIniKeyValue szIni$, "Setup", "Joystick.WinNT", SHDST$, cmoOverwrite
		ENDIF
	ENDIF
		

	IF LangType = 66 THEN
		CreateIniKeyValue szIni$, "Setup", "Joystick.Lng", "German", cmoOverwrite
		AddSectionKeyFileToCopyList "Files", "SIDEWNDR.GER", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "PSSTOC.GER", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "INFO.WRI", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "INFO.TXT", SRCDIR$, SHDST$
	ELSEIF LangType = 67 THEN
		CreateIniKeyValue szIni$, "Setup", "Joystick.Lng", "French", cmoOverwrite
		AddSectionKeyFileToCopyList "Files", "SIDEWNDR.FRN", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "PSSTOC.FRN", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "LISEZMOI.WRI", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "LISEZMOI.TXT", SRCDIR$, SHDST$
	ELSEIF LangType = 68 THEN
		CreateIniKeyValue szIni$, "Setup", "Joystick.Lng", "Spanish", cmoOverwrite
		AddSectionKeyFileToCopyList "Files", "SIDEWNDR.SPA", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "PSSTOC.SPA", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "LEAME.WRI", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "LEAME.TXT", SRCDIR$, SHDST$
	ELSEIF LangType = 69 THEN
		CreateIniKeyValue szIni$, "Setup", "Joystick.Lng", "Italian", cmoOverwrite
		AddSectionKeyFileToCopyList "Files", "SIDEWNDR.ITA", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "PSSTOC.ITA", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "LEGGIMI.WRI", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "LEGGIMI.TXT", SRCDIR$, SHDST$
	ELSE
		'Default to English
		CreateIniKeyValue szIni$, "Setup", "Joystick.Lng", "English", cmoOverwrite
		AddSectionKeyFileToCopyList "Files", "SIDEWNDR.ENG", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "PSSTOC.ENG", SRCDIR$, SHDST$
		AddSectionKeyFileToCopyList "Files", "README.TXT", SRCDIR$, SHDST$
	ENDIF

	'Common files
	AddSectionKeyFileToCopyList "Files", "PSS.ENG", SRCDIR$, SHDST$
	AddSectionKeyFileToCopyList "Files", "SETUP.INI", SRCDIR$, SHDST$

	'This is for uninstall
	AddSectionKeyFileToCopyList "Files", "UNINSTAL", SRCDIR$, SHDST$
	AddSectionKeyFileToCopyList "Files", "NTFILE", SRCDIR$, SHDST$

END SUB
'*************************************************************************

'*************************************************************************
'*************************************************************************
SUB FillIniFile STATIC
	ini$ = MakePath(SHMSE$,"MSINPUT.INI")
	CreateIniKeyValue ini$, "Directories", "Joystick", SHDST$, cmoOverwrite
	CreateIniKeyValue ini$, "Joystick", "Interface", "Virtual_Input", cmoOverwrite
	CreateIniKeyValue ini$, "Joystick", "Device", "Joystick", cmoOverwrite
	CreateIniKeyValue ini$, "Joystick", "SideWinderGPE", "ON", cmoOverwrite
END SUB
'*************************************************************************

