'**************************************************************************
'*                       viewSpace 1.0 Setup
'**************************************************************************

'***'$DEFINE DEBUG  ''Define for script development/debugging

'$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 APPHELP       = 900
CONST BADPATH       = 6400
CONST SMALLWIN      = 2200
CONST RESTART       = 2600
CONST RESTARTII     = 2700

''Bitmap ID
CONST LOGO = 1

GLOBAL SizeReq&  '' Total Disk Size required for installation

''File Types
GLOBAL WinDir$
GLOBAL WinSysDir$
GLOBAL WINDRIVE$    ''Windows Drive Letter.
GLOBAL DEST$        ''Default destination directory.
GLOBAL HasMath%     ''has math coprocessor
GLOBAL HasEMU%      ''math emulator already installed    

DECLARE SUB Install
DECLARE SUB EMUInstall
DECLARE FUNCTION MakePath (szDir$, szFile$) AS STRING
DECLARE SUB Reboot LIB "iniupd.dll"
DECLARE fUNCTION ExitWindowsExec  LIB "User" (Exec$, Param$) AS INTEGER


INIT:
    CUIDLL$ = "mscuistf.dll"            ''Custom user interface dll
    HELPPROC$ = "FHelpDlgProc"          ''Help dialog procedure

    SetBitmap CUIDLL$, LOGO
    SetTitle "Caligari viewSpace 1.0"

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

    WINDRIVE$ = MID$(GetWindowsDir, 1, 1)
    DEST$ = WINDRIVE$ + ":\viewspac"

WELCOME:
    sz$ = UIStartDlg(CUIDLL$, WELCOME, "FInfoDlgProc", APPHELP, HELPPROC$)
    IF sz$ = "CONTINUE" THEN
        UIPop 1
    ELSE
        GOSUB ASKQUIT
        GOTO WELCOME
    END IF


GETPATH:
    SetSymbolValue "EditTextIn", DEST$
    SetSymbolValue "EditFocus", "END"
GETPATHL1:
    sz$ = UIStartDlg(CUIDLL$, DESTPATH, "FEditDlgProc", APPHELP, HELPPROC$)
    DEST$ = GetSymbolValue("EditTextOut")

    IF sz$ = "CONTINUE" THEN
        IF IsDirWritable(DEST$) = 0 THEN
            GOSUB BADPATH
            GOTO GETPATHL1
        END IF
        UIPop 1
    ELSEIF sz$ = "REACTIVATE" THEN
        GOTO GETPATHL1
    ELSEIF sz$ = "BACK" THEN
        UIPop 1
        GOTO WELCOME
    ELSE
        GOSUB ASKQUIT
        GOTO GETPATH
    END IF

    SrcDir$ = GetSymbolValue("STF_SRCDIR")
    WinDir$ = GetWindowsDir()
    WinSysDir$ = GetWindowsSysDir()
    HasMath% = Has87MathChip()

    AddSectionFilesToCopyList "Files", SrcDir$, DEST$
    IF HasMath% < 1 THEN
        AddSectionFilesToCopyList "Emulator", SrcDir$, WinSysDir$
    END IF

''  Check windrive diskspace
    SizeReq& = GetCopyListCost ("","", "")
    IF SizeReq& <> 0 THEN
	GOSUB SMALLWIN
	END
    END IF


    Install

'' If no emulator installed then no need to restart windows
   IF HasMath%  > 0 THEN
        GOTO QUIT
   ENDIF
'' Restart Windows: if it has to updates ACM from DOS, it restarts Windows automatically
'' else, it gives the user the choice
    RESTRT% = RestartListEmpty ()
    Exe$ = DEST$ + "\_msrstrt.exe"
    Batch$ = DEST$ + "\_mssetup.bat"
    empty$ = ""
RESTART:
    IF RESTRT% = 0 THEN
       sz$ = UIStartDlg(cuidll$, RESTART, "FInfo0DlgProc", 0, "")
       IF sz$ = "REACTIVATE" THEN
	  GOTO RESTART
       ENDIF
       I% = ExitExecRestart ()
       RemoveFile Exe$, cmoForce
       RemoveFile Batch$, cmoForce
       END
    ELSE
       sz$ = UIStartDlg(CUIDLL$, RESTARTII, "FQuitDlgProc", 0, "")
       IF sz$ = "CONTINUE" THEN
	  I% = ExitWindowsExec (Exe$, empty$)
	  IF I% = 0 THEN
	     GOTO RESTART
	  ELSE
	     END
	 ENDIF
       ELSEIF sz$ = "EXIT" THEN
	  UIPopAll
	  END
       ELSEIF sz$ = "REACTIVATE" THEN
	  GOTO RESTART
       ELSE
	  UIPop 1
       END IF
    END IF


QUIT:
    ON ERROR GOTO ERRQUIT

    IF ERR = 0 THEN
	dlg% = EXITSUCCESS
    ELSEIF ERR = STFQUIT THEN
	dlg% = EXITQUIT
    ELSE
	dlg% = EXITFAILURE
    END IF
QUITL1:
    sz$ = UIStartDlg(CUIDLL$, dlg%, "FInfo0DlgProc", 0, "")
    IF sz$ = "REACTIVATE" THEN
	GOTO QUITL1
    END IF
    UIPop 1
    END

ERRQUIT:
    i% = DoMsgBox("An installation problem occured, call the product support service", "Setup Problem", MB_OK+MB_TASKMODAL+MB_ICONHAND)
    END

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

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

SMALLWIN:
    sz$ = UIStartDlg(CUIDLL$, SMALLWIN, "FInfo0DlgProc", 0, "")
    IF sz$ = "REACTIVATE" THEN
	GOTO SMALLWIN
    END IF
    UIPop 1
    RETURN


BADPATH:
    sz$ = UIStartDlg(CUIDLL$, BADPATH, "FInfo0DlgProc", 0, "")
    IF sz$ = "REACTIVATE" THEN
        GOTO BADPATH
    END IF
    UIPop 1
    RETURN


'**
'** Purpose:
'**     performs all installation operations.
'** Arguments:
'**     none.
'** Returns:
'**     none.
'*************************************************************************
SUB Install STATIC

    CreateDir DEST$, cmoNone

    CopyFilesInCopyList

    IF HasMath% < 1 THEN
        EMUInstall
    END IF

    CreateIniKeyValue "WIN.INI", "Extensions", "tsd", DEST$ + "\VIEWSPAC.EXE ^.tsd", cmoOverwrite
    CreateProgmanGroup "viewSpace", "", cmoNone
    ShowProgmanGroup  "viewSpace", 1, cmoNone
    CreateProgmanItem "viewSpace", "viewSpace", MakePath(DEST$,"viewspac.exe"), "", cmoOverwrite
    CreateProgmanItem "viewSpace", "Self-running Demo 1024x768", MakePath(DEST$,"dems1024.tsd"), "", cmoOverwrite
    CreateProgmanItem "viewSpace", "Self-running Demo 800x600", MakePath(DEST$,"dems800.tsd"), "", cmoOverwrite

END SUB

'**
'** Purpose:
'**     install math emulator in system.ini file
'** Arguments:
'**     none.
'** Returns:
'**     none.
'*************************************************************************
SUB EMUInstall STATIC

    HasEMU% = DoesIniKeyExist("system.ini", "386Enh", "wemu")
    IF HasEMU% < 1 THEN
        CreateIniKeyValue WinDir$ + "system.ini", "386Enh", "wemu", "installed", cmoOverwrite
        CreateSysIniKeyValue WinDir$ + "system.ini", "386Enh", "device", "wemu387.386", cmoOverwrite
    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
