'**************************************************************************
'*                        Mathcad 4.0 Handbook Installation Script
'**************************************************************************
'' $DEFINE DEBUG  ''Define for script development/debugging

'$INCLUDE 'setupapi.inc'
'$INCLUDE 'msdetect.inc'
'$INCLUDE 'msregdb.inc'
'$INCLUDE 'Strings.H'

''Dialog ID's
CONST WELCOME       = 100
CONST ASKQUIT       = 200
CONST DESTPATH      = 300
CONST EXITFAILURE   = 400
CONST EXITQUIT      = 600
CONST EXITSUCCESS   = 700
CONST HELPWELCOME   = 900
CONST EXITFAILNOTWIN31  = 1200
CONST EXITFAILNOTENH = 1300
CONST HELPDEST      = 1400
CONST WELCOME1      = 1700
CONST EXITSUCCESSREBOOT = 1800
CONST EXITFAILNOTDOS33  = 1900
CONST WIN32SLIBS    = 2100
CONST EXITFAILNOTINTEL  = 2200
CONST DESTTOOBIG  = 2400
CONST VERSION   = 2600
CONST TOOBIG        = 6300
CONST BADPATH       = 6400

GLOBAL DESTSYS$      ''WinMcad directory.
GLOBAL DEST32S$      ''Name of Windows Directory
GLOBAL DESTEFI$      ''Name of EFI Directory
GLOBAL InstallDir$   ''Name of Installation Directory
GLOBAL CUIDLL$
GLOBAL RestarWin% '' Decide whether to restart windows or not
GLOBAL WIN32ENABLED%
GLOBAL Win32Ver$
GLOBAL INFOTEXT$

'$IFDEF PRODUCT
    '$INCLUDE 'Notes.inc'
    DECLARE SUB UpdateWinIni
    DECLARE SUB UpdateMcadIni
    DECLARE SUB UpdateWin32sIni
'$ENDIF

DECLARE SUB UpdateSystemIni
DECLARE SUB RegisterMcad
DECLARE SUB RebootSystem
DECLARE FUNCTION MakeSystemIni LIB "INIUPD.DLL" (arg1$, arg2$) AS INTEGER
DECLARE FUNCTION ExitWindows LIB "USER.EXE" (arg1%, arg2%) AS INTEGER
DECLARE FUNCTION RestartWindows LIB "INIUPD.DLL" (arg1$) as INTEGER
DECLARE FUNCTION Unarj Lib "Unarjdll.dll" (ARJPATH$, ARJNAME$, DESTPATH$, TITLEBOX$, HANDLE%) as INTEGER
DECLARE FUNCTION Dearj (destdir$, filename$, titlebox$, sourcedir$) AS INTEGER
DECLARE FUNCTION MakePath (szDir$, szFile$) AS STRING
DECLARE FUNCTION UpaLevel (szDir$, szFile$) AS STRING
DECLARE FUNCTION OnWindowsNT LIB "INIUPD.DLL" AS INTEGER
DECLARE FUNCTION ShowWindow LIB "USER.EXE" (Handle%, WindowState%)  AS INTEGER
DECLARE FUNCTION InitUnarj LIB "Unarjdll.dll" () AS INTEGER
DECLARE FUNCTION FinalUnarj LIB "Unarjdll.dll" () AS INTEGER

INIT:

    o% = ShowWindow(HwndFrame(), 3)
    CUIDLL$ = "mscuistf.dll"            ''Custom user interface dll
    HELPPROC$ = "FHelpCustDlgProc"          ''Help dialog procedure
    RestarWin% = 0
    WIN32ENABLED% = 0

    INFOTEXT$ = "DriveStatusText"
    FOR i% = 1 TO 15 STEP 1
        AddlistItem INFOTEXT$, ""
    NEXT i%

    DESTSYS$ = GetRegKeyValue("Mathcad\shell\open\command")
    IF DESTSYS$ = "" THEN
        MCADVERSION$ = "3.1"
    ELSE
        MCADVERSION$ = "4.0"
    ENDIF

    DEST32S$ = GetWindowsSysDir()
    InstallDir$ = GetSymbolValue("STF_SRCINFPATH")
    IF InstallDir$ = "" THEN
        InstallDir$ = GetSymbolValue("STF_CWDDIR")
    ENDIF
    InstallDir$ = MakePath(InstallDir$, "")

    SetTitle Title$

    szInf$ = GetSymbolValue("STF_SRCINFPATH")
    IF szInf$ = "" THEN
        szInf$ = GetSymbolValue("STF_CWDDIR") + "mcad.inf"
    ENDIF
    ReadInfFile szInf$

