'**************************************************************************
'*                       MSSetup Toolkit Sample 1
'**************************************************************************
'$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 EXITSUCCESSNOBOOT   = 701
CONST OPTIONS       = 800
CONST APPHELP       = 900
CONST BADPATH       = 6400

''Bitmap ID
CONST LOGO = 1

GLOBAL DEST$        ''Default destination directory.
GLOBAL OPTCUR$      ''Option selection from option dialog.
GLOBAL PRODNAME$
GLOBAL WINDIR$
GLOBAL WINSYSDIR$

DECLARE SUB Install
DECLARE FUNCTION MakePath (szDir$, szFile$) AS STRING
DECLARE FUNCTION _MaxWin LIB "mscuistf.dll" (hwnd%) AS INTEGER


INIT:
    PRODNAME$ = "Vivid Clarity"
    WINDIR$ = GetWindowsDir
    WINSYSDIR$ = WINDIR$ + "SYSTEM\"
    CUIDLL$ = "mscuistf.dll"            ''Custom user interface dll
    HELPPROC$ = "FHelpDlgProc"          ''Help dialog procedure
    hwndF% = HwndFrame()
    dummy% = _MaxWin(hwndF%)
    SetRestartDir(WINDIR$)
    i% = SetBeepingMode(1)
    SetBitmap CUIDLL$, LOGO
    SetTitle PRODNAME$

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

    OPTCUR$ = "1"
    DEST$ = "C:\CLARITY"

'$IFDEF DEBUG
    i% = SetSizeCheckMode(scmOnIgnore)    '' could use scmOff; def = scmOnFatal
    WinDrive$ = MID$(GetWindowsDir, 1, 1)
    IF IsDriveValid(WinDrive$) = 0 THEN
	i% = DoMsgBox("Windows drive ('"+WinDrive$+"') is not a valid drive.", "DEBUG", MB_TASKMODAL+MB_ICONHAND+MB_OK)
	GOTO QUIT
    END IF
'$ENDIF ''DEBUG


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
    Install

QUIT:
    ON ERROR GOTO ERRQUIT

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

ERRQUIT:
    i% = DoMsgBox("Setup sources were corrupted, call 1-800-706-8483!", "Setup Message", MB_OK+MB_TASKMODAL+MB_ICONHAND)
    END

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

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

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

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

'    OpenLogFile MakePath(DEST$, "LOGFILE.OUT"), 0
'    WriteToLogFile ""
'    WriteToLogFile "  User chose as destination directory: '" + DEST$ + "'"
'    WriteToLogFile "  User chose option: '" + OPTCUR$ + "'"
'    WriteToLogFile ""
'    WriteToLogFile "May have had to create the directory: " + DEST$
'    WriteToLogFile ""

    AddSectionFilesToCopyList "Files", SrcDir$, DEST$
    AddSectionFilesToCopyList "OLEFILES", SrcDir$, WINSYSDIR$
    AddSectionFilesToCopyList "CTL3DFILES", SrcDir$, WINSYSDIR$
    AddSectionFilesToCopyList "DBFILES", SrcDir$, WINSYSDIR$
    AddSectionFilesToCopyList "INIFILES", SrcDir$, WINDIR$
    CopyFilesInCopyList

    CreateProgmanGroup PRODNAME$, "", cmoNone
    ShowProgmanGroup  PRODNAME$, 1, cmoNone
	overWrite% = cmoOverwrite
    CreateProgmanItem PRODNAME$, "Vivid Clarity", MakePath(DEST$,"CLARITY.EXE"), "", overWrite%
    CreateProgmanItem PRODNAME$, "Quick Tour", MakePath(DEST$,"QTOUR.EXE"), "", overWrite%
    CopyFile MakePath(DEST$,"SAMPLE.OBG"),MakePath(DEST$,"ABCPROJ.OBG"),0,0
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
