DefineVariables
Number [DlgButton]
Number [SpaceRequired]
Number [InstallCAD]
Number [InstallDwg]
Number [InstallSlb]
Number [InstallMacro]
Number [IndividualInstall]
Number [DiskHasRoom]
Number [IniExists]

Text   [szInstallPath]
Text   [szUserName]
Text   [szFirmName]
Text   [szInstallDrive]
Text   [szOldCtl3dVersion]
Text   [szNewCtl3dVersion]
EndDefineVariables

SetCustomControls Off

Do SetupDisplay
Do GetInstallOptions
Do InstallSoftware
if [IndividualInstall] = 1
	Do UpdateIni
	Do AddProgramItems
	Do RegisterOLE
endif
Do ThankYou
SoLong Quietly


;; *********************************************************
;; setup the display
Procedure SetupDisplay
	SetDefaultBitMap Off
	SetPopupAttrTo BlackOnWhite
	SetTopLineTo Drafix CAD Professional Setup
	ShowWindow maximized
EndProcedure


;; *********************************************************
;; get custom install information
Procedure GetInstallOptions
	[Array1][1] := Drafix Windows CAD
	[Array1][2] := InstallPath
	[Array1][3] := C:\CAD
	GetProfileString Using [Array1]
	[szInstallPath] := [Array1][4]

	[Array1][1] := Startup
	[Array1][2] := UserName
	[Array1][3] := ""
	[Array1][4] := DRAFIX.INI
	GetPrivateProfileString Using [Array1]
	[szUserName] := [Array1][5]

	[Array1][1] := Startup
	[Array1][2] := FirmName
	[Array1][3] := ""
	[Array1][4] := DRAFIX.INI
	GetPrivateProfileString Using [Array1]
	[szFirmName] := [Array1][5]

	[InstallCAD] := 1
	[InstallDwg] := 1
	[InstallSlb] := 1
	[InstallMacro] := 1
	[IndividualInstall] := 1

:start
	DialogBox 268 187 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
	Font "Helv" 8
	LText @xy(5,7) 196 27 "Setup will install Drafix CAD Professional in the directory shown below.  If you prefer another location, enter a new drive and directory."
	LText @xy(5,38) 196 8 "Copy to:"
	EditText @xy(5,48) 196 12 Uses [szInstallPath] Border AutoHScroll UpperCase
	RText @xy(5,74) 62 8 "User Name:"
	RText @xy(5,91) 62 8 "Company Name:"
	EditText @xy(69,72) 132 12 Uses [szUserName] Border AutoHScroll
	EditText @xy(69,89) 132 12 Uses [szFirmName] Border AutoHScroll
	GroupBox @xy(5,110) 112 69 "Options"
	CheckBox @xy(11,121) 101 12 "Install Program Files" Uses [InstallCAD]
	CheckBox @xy(11,135) 101 12 "Install Sample Drawings" Uses [InstallDwg]
	CheckBox @xy(11,149) 101 12 "Install Sample Macros" Uses [InstallMacro]
	CheckBox @xy(11,163) 101 12 "Install Sample Symbols" Uses [InstallSlb]
	GroupBox @xy(123,138) 78 41 "Installation Type"
	RadioButtonGroup Uses [IndividualInstall]
	RadioButton @xy(128,150) 68 12 "Individual"
	RadioButton @xy(128,164) 68 12 "Network"
	DefPushButton @xy(212,7) 50 14 "Install" OK
	PushButton @xy(212,24) 50 14 "Exit" CANCEL
	EndDialogBox

	;; TODO: when pressed cnx and yes, just ended; capture msg
	if [DlgButton] <> 1
		if [DlgButton] = 6
			Do SetupFailed
			SoLong Quietly
		else
	  		GoTo start
		endif
	endif

	if [DlgButton] = 1
		;; must have drive to install (ie d:\path)
		;; TODO: better way of valid path
		if .not. ([szInstallPath] Contains :\)
			Beep
:a1
			DialogBox 180 40 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
			Font "Helv" 8
			CText @xy(4,4) 172 10 "You must enter a valid path in the 'Copy to' text box"
			DefPushButton @xy(71,21) 38 14 "OK" OK
			EndDialogBox
			if [DlgButton] <> 1
				GoTo a1
			endif
			Goto start
		endif

		if [IndividualInstall] = 1
			;; must have user name
			if [szUserName] = ""
				Beep