CHECK:
    IF GetWindowsMajorVersion < 3 THEN
        ReplaceListItem INFOTEXT$, 1, EXITFAILNOTWIN311$
        ExitCode% = EXITFAILNOTWIN31
        GOTO QUIT
    ENDIF
    IF GetWindowsMajorVersion = 3 AND GetWindowsMinorVersion < 1 THEN
        ReplaceListItem INFOTEXT$, 1, EXITFAILNOTWIN311$
        ExitCode% = EXITFAILNOTWIN31
        GOTO QUIT
    ENDIF
    IF GetDOSMajorVersion < 3 THEN
        ReplaceListItem INFOTEXT$, 1, EXITFAILNOTDOS331$
        ExitCode% = EXITFAILNOTDOS33
    ENDIF
    IF GetDOSMinorVersion < 3 THEN
        ReplaceListItem INFOTEXT$, 1, EXITFAILNOTDOS331$
        ExitCode% = EXITFAILNOTDOS33
    ENDIF
    IF GetWindowsMode < 2  THEN     '' Not Enhanced or Windows NT
        IF OnWindowsNT() THEN
            ReplaceListItem INFOTEXT$, 1, EXITFAILNOTINTEL1$
            ReplaceListItem INFOTEXT$, 2, EXITFAILNOTINTEL2$
            ExitCode% = EXITFAILNOTINTEL '' Running on WindowsNT (on RISC)
            GOTO QUIT
        ELSE
            IF WIN32SSHIP$ = "YES" THEN
                ReplaceListItem INFOTEXT$, 1, EXITFAILNOTENH1$
                ExitCode% = EXITFAILNOTENH
                GOTO QUIT
            ENDIF
        ENDIF
    ENDIF
    IF OnWindowsNT() THEN
        ExitCode% = EXITSUCCESS
        WIN32ENABLED% = 1
        GOTO WELCOME
    ENDIF
    IF GetWindowsMajorVersion >= 4 THEN
        ExitCode% = EXITSUCCESS
        WIN32ENABLED% = 1
    ENDIF
    ExitCode% = EXITSUCCESS

WELCOME:

    IF HANDBOOKNAME$ = "" THEN
        ReplaceListItem INFOTEXT$, 1, WELCOME11$
        ReplaceListItem INFOTEXT$, 2, WELCOME12$
        ReplaceListItem INFOTEXT$, 3, WELCOME13$
        ReplaceListItem INFOTEXT$, 4, WELCOME14$
        ReplaceListItem INFOTEXT$, 5, WELCOME15$
        ReplaceListItem INFOTEXT$, 6, HELPWELCOME1$
        ReplaceListItem INFOTEXT$, 7, HELPWELCOME2$
        ReplaceListItem INFOTEXT$, 8, HELPWELCOME3$
        sz$ = UIStartDlg(CUIDLL$, WELCOME1, "FCustInstDlgProc", HELPWELCOME, HELPPROC$)
        IF sz$ = "OK" THEN
            UIPop 1
        ELSEIF sz$ = "REACTIVATE" THEN
            GOTO WELCOME
        ELSEIF sz$ = "NOTES" THEN
            GOSUB READNOTES
            GOTO WELCOME
        ELSE
            UIPop 1
            GOSUB ASKQUIT
            GOTO WELCOME
        ENDIF
    ELSE
        ReplaceListItem INFOTEXT$, 1, WELCOM1$
        ReplaceListItem INFOTEXT$, 3, WELCOM2$
        ReplaceListItem INFOTEXT$, 4, WELCOM3$
        ReplaceListItem INFOTEXT$, 5, WELCOM4$
        ReplaceListItem INFOTEXT$, 6, HELPWELCOME1$
        ReplaceListItem INFOTEXT$, 7, HELPWELCOME2$
        ReplaceListItem INFOTEXT$, 8, HELPWELCOME3$
        sz$ = UIStartDlg(CUIDLL$, WELCOME, "FCustInstDlgProc", HELPWELCOME, HELPPROC$)
        IF sz$ = "OK" THEN
            UIPop 1
        ELSEIF sz$ = "REACTIVATE" THEN
            GOTO WELCOME
        ELSE
            UIPop 1
            GOSUB ASKQUIT
            GOTO WELCOME
        ENDIF
    ENDIF

