'**************************************************************************
'*                       MSSetup Toolkit Sample 1
'**************************************************************************

'$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 OPTIONS       = 800
CONST APPHELP       = 900
CONST FREESPACE     = 1000
CONST FREESPACE2    = 2000
CONST QSND          = 3000
CONST BADPATH       = 6400
''QSound variables          
 CONST IDSSI&        = 3191  ''Manufacturer's ID
 CONST QSIZE%        = 4844  '' disk space needed for q stereo files
 CONST MONOSIZE%     = 2534  '' disk space needed for mono files
 CONST NRATE%        = 22050 '' sample rate
 CONST NBITS%        = 8     '' bit resolution of samples
 CONST TRUE%         = 1     '' why? qCheckCaps seems to return reversed
 CONST FALSE%        = 0     '' BOOL values!
 CONST PT_INSTALL%   = 0     '' give a choice to user
 CONST PT_TEST%      = 1     '' test a qsound file

''Bitmap ID
CONST LOGO = 1
'' Installed drive capacity 
CONST DRIVCAP1%       = 2534000
CONST DRIVCAP2%       = 4844000

GLOBAL DEST$        ''Default destination directory.
GLOBAL DRIVE$       ''Holds desk drive letter
GLOBAL OPTCUR$      ''Option selection from option dialog.

DECLARE SUB Install
DECLARE FUNCTION MakePath (szDir$, szFile$) AS STRING
DECLARE FUNCTION SonarcX  LIB "SONLIB.DLL" (inp$, outp$, delold%, showname%) AS INTEGER
DECLARE FUNCTION qTellAboutQ LIB "QSOUND.DLL" (parent%, testfile$, qsizer%, monosizer%, nPrompt%) as INTEGER
DECLARE FUNCTION qCheckCaps LIB "QSOUND.DLL" (nRate%, nBits%) as INTEGER
DECLARE FUNCTION qQExtract LIB "QSOUND.DLL" (infile$, outfile$, id&) as INTEGER
DECLARE FUNCTION GetQErrorText LIB "QSOUND.DLL" (err%, errmsg$, buffsize%) as INTEGER


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

    SetBitmap CUIDLL$, LOGO
    SetTitle "Empire Strikes Back AudioClips"

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

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

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


'' OPTION:
''     SetSymbolValue "RadioDefault", OPTCUR$
'' OPTL1:
''     sz$ = UIStartDlg(CUIDLL$, OPTIONS, "FRadioDlgProc", APPHELP, HELPPROC$)
''     OPTCUR$ = GetSymbolValue("ButtonChecked")
 
''     IF sz$ = "CONTINUE" THEN
''         UIPop(1)
''     ELSEIF sz$ = "REACTIVATE" THEN
''         GOTO OPTL1
''     ELSE
''         GOSUB ASKQUIT
''         GOTO OPTION
''     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 OPTION
    ELSE
        GOSUB ASKQUIT
    END IF

    DRIVE$ = MID$(DEST$, 1, 1)
    IF GetFreeSpaceForDrive(DRIVE$) < DRIVCAP1 THEN
       GOTO SPACEERROR 
    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:
    sz$ = UIStartDlg(CUIDLL$, dlg%, "FInfo0DlgProc", 0, "")
    IF sz$ = "REACTIVATE" THEN
        GOTO QUITL1
    END IF
    UIPop 1

    END

ERRQUIT:
    i% = DoMsgBox("Setup sources were corrupted, call Product Support!", "Empire Strikes BackPersonal Desktop AudioClips", MB_OK+MB_TASKMODAL+MB_ICONHAND)
    END



SPACEERROR:
    sz$ = UIStartDlg(CUIDLL$, FREESPACE, "FInfoDlgProc", APPHELP, HELPPROC$)
    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



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

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


    AddSectionFilesToCopyList "Files", SrcDir$, DEST$