:b1
				DialogBox 180 40 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
				Font "Helv" 8
				CText @xy(4,4) 172 10 "You must enter your name in the 'User Name' text box"
				DefPushButton @xy(71,21) 38 14 "OK" OK
				EndDialogBox
				if [DlgButton] <> 1
					GoTo b1
				endif
				GoTo start
			endif
		endif

		[szInstallDrive] := SubstringOf [szInstallPath] from 1 to 1
		if DriveIsCDROM [szInstallDrive]
			Beep
:p1
			DialogBox 180 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
			Font "Helv" 8
			CText @xy(4,4) 172 20 "You cannot install to a CDROM"
			DefPushButton @xy(71,31) 38 14 "OK" OK
			EndDialogBox
			if [DlgButton] <> 1
				GoTo p1
			endif
			Goto start
		endif

		;; TODO: DirectoryAllowable does not work
		if [IndividualInstall] <> 1
			if DirectoryAllowable [szInstallPath] = 0
:g1
				DialogBox 180 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
				Font "Helv" 8
				CText @xy(4,4) 172 10 "You do not have the network rights to create"
				CText @xy(4,14) 172 10 [szInstallPath]
				DefPushButton @xy(71,31) 38 14 "OK" OK
				EndDialogBox
				if [DlgButton] <> 1
					GoTo g1
				endif
				GoTo start
			endif
		endif

		if DirectoryExists [szInstallPath]
			DialogBox 180 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
			Font "Helv" 8
			CText @xy(4,4) 172 10 "Do you want to overwrite the existing version?"
			DefPushButton @xy(48,30) 38 14 "&Yes" OK
			PushButton @xy(94,30) 38 14 "&No" NO
			EndDialogBox
			if [DlgButton] <> 1
				GoTo start
			endif
		endif

		;; create the paths
		;; TODO: is there another way since this aborts the setup
		CreateDirectoryIfNecessary [szInstallPath]
		CreateDirectoryIfNecessary [szInstallPath]\DWG
		CreateDirectoryIfNecessary [szInstallPath]\SLB
		CreateDirectoryIfNecessary [szInstallPath]\MACRO
		CreateDirectoryIfNecessary [szInstallPath]\BITMAP
	endif

	;; add appropriate program files to the que
	if [InstallCAD] = 1
		QueFiles 
			*.EXE
			*.DLL
			*.PWD
			README.*
			*.FON
			*.HLP
			*.BMP
			*.TBX
			DXFWIN.INI
			CADNEWPL.ENG
			*.SHX
			*.SHP
		EndQueFiles
		[SpaceRequired] := QueSize + 100000
		if FreeSpaceOnDrive [szInstallDrive] < [SpaceRequired]
			UnQueFiles 
				*.EXE
				*.DLL
				*.PWD
				README.*
				*.FON
				*.HLP
				*.BMP
				*.TBX
				DXFWIN.INI
				CADNEWPL.ENG
				*.SHX
				*.SHP
			EndUnQueFiles
			Beep
:e1
			DialogBox 180 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
			Font "Helv" 8
			CText @xy(4,4) 172 20 "The drive you specified does not have enough space to install Drafix CAD Professional."
			DefPushButton @xy(71,31) 38 14 "OK" OK
			EndDialogBox
			if [DlgButton] <> 1
				GoTo e1
			endif
			Goto start
		endif
	endif

	if [IndividualInstall] <> 1
		QueFiles
			CADNET.INF
		EndQueFiles
		[SpaceRequired] := QueSize + 350000
		if FreeSpaceOnDrive [szInstallDrive] < [SpaceRequired]
			UnQueFiles
				CADNET.INF
			EndUnQueFiles
			Beep
:e2
			DialogBox 180 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
			Font "Helv" 8
			CText @xy(4,4) 172 20 "The drive you specified does not have enough space to install Drafix CAD Professional."
			DefPushButton @xy(71,31) 38 14 "OK" OK
			EndDialogBox
			if [DlgButton] <> 1
				GoTo e2
			endif
			Goto start
		endif
		CopyFiles from [InstallFromDirectory] to [szInstallPath]
			SETUP.EXE
		EndCopyFiles
	endif

	;; add appropriate drawing files to the que
	if [InstallDwg] = 1
		QueFiles
			*.CAD
			*.CSV
		EndQueFiles
		[SpaceRequired] := QueSize + 100000
		if FreeSpaceOnDrive [szInstallDrive] < [SpaceRequired]
			UnQueFiles
				*.CAD
				*.CSV
			EndUnQueFiles
			Beep