EFIHANDLER:

    IF EFISHIP$ = "YES" AND EFICHOICE$ = "YES" THEN
        IF MCADVERSION$ = "4.0" THEN
            SetSymbolValue "RadioDefault", "1"
        ELSE
            SetSymbolValue "RadioDefault", "2"
        ENDIF

        VERSION2$ = "The installation has determined that Mathcad " + MCADVERSION$ + " is installed."
        ReplaceListItem INFOTEXT$, 1, VERSION1$
        ReplaceListItem INFOTEXT$, 2, VERSION2$
        ReplaceListItem INFOTEXT$, 3, VERSION3$
        ReplaceListItem INFOTEXT$, 4, VERSION4$

        sz$ = UIStartDlg(CUIDLL$, VERSION, "FRadioDlgProc", 0, "")

        IF sz$ = "REACTIVATE" THEN
            GOTO EFIHANDLER
        ELSEIF sz$ = "QUIT INSTALLATION" THEN
            UIPop 1
            GOSUB ASKQUIT
            GOTO EFIHANDLER
        ELSEIF sz$ = "OK" THEN
        Print GetSymbolValue("ButtonChecked")
            IF GetSymbolValue("ButtonChecked") = "1" THEN
                MCADVERSION$ = "4.0"
            ELSEIF GetSymbolValue("ButtonChecked") = "2" THEN
                MCADVERSION$ = "3.1"
            ELSE
                GOTO EFIHANDLER
            ENDIF
        ELSE
            GOTO EFIHANDLER
        ENDIF

        ENDIF
        UIPop 1

SOLUTIONHANDLER:

''    IF SOLUTIONS$ = "YES" THEN
''    ENDIF

GETDEST:

    IF MCADVERSION$ = "4.0" THEN
        DESTSYS$ = GetRegKeyValue("Mathcad\shell\open\command")
        IF DESTSYS$ = "" THEN
            DESTSYS$ = SUGGESTEDPATH$
        ELSE
            FOR l%= LEN(DESTSYS$) TO 1 STEP -1
                IF MID$(DESTSYS$, l%, 1) = "\" THEN
                    DESTSYS$ = MID$(DESTSYS$, 1, l% - 1)
                    l% = 1
                ENDIF
            NEXT l%
            IF HANDBOOKNAME$ <> "" THEN
                DESTSYS$ = MakePath(DESTSYS$, "")
                DESTSYS$ = DESTSYS$ + "HANDBOOK\"
            ENDIF
        ENDIF
    ELSE
        DESTSYS$ = SUGGESTEDPATH$
    ENDIF

    DESTSYS$ = MakePath(DESTSYS$, "")
DESTPATH:

    SetSymbolValue "EditTextIn", DESTSYS$
    SetSymbolValue "EditFocus", "ALL"
    ReplaceListItem INFOTEXT$, 1, DESTPATH1$
    ReplaceListItem INFOTEXT$, 2, HELPDEST1$
    ReplaceListItem INFOTEXT$, 3, HELPDEST2$
    sz$ = UIStartDlg(CUIDLL$, DESTPATH, "FCustDestDlgProc", HELPDEST, HELPPROC$)

    IF sz$ = "OK" THEN
        DESTSYS$ = GetSymbolValue("EditTextOut")
        DESTSYS$ = MakePath(DESTSYS$, "")
        IF LEN(DESTSYS$) > DESTPATHLEN% THEN
            GOSUB DESTPATHTOOBIG
            DESTSYS$ = MakePath(MID$(DESTSYS$, 1, DESTPATHLEN%), "")
            UIPop 1
            GOTO DESTPATH
        ENDIF
        IF IsDirWritable(DESTSYS$) = 0 THEN
            GOSUB BADPATH
            GOTO DESTPATH
        ENDIF
        IF GetFreeSpaceforDrive(MID$(DESTSYS$, 1, 1)) < SizeRequired& THEN
            GOSUB TOOBIG
            GOTO DESTPATH
        ENDIF
    ELSEIF sz$ = "REACTIVATE" THEN
        GOTO GETDEST
    ELSE
        UIPop 1
        GOSUB ASKQUIT
        GOTO GETDEST
    ENDIF

    DESTEFI$ = MakePath(UpaLevel(DESTSYS$, ""), "")
    UIPop 1