'    AddSectionKeyFileToCopyList "Options", OPTCUR$, SrcDir$, DEST$
    CopyFilesInCopyList
     ini$ = MakePath(DEST$,"EMPIRE\*.wv")
     ini2$ = MakePath(DEST$,"EMPIRE\")
     i = SonarcX(ini$, ini2$, TRUE, TRUE)
     IF I <> 0 THEN
    j% = DoMsgBox("FILE I/O ERROR OR DISK FULL", "Personal Desktop Setup", MB_OK+MB_TASKMODAL+MB_ICONHAND)
        ERROR STFQUIT
     END IF
' Start of QSound stuff
' 1st, try setting some variable assignments to pass to QDLL
    qini$ = MakePath(DEST$,"EMPIRE\")
    q_able% = qCheckCaps (NRATE%, NBITS%)
  IF q_able% = FALSE THEN
  sz$ = UIStartDlg(CUIDLL$, QSND, "FInfoDlgProc", APPHELP, HELPPROC$)
' j% = DoMsgBox("No stereo soundcard installed!", "Personal Desktop Setup", MB_OK+MB_TASKMODAL+MB_ICONHAND)
  ELSEIF q_able% = TRUE THEN
 ' extract demo qfile
      infile$ = qini$ + "afraid.wav"
      outfile$ = qini$ + "afraid.q"
      k% = qQExtract(infile$, outfile$, IDSSI&)
      z = qTellAboutQ(0,outfile$,QSIZE%,MONOSIZE%,PT_INSTALL)
        KILL outfile$
        IF z = 1 THEN

    DRIVE$ = MID$(DEST$, 1, 1)
    IF GetFreeSpaceForDrive(DRIVE$) < DRIVCAP2 THEN
       sz$ = UIStartDlg(CUIDLL$, FREESPACE2, "FInfoDlgProc", APPHELP, HELPPROC$)
    END
    END IF
 'Qextract all files
        k%= qQExtract((qini$ + "*.wav"), NULL,IDSSI&)

     IF k <> 0 THEN
'    y% = GetQErrorText(k%, errmsg$, 100)   
 ERR1$ = "Q_COULD_NOT_OPEN"
 ERR2$ = "Q_NO_WAVE_CHUNK"
 ERR4$ = "Q_NO_FORMAT_CHUNK"
 ERR5$ = "Q_COUDNT_READ_FORMAT"
 ERR6$ = "Q_BAD_FORMAT"
 ERR7$ = "Q_NO_DATA_CHUNK"
 ERR8$ = "Q_OUT_OF_MEMORY"
 ERR9$ = "Q_ERROR_READING_DATA"
 ERR10$ = "Q_GOT_FILE"
 ERR11$ = "Q_COULD_NOT_WRITE"
 ERR12$ = "Q_COUDNT_READ_QCHUNK"
 ERR13$ = "Q_BAD_QCHUNK"

    IF k = 1 THEN
          errmsg$ = ERR1$
       ELSEIF k = 2 THEN
          errmsg$ = ERR2$
       ELSEIF k = 4 THEN
          errmsg$ = ERR4$
       ELSEIF k = 5 THEN
          errmsg$ = ERR5$
       ELSEIF k = 6 THEN
          errmsg$ = ERR6$
       ELSEIF k = 7 THEN
          errmsg$ = ERR7$
       ELSEIF k = 8 THEN
          errmsg$ = ERR8$
       ELSEIF k = 9 THEN
          errmsg$ = ERR9$
       ELSEIF k = 10 THEN
          errmsg$ = ERR10$
       ELSEIF k = 11 THEN
          errmsg$ = ERR11$
       ELSEIF k = 12 THEN
          errmsg$ = ERR12$
       ELSEIF k = 13 THEN
          errmsg$ = ERR13$
    ENDIF
   j% = DoMsgBox(errmsg$, "Personal Desktop Setup", MB_OK+MB_TASKMODAL+MB_ICONHAND)
    ERROR STFQUIT


  
  'j% = DoMsgBox(errmsg$, Personal Desktop Setup", MB_OK+MB_TASKMODAL+MB_ICONHAND)
  '   ERROR STFQUIT

 END IF
     END IF
     END IF
    UIPopAll


    CreateProgmanGroup "AudioClips", "", cmoNone
    ShowProgmanGroup  "AudioClips", 1, cmoNone
    CreateProgmanItem "AudioClips", "Personal Desktop", MakePath(DEST$,"PDESK.EXE"), "", cmoOverwrite
    CreateProgmanItem "AudioClips", "Readme.Txt", "notepad.exe "+MakePath(DEST$,"readme.txt"), "", cmoOverwrite
    CreateProgmanItem "StartUp", "Personal Desktop", MakePath(DEST$,"PDESK.EXE"), "", cmoOverwrite


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