:e3
			DialogBox 180 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
			Font "Helv" 8
			CText @xy(4,4) 172 20 "The drive you specified does not have enough space to install the sample drawings."
			DefPushButton @xy(71,31) 38 14 "OK" OK
			EndDialogBox
			if [DlgButton] <> 1
				GoTo e3
			endif
			Goto start
		endif
	endif

	;; add appropriate library files to the que
	if [InstallSlb] = 1
		QueFiles
			*.SLB
		EndQueFiles
		[SpaceRequired] := QueSize + 100000
		if FreeSpaceOnDrive [szInstallDrive] < [SpaceRequired]
			UnQueFiles
				*.SLB
			EndUnQueFiles
			Beep
:e4
			DialogBox 180 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
			Font "Helv" 8
			CText @xy(4,4) 172 20 "The drive you specified does not have enough space to install the sample symbol libraries."
			DefPushButton @xy(71,31) 38 14 "OK" OK
			EndDialogBox
			if [DlgButton] <> 1
				GoTo e4
			endif
			Goto start
		endif
	endif

	;; add appropriate macro files to the que
	if [InstallMacro] = 1
		QueFiles
			*.D
		EndQueFiles
		[SpaceRequired] := QueSize + 100000
		if FreeSpaceOnDrive [szInstallDrive] < [SpaceRequired]
			UnQueFiles
				*.D
			EndUnQueFiles
			Beep
:e5
			DialogBox 180 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
			Font "Helv" 8
			CText @xy(4,4) 172 20 "The drive you specified does not have enough space to install the sample macros."
			DefPushButton @xy(71,31) 38 14 "OK" OK
			EndDialogBox
			if [DlgButton] <> 1
				GoTo e5
			endif
			Goto start
		endif
	endif
EndProcedure


;; *********************************************************
;; install the software
Procedure InstallSoftware
	[InstallationDirectory] := [szInstallPath]
	GetQuedFiles

	if [IndividualInstall] = 1
		;; move ctl3d.dll to WindowsSystemDiriectory only if version newer
		[szOldCtl3dVersion] := FileVersionOf [WindowsSystemDirectory]\CTL3D.DLL
		[szNewCtl3dVersion] := FileVersionOf [szInstallPath]\CTL3D.DLL
		if [szNewCtl3dVersion] > [szOldCtl3dVersion] 
			CopyFiles from [szInstallPath] to [WindowsSystemDirectory]
				CTL3D.DLL
			EndCopyFiles
		endif
		DeleteFiles from [szInstallPath]
			CTL3D.DLL
		EndDeleteFiles
	endif
EndProcedure