COPYFILES:

    o% = InitUnarj()
    CreateDir DESTSYS$, cmoNone

    IF HANDBOOKNAME$ <> "" THEN
        DESTINATION$ = MakePath(UpaLevel(DESTSYS$, ""), "")
    ELSE
        DESTINATION$ = MAKEPATH(DESTSYS$, "")
    ENDIF

    LogFile$ = DESTINATION$
    OpenLogFile MakePath(LogFile$, "MATHCAD.LOG"), 0
    IF WIN32SSHIP$ = "YES" THEN
        IF "1" <> GetIniKeyString (DEST32S$ + "WIN32S.INI", "Win32s", "Setup") THEN
            CreateIniKeyValue DEST32S$ + "WIN32S.INI", "Win32s", "Setup", "0", cmoOverwrite
        ELSE
            WIN32ENABLED% = 1
        ENDIF
        Win32Ver$ =  GetVersionOfFile(GetSymbolValue("STF_SRCDIR") + "WIN32S16.DLL")
    ENDIF
    DESTINATION$ = LogFile$
    '$IFDEF PRODUCT
        UpdateMcadIni
        UpdateSystemIni
        UpdateWinini
    '$ENDIF
    IF MCADVERSION$ = "4.0" AND (EFISHIP$ = "YES" OR WIN32SSHIP$ = "YES") THEN
        IF DoesFileExist (DESTINATION$ + "commdlg.dll", femExist) = 1 THEN
            RemoveFile DESTINATION$ + "commdlg.dll", cmoVital
        ENDIF
        IF DoesFileExist (DESTINATION$ + "olecli.dll", femExist) = 1 THEN
            RemoveFile DESTINATION$ + "olecli.dll", cmoVital
        ENDIF
        IF DoesFileExist (DESTINATION$ + "olesvr.dll", femExist) = 1 THEN
            RemoveFile DESTINATION$ + "olesvr.dll", cmoVital
        ENDIF
        IF DoesFileExist (DESTINATION$ + "shell.dll", femExist) = 1 THEN
            RemoveFile DESTINATION$ + "shell.dll", cmoVital
        ENDIF
        IF DoesFileExist (DESTINATION$ + "efiutil.dll", femExist) = 1 THEN
            RemoveFile DESTINATION$ + "efiutil.dll", cmoVital
        ENDIF
    ENDIF
    GOSUB Install

    '$IFDEF PRODUCT
        CreateProgmanGroup "Mathcad 4.0", "", cmoNone
        ShowProgmanGroup   "Mathcad 4.0", 1, cmoNone
        CreateProgmanItem  "Mathcad 4.0", "Mathcad 4.0", DESTINATION$ + "MCAD.EXE", "", cmoOverwrite
        RegisterMcad
    '$ENDIF
    CloseLogFile
    RemoveFile Logfile$ + "mathcad.log", cmoVital
    o% = FinalUnarj()

QUIT:
    ON ERROR GOTO ERRQUIT
    IF ERR = 0 THEN
        dlg% = ExitCode%
    ELSEIF ERR = STFQUIT THEN
        ReplaceListItem INFOTEXT$, 1, EXITQUIT1$
        ReplaceListItem INFOTEXT$, 2, EXITQUIT2$
        dlg% = EXITQUIT
    ELSE
        ReplaceListItem INFOTEXT$, 1, EXITFAILURE1$
        dlg% = EXITFAILURE
    ENDIF

QUITL1:
    IF RestarWin% = 5 and dlg% = EXITSUCCESS THEN
        ReplaceListItem INFOTEXT$, 1, EXITSUCCESSREBOOT1$
        ReplaceListItem INFOTEXT$, 2, EXITSUCCESSREBOOT2$
        sz$ = UIStartDlg(CUIDLL$, EXITSUCCESSREBOOT, "FCustInstDlgProc", 0, "")
            IF sz$ = "QUIT INSTALLATION" THEN
                END
            ELSEIF sz$ = "REACTIVATE" THEN
                GOTO QUITL1
            ELSE
                UiPop ALL
                RebootSystem
                END
            ENDIF
    ELSE
        IF RestarWin% = 5 THEN
            RemoveFile DEST32S$ + "WIN32S.INI", cmoVital
        ELSEIF dlg% = EXITSUCCESS THEN
            ReplaceListItem INFOTEXT$, 1, EXITSUCCESS1$
            ReplaceListItem INFOTEXT$, 2, EXITSUCCESS2$
        ENDIF
        sz$ = UIStartDlg(CUIDLL$, dlg%, "FCustInstDlgProc", 0, "")
    ENDIF
    IF sz$ = "REACTIVATE" THEN
        GOTO QUITL1
    ENDIF
    UIPop 1

    END

ERRQUIT:
    i% = DoMsgBox("The installation has failed.", "Setup Message", MB_OK+MB_TASKMODAL+MB_ICONHAND)
    END

BADPATH:

    BADPATH1$ = "The path:  " + DESTSYS$ + " is not valid."
    ReplaceListItem INFOTEXT$, 1, BADPATH1$
    sz$ = UIStartDlg(CUIDLL$, BADPATH, "FCustInstDlgProc", 0, "")
    IF sz$ = "REACTIVATE" THEN
        GOTO BADPATH
    ENDIF
    UIPop 1
    RETURN

TOOBIG:
    FREESPACE$ = STR$(GetFreeSpaceforDrive(MID$(DESTSYS$, 1, 1)) / 1024)
    NEEDSPACE$ = STR$((SizeRequired/1024) - (GetFreeSpaceforDrive(MID$(DESTSYS$, 1 ,1))/1024))
    TOOBIG3$ = TOOBIG6$ + LTRIM$(FREESPACE$) + TOOBIG7$
    TOOBIG4$ = TOOBIG8$ + LTRIM$(NEEDSPACE$) + TOOBIG9$
    ReplaceListItem INFOTEXT$, 1, TOOBIG1$
    ReplaceListItem INFOTEXT$, 2, TOOBIG2$
    ReplaceListItem INFOTEXT$, 3, TOOBIG3$
    ReplaceListItem INFOTEXT$, 4, TOOBIG4$
    ReplaceListItem INFOTEXT$, 5, TOOBIG5$
    sz$ = UIStartDlg(CUIDLL$, TOOBIG, "FCustInstDlgProc", 0, "")
    IF sz$ = "REACTIVATE" THEN
        GOTO TOOBIG
    ENDIF
    UIPop 1
    RETURN

DESTPATHTOOBIG:
    ReplaceListItem INFOTEXT$, 1, DESTTOOBIG1$
    sz$ = UIStartDlg(CUIDLL$, DESTTOOBIG, "FCustInstDlgProc", 0, "")
    IF sz$ = "REACTIVATE" THEN
        GOTO DESTPATHTOOBIG
    ENDIF
    UIPop 1
    RETURN

  ASKQUIT:

    ReplaceListItem INFOTEXT$, 1, ASKQUIT1$
    ReplaceListItem INFOTEXT$, 2, ASKQUIT2$

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

    IF sz$ = "QUIT INSTALLATION" THEN
        UIPopAll
        ERROR STFQUIT
    ELSEIF sz$ = "REACTIVATE" THEN
        GOTO ASKQUIT
    ELSE
        UIPop 1
    ENDIF
    RETURN

READNOTES:

'$IFDEF PRODUCT
    o% = READNOTES()
'$ENDIF
RETURN

Function Dearj(destdir$, filename$, titlebox$, sourcedir$) STATIC as Integer

    PRINT SOURCEDIR$
    PRINT FILENAME$
    PRINT DESTDIR$
    PRINT TITLEBOX$

  o% =   Unarj(sourcedir$, filename$, destdir$, titlebox$, HwndFrame())

    PRINT o%

    IF o% <> 0 THEN
        DEARJ = 1
    ELSE
        DEARJ = 0
    ENDIF
END FUNCTION

'**
'** Purpose:
'**     Builds the copy list and performs all installation operations.
'** Arguments:
'**     none.
'** Returns:
'**     none.
'*************************************************************************
Install:

    SrcDir$ = GetSymbolValue("STF_SRCDIR")

    WriteToLogFile ""
    WriteToLogFile "  User chose as destination directory: '" + DESTSYS$ + "'"
    WriteToLogFile ""
    WriteToLogFile "May have had to create the directory: " + DESTSYS$
    WriteToLogFile ""

    IF HANDBOOKNAME$ <> "" THEN
        DESTINATION$ = MakePath(UpaLevel(DESTSYS$, ""), "")
    ELSE
        DESTINATION$ = MAKEPATH(DESTSYS$, "")
    ENDIF
    ClearCopyList
    AddSectionKeyFileToCopyList "Files", "Unarj", SrcDir$, DESTINATION$
    IF HANDBOOKNAME$ = "" THEN
        AddSectionKeyFileToCopyList "Files", "Relnotes", SrcDir$, DESTINATION$
    ENDIF
    CopyFilesInCopyList

    ClearCopyList