;; *********************************************************
;; update the ini files
Procedure UpdateIni
	[Array2][1] := Drafix Windows CAD
	[Array2][2] := InstallPath
	[Array2][3] := [szInstallPath]
	WriteProfileString Using [Array2] NoBackup

	[IniExists] := 0
	if FileExists [WindowsDirectory]\DRAFIX.INI
		[IniExists] := 1
	endif

	[Array2][1] := Startup
	[Array2][2] := UserName
	[Array2][3] := [szUserName]
	[Array2][4] := DRAFIX.INI
	WritePrivateProfileString Using [Array2] NoBackup

	if [szFirmName] <> ""
		[Array2][1] := Startup
		[Array2][2] := FirmName
		[Array2][3] := [szFirmName]
		[Array2][4] := DRAFIX.INI
		WritePrivateProfileString Using [Array2] NoBackup
	endif

	[Array2][1] := Path
	[Array2][2] := PathSymbol
	[Array2][3] := [szInstallPath]\SLB
	[Array2][4] := DRAFIX.INI
	WritePrivateProfileString Using [Array2] NoBackup

	[Array2][1] := Path
	[Array2][2] := PathDrawing
	[Array2][3] := [szInstallPath]\DWG
	[Array2][4] := DRAFIX.INI
	WritePrivateProfileString Using [Array2] NoBackup

	[Array2][1] := Path
	[Array2][2] := PathMacro
	[Array2][3] := [szInstallPath]\MACRO
	[Array2][4] := DRAFIX.INI
	WritePrivateProfileString Using [Array2] NoBackup

	[Array2][1] := Path
	[Array2][2] := PathBitmap
	[Array2][3] := [szInstallPath]\BITMAP
	[Array2][4] := DRAFIX.INI
	WritePrivateProfileString Using [Array2] NoBackup

	if [IniExists] = 0
		[Array2][1] := Startup
		[Array2][2] := DefaultSLB
		[Array2][3] := [szInstallPath]\SLB\TUTORIAL.SLB
		[Array2][4] := DRAFIX.INI
		WritePrivateProfileString Using [Array2] NoBackup
	endif

	[Array2][1] := Filter
	[Array2][2] := NativeExtension
	[Array2][3] := CAD
	[Array2][4] := DRAFIX.INI
	WritePrivateProfileString Using [Array2] NoBackup

	if [IniExists] = 0
		if MaxX <= 640
			[Array2][1] := ToolBox0
			[Array2][2] := _Size
			[Array2][3] := 0 2
			[Array2][4] := DRAFIX.INI
			WritePrivateProfileString Using [Array2] NoBackup
		endif

		if MaxX <= 800 .AND. MaxX > 640
			[Array2][1] := ToolBox0
			[Array2][2] := _Size
			[Array2][3] := 1 2
			[Array2][4] := DRAFIX.INI
			WritePrivateProfileString Using [Array2] NoBackup

			[Array2][1] := Preference
			[Array2][2] := BarFont
			[Array2][3] := Arial~0~0~10
			[Array2][4] := DRAFIX.INI
			WritePrivateProfileString Using [Array2] NoBackup
		endif

		if MaxX > 800
			[Array2][1] := ToolBox0
			[Array2][2] := _Size
			[Array2][3] := 2 2
			[Array2][4] := DRAFIX.INI
			WritePrivateProfileString Using [Array2] NoBackup

			[Array2][1] := Preference
			[Array2][2] := BarFont
			[Array2][3] := Arial~0~0~10
			[Array2][4] := DRAFIX.INI
			WritePrivateProfileString Using [Array2] NoBackup
		endif
	endif
EndProcedure


;; *********************************************************
;; inform user that setup is incomplete
Procedure SetupFailed
	Beep
	DialogBox 200 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
	Font "Helv" 8
	CText @xy(4,4) 192 20 "Drafix CAD Professional has not been properly installed.  You must rerun Setup to install the software"
	DefPushButton @xy(81,31) 38 14 "OK" OK
	EndDialogBox

	SoLong Quietly
EndProcedure


;; *********************************************************
;; inform user that setup is complete
Procedure ThankYou
	if [IndividualInstall] = 1
		DialogBox 200 50 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
		Font "Helv" 8
		CText @xy(4,4) 192 20 "Drafix CAD Professional installation is complete.  Remember to send in your registration card!"
		DefPushButton @xy(81,31) 38 14 "OK" OK
		EndDialogBox
	else
		DialogBox 200 70 UseHeader "Drafix CAD Professional" Returns [DlgButton] ModalFrame
		Font "Helv" 8
		CText @xy(4,4) 192 40 "Drafix CAD Professional network installation is complete.  To install Drafix CAD Professional on a network workstation, run SETUP from the Drafix CAD Professional network directory.  Remember to send in your registration card!"
		DefPushButton @xy(81,51) 38 14 "OK" OK
		EndDialogBox
	endif
EndProcedure


;; *********************************************************
;; add program groups
Procedure AddProgramItems
	if [InstallCAD] = 1
		ProgramManagerDDE 
			CreateGroup (Drafix, [WindowsDirectory]\cad.grp)
			DeleteItem (CAD)
			DeleteItem (CAD Read Me)
			AddItem ([szInstallPath]\cad.exe, CAD, [szInstallPath]\cad.exe ,0)
			AddItem (notepad.exe [szInstallPath]\readme.cad, CAD Read Me, notepad.exe ,1)
		EndProgramManagerDDE
	endif
EndProcedure


;; *********************************************************
;; prevent the stupid nonfatal dde dlg box from appearing
Procedure DDENonFatalErrorTrap
EndProcedure


;; *********************************************************
;; add to registration database
Procedure RegisterOLE
	if [InstallCAD] = 1
		[Array2][1] := Embedding
		[Array2][2] := DrafixCAD
		[Array2][3] := Drafix CAD,Drafix CAD,[szInstallPath]\cad.exe,picture
		WriteProfileString Using [Array2] NoBackup
	endif
EndProcedure

EndScript