'$IFDEF PRODUCT
    IF WIN32SSHIP$ = "YES" THEN
        szOldVer$ = GetVersionOfFile(DEST32S$ + "WIN32S16.DLL")
        szNewVer$ = GetVersionOfFile( SrcDir$ + "WIN32S16.DLL")
        IF szOldVer$ = "" OR szNewVer$ > szOldVer$ OR WIN32ENABLED% <> 1 THEN
WINLIBS:
            AddSectionFilesToCopyList "WindowsSystem", SrcDir$, DEST32S$
            AddSectionFilesToCopyList "WindowsSystemWin32s", SrcDir$, DEST32S$ + "WIN32S\"
            t% = RestartWindows(GetWindowsSysDir() + "OLECLI.DLL")
            SetRestartDir GetWindowsDir()
            CopyFilesInCopyList
            RestarWin% = 5
            UpdateWin32sIni
        ENDIF
    ENDIF
'$ENDIF
    x = 1
    IF EFICHOICE$ = "YES" and MCADVERSION$ = "4.0" THEN
        x = 2
    ENDIF
    WHILE x <= NUMBEROFARCHIVES%
        IF (EFISHIP$ = "YES" and x = 1) or (EFICHOICE$ = "YES" and x = 2) THEN
            DESTINATION$ = MAKEPATH(DESTEFI$, "")
        ELSE
            DESTINATION$ = MAKEPATH(DESTSYS$, "")
        ENDIF
        ClearCopyList
        ARCNAME$ = "Archive" + STR$(x)
        AddSectionFilestoCopyList ARCNAME$, SrcDir$, DESTINATION$
        CopyFilesInCopyList
        IF x = 1 THEN
            o% = Dearj(DESTINATION$,"mcad.arj", UNCOMPRESSTITLE$, DESTINATION$)
        ELSEIF x = 10 THEN
            o% = Dearj(DESTINATION$,"mcad.a09", UNCOMPRESSTITLE$, DESTINATION$)
        ELSEIF x > 10 THEN
           o% = Dearj(DESTINATION$, "mcad.a" + LTRIM$(STR$(x-1)), UNCOMPRESSTITLE$, DESTINATION$)
        ELSE
            o% = Dearj(DESTINATION$, "mcad.a0" + LTRIM$(STR$(x - 1)), UNCOMPRESSTITLE$, DESTINATION$)
        ENDIF
        WriteToLogFile "Message from Dearj " + STR$(o%)
        DESTINATION$ = MakePath(DESTINATION$, "")
        IF x = 1 THEN
            RemoveFile DESTINATION$ + "mcad.arj", cmoForce
        ELSEIF x = 10 THEN
            RemoveFile DESTINATION$ + "mcad.a09", cmoForce
        ELSEIF x > 10 THEN
            RemoveFile DESTINATION$ + "mcad.a" + LTRIM$(STR$(x - 1)), cmoForce
        ELSE
            RemoveFile DESTINATION$ + "mcad.a0" + LTRIM$(STR$(x - 1)), cmoForce
        ENDIF
        IF o% = 1 THEN
            ERR = 1
            GOSUB QUIT
            GOTO INSTALL
        ENDIF
        IF x = 1 and EFISHIP$ = "YES" and MCADVERSION$ = "3.1" THEN
            x = 2
        ENDIF
        x = x + 1
    WEND

RETURN

SUB UpdateSystemIni STATIC

    VxDPath$ = DEST32S$ + "WIN32S\W32S.386"
    SystemIniPath$ = GetWindowsDir()

    t% = MakeSystemIni(SystemIniPath$, VxdPath$)

END SUB

'$IFDEF PRODUCT

    SUB UpdateWinini Static

        VxDPath$ = DESTSYS$
        WinIniPath$ = GetWindowsDir() + "Win.Ini"
        IniSect$ = "Mathcad"
        IniKey$ =  "Private-Ini-File"
        t% = UpdateIni(IniSect$, IniKey$, VxDPath$, WinIniPath$)

    END SUB

    SUB UpdateMcadIni Static

        VxDPath$ = DESTSYS$ + "HANDBOOK"
        IniFile$ = DESTSYS$ + "MCAD.INI"
        IniSect$ = "Handbooks"
        IniKey$  = "Handbook_Path"

        t% = UpdateIni(IniSect$, IniKey$, VxDPath$, IniFile$)

        IniSect$ = "Libraries"
        IniKey$  = "Libraries"
        VxDPath$ = MID$(DESTSYS$, 1, Len(DESTSYS$) - 1)

        t% = UpdateIni(IniSect$, IniKey$, VxDPath$, IniFile$)

    END SUB

    SUB UpdateWin32sIni Static

        IniFile$ = GetWindowsDir() + "SYSTEM\WIN32S.INI"
        IniSect$ = "Win32s"
        IniKey$ = "Setup"
        VxDPath$ = "1"

        t% = UpdateIni(IniSect$, IniKey$, VxDPath$, IniFile$)

        IniKey$ = "Version"
        t% = UpdateIni(IniSect$, IniKey$, WIN32VER$, IniFile$)

    END SUB

'$ENDIF
SUB RebootSystem Static

    i% = RestartListEmpty()
    i% = ExitExecRestart()

END SUB

SUB RegisterMcad Static

    CreateRegKeyValue ".mcd", "Mathcad"
    CreateRegKeyValue ".hbk", "MathcadHandbook"

    CreateRegKeyValue "MathcadHandbook", "Mathcad Handbook"
    CreateRegKeyValue "MathcadHandbook\shell\open\ddeexec","[Open(" + CHR$(34) + "%1" + CHR$(34) + ")]"
    CreateRegKeyValue "MathcadHandbook\shell\open\ddeexec\Topic", "System"
    CreateRegKeyValue "MathcadHandbook\shell\open\ddeexec\application", "MCAD"
    CreateRegKeyValue "MathcadHandbook\shell\open\command", DESTSYS$ + "MCAD.EXE"

    CreateRegKeyValue "Mathcad", "Mathcad 4.0"
    CreateRegKeyValue "Mathcad\shell\open\ddeexec","[Open(" + CHR$(34) + "%1" + CHR$(34) + ")]"
    CreateRegKeyValue "Mathcad\shell\open\ddeexec\Topic", "System"
    CreateRegKeyValue "Mathcad\shell\open\ddeexec\application", "MCAD"
    CreateRegKeyValue "Mathcad\shell\open\command", DESTSYS$ + "MCAD.EXE"
    CreateRegKeyValue "Mathcad\protocol\StdFileEditing\RequestDataFormats", "Native,CF_METAFILEPICT"
    CreateRegKeyValue "Mathcad\protocol\StdFileEditing\SetDataFormats", "Native"
    CreateRegKeyValue "Mathcad\protocol\StdFileEditing\verb\0", "Edit"
    CreateRegKeyValue "Mathcad\protocol\StdFileEditing\server", DESTSYS$ + "MCAD.EXE"

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 MID$(szDir$, LEN(szDir$), 1) = "\" THEN
        MakePath = szDir$ + szFile$
    ELSEIF szFile$ = "" THEN
        IF MID$(szDir$, LEN(szDir$), 1) = "\" THEN
            MakePath = szDir$
        ELSE
            MakePath = szDir$ + "\"
        ENDIF
    ELSE
        MakePath = szDir$ + "\" + szFile$
    ENDIF
END FUNCTION

''** Purpose

FUNCTION UpaLevel (szDir$, szFile$) STATIC AS STRING
    IF szDir$ = "" THEN
        UpaLevel = "Error"
    ELSEIF MID$(szDir$, LEN(szDir$) - 1, 1) = ":" THEN
        UpaLevel = szDir$
    ELSEIF MID$(szDir$, LEN(szDir$), 1) = "\" THEN
        szFile$ = MID$(szDir$, 1, LEN(szDir$) - 1)
        FOR l% = LEN(szFile$) TO 1 STEP -1
            IF MID$(szFile$, l%, 1) = "\" THEN
                szFile$ = MID$(szFile$, 1, l%)
                l% = 1
            ENDIF
        NEXT l%
        UpaLevel = szFile$
    ENDIF
END FUNCTION

