'**************************************************************************
'*                       TeamOFFICE installation script
'*                           C A L E N D A R 
'*                        version 3.0
'*                       
'*   Date :      26.10.93
'*   Author:     Arja Pietik„inen & RV
'*               Modify: 30.6.94  NWIPXSPX.DLL and NWNETAPI.DLL
'*                       has been moved in system directory and
'*                       the new file TOCONKPR.EX has been taken
'*                       in use. New version some other files. 
'**************************************************************************


'$INCLUDE 'setupapi.inc'
'$INCLUDE 'msdetect.inc'

DECLARE SUB SetFiletime LIB "toinst.dll" (lpString$)
DECLARE SUB TOInitTransport LIB "toinst.dll" (lpIni$)
DECLARE SUB TOCopyTransport LIB "toinst.dll" (lpIni$)
DECLARE SUB StartTOProc LIB "toinst.dll" (lbString$)
DECLARE SUB TOCompare LIB "toinst.dll" (lpDest$) 

''Dialog ID's
CONST ASKQUIT      = 200
CONST DESTPATH     = 300
CONST EXITFAILURE  = 400
CONST STARTFAILED  = 600
CONST EXITSUCCESS  = 700
CONST EXITSUCCESS_2 = 800
CONST APPHELP      = 900
CONST NETDRIVE     = 1100
CONST MAINHELP     = 1300
CONST COMPCHANGE   = 1600
CONST CUSTINST     = 6200
CONST TOOBIG       = 6300
CONST BADPATH      = 6400
CONST AOCUSING     = 6500
CONST ALARMUSING   = 6600
CONST TOOLONGPATH  = 7000
CONST WRONGVER     = 7100

''Bitmap ID
CONST TWLOGO         = 1

GLOBAL DEST$        '' Default destination directory.
GLOBAL AOPATH$      '' Path of AO
GLOBAL USERNAME$    '' Username
GLOBAL GROUPNAME$   '' Name of the group
GLOBAL TOITEM$      '' Name of the TO item
GLOBAL TOALARM$     '' Name of the TOALARM
GLOBAL STARTUP$     '' Name of the StartUp group
GLOBAL TOEXE$       '' Name of the installed program
GLOBAL TOINI$       '' Name of the installed program
GLOBAL TRSPORT$     '' Name of the TRSPORT.INI file 
GLOBAL ALARMCHECK$ 
GLOBAL AUTOCHECK$
GLOBAL PATHVAR$
GLOBAL TOCOMP$
GLOBAL RIVI$
GLOBAL RIVI2$
GLOBAL TOFILES$
GLOBAL RELNOTE$
GLOBAL COMFILES$
GLOBAL CUIDLL$
GLOBAL AUTOUPDATED$

''Dialog list symbol names
GLOBAL CHECKSTATES$
GLOBAL EDITTEXT$



DECLARE SUB Install
DECLARE SUB UpdateFiles
DECLARE SUB FileTimes
DECLARE SUB UpdateAutoexec
DECLARE FUNCTION MakePath (szDir$, szFile$) AS STRING


INIT:

'' *******************************************
'' DEFAULT TEXT HERE  for nationalisation
'' *******************************************
    
    GROUPNAME$  = "TeamWARE Office"
    TOITEM$     = "TeamWARE Calendar"  
    TOALARM$    = "TeamWARE Alarm"
    title$      = "TeamWARE Office Setup"
    STARTUP$    = "StartUp"
    RELNOTE$    = "TeamWARE Readme"
''  ******************************************

    CUIDLL$ = "toinst.dll"            ''custom user interface dll
    HELPPROC$ = "FHelpDlgProc"        ''Help dialog procedure

    SetBitmap CUIDLL$, TWLOGO
    SetTitle  title$ 

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

'' Needed list symbols
''
    ''CustInst list symbols
    CHECKSTATES$ = "CheckItemsState"
    STATUSTEXT$  = "StatusItemsText"
    EDITTEXT$    = "EditText"
    TOFILES$="TO FILES"
    COMFILES$="COM FILES"

''  Name of the program
    TOEXE$ = "tocal.exe"

''  Windows directory
    windir$ =  GetWindowsdir()
   
'' Path variable
    PATHVAR$ = ENVIRON$("PATH")
''  TOUI.INI 
    TOINI$ = MakePath(windir$, "TOUI.INI")
    
''  If any OFFICE program is installed earlier 
''  TOUI.INI exists 

    DEST$=""
    IF EXISTS(TOINI$) THEN
       DEST$ =GetIniKeyString(TOINI$, "UI", "office_directory")
    END IF

    IF DEST$ = "" THEN
       DEST$ = "C:\OFFICE"
    END IF
    AUTOUPDATED$ = "NO"

    FOR i% = 1 TO 2 STEP 1
        AddListItem CHECKSTATES$, "ON"
    NEXT i%

    FOR i% = 1 TO 1 STEP 1
        AddListItem EDITTEXT$, ""
    NEXT i%

'*********************************************************************
'
' CHECK THE VERSION NUMBER OF WINDOWS 
' ONLY 3.1 IS VALID
'
'*********************************************************************
VERSION:
     m%=GetWindowsMajorVersion()
     n%=GetWindowsMinorVersion()
     IF m%<3 THEN
         dlg%=WRONGVER 
         GOTO QUITL1  
     ELSEIF (m%=3) AND (n%<1) THEN
         dlg%=WRONGVER
         GOTO QUITL1  
     END IF


'**********************************************************************
'   INSTALLATION STARTS HERE
'   ASK THE PATH
'**********************************************************************

TOPATH:
    SetSymbolValue "EditTextIn", DEST$
    SetSymbolValue "EditFocus", "END"
TOPATH1:
    sz$ = UIStartDlg(CUIDLL$, DESTPATH, "FTOPathDlgProc", MAINHELP, HELPPROC$)
    IF sz$ = "CONTINUE" THEN
        olddest$ = DEST$
        DEST$ = GetSymbolValue("EditTextOut")
        SetSymbolValue "PathPath", PATHVAR$
        TOCompare(DEST$)
        d$ = GetSymbolValue("DestPath")
        ''Validate new path.
        IF IsDirWritable(d$) = 0 THEN
            dlg%=BADPATH
            GOSUB NOTES
            GOTO TOPATH1
        END IF

        a$  = GetSymbolValue ("IsAoconnUsing")
        IF a$ = "1" THEN
           dlg% = AOCUSING
           GOSUB NOTES
           GOTO ALARM
        END IF
ALARM:
        b$ = GetSymbolValue ("IsAlarmUsing")
        IF b$ = "1" THEN
           dlg% = ALARMUSING
           GOSUB NOTES
           GOTO TOPATH1
        ELSEIF a$ = "1" THEN
                GOTO TOPATH1
             END IF
           
        
'*************************************************************
' Check if given drive has enough space for TeamOFFICE program 
'*************************************************************
        ClearCopyList
        SrcDir$ = GetSymbolValue("STF_SRCDIR")
        AddSectionFilesToCopyList TOFILES$, SrcDir$, DEST$
        AddSectionFilesToCopyList COMFILES$, SrcDir$, GetWindowsSysDir()

        drive$ = MID$(DEST$, 1, 1)
        need&=GetCopyListCost (extras$,costs$,needed$)


'*************************************************
' There is not enough space for setup application
'*************************************************        
        IF  need& > 0 THEN
            dlg%=TOOBIG
            GOSUB NOTES
            GOTO TOPATH1
        END IF
'********
'  Check if given drive is network drive
'********
        drive$ = MID$(DEST$, 1, 1)

        IF IsDriveNetwork (drive$) THEN
             GOSUB NETWORK
             GOTO TOPATH1             
        END IF
        UIPop 1
        olddest$ = ""
        GOTO OPTIONS
    ELSEIF sz$ = "REACTIVATE" THEN
        GOTO TOPATH1
    ELSEIF sz$ = "EXIT" THEN
        GOSUB ASKQUIT
        GOTO TOPATH1
    ELSE
        GOTO TOPATH1
    END IF


'************************************************
'   OTHER PARAMETERS
'
'************************************************

OPTIONS:
    '' * Network Softfware

    ReplaceListItem CHECKSTATES$, 1, "ON"
    ReplaceListItem CHECKSTATES$, 2, "ON"

    TOCOMP$ = ""

    IF EXISTS(TOINI$) THEN
       trpinf$ = GetIniKeyString(TOINI$, "TRANSPORT","3")
    ENDIF
    IF trpinf$ = "" THEN
       TOCOMP$ = GetEnvVariableValue ("TOCOMPUTER")    
    ENDIF
    SetSymbolValue "EditTextIn", TOINI$
    SetSymbolValue "EditFocus", "END"

CUST:
    
    sz$ = UIStartDlg(CUIDLL$, CUSTINST, "FTOParamDlgProc", APPHELP, HELPPROC$)
    IF sz$ = "CONTINUE" THEN
        USERNAME$ = GetListItem(EDITTEXT$,1)
        AUTOCHECK$ = GetListItem(CHECKSTATES$,  1)
        ALARMCHECK$  = GetListItem (CHECKSTATES$, 2)
        UIPop 1
        SetSymbolValue "SetConfigure" ,""
        Install
        b$ = GetSymbolValue("SetConfigure")
        IF b$ <> "" THEN
           END
        ENDIF
        GOTO QUIT
    ELSEIF sz$ = "BACK" THEN
        UIPop 1       
        GOTO TOPATH
    ELSEIF sz$ = "EXIT" THEN
        GOSUB ASKQUIT
        GOTO OPTIONS
    ELSE
        GOTO OPTIONS
    END IF

QUIT:
    ON ERROR GOTO ERRQUIT
    IF ERR = 0 THEN
        IF AUTOUPDATED$ = "NO" THEN
            dlg% = EXITSUCCESS
        ELSE 
            dlg% = EXITSUCCESS_2
        END IF
    ELSEIF ERR = STFQUIT THEN
        END
    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!", "Setup Message", MB_OK+MB_TASKMODAL+MB_ICONHAND)
    END





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



'****************************************************
'*  Given drive is a network drive
'****************************************************
NETWORK:
    sz$ = UIStartDlg(CUIDLL$, NETDRIVE, "FInfoDlgProc", 0, "")
    IF sz$="CONTINUE" THEN
        UIPop 1
        Install
        UIPopAll
        GOTO QUIT
    ELSEIF sz$ = "REACTIVATE" THEN
        GOTO NETWORK
    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



'*************************************************************************
'**
'**     INSTALLATION PART
'**
'** Purpose:
'**     Builds the copy list and copy files
'** Arguments:
'**     none.
'** Returns:
'**     none.
'*************************************************************************
SUB Install STATIC

'' Making OFFICE and TMP directories
    CreateDir DEST$, cmoNone
    tmp$ = MakePath(DEST$,"TMP")
    Createdir tmp$, cmoNone

   ''*****************************
   '' Transports' initialization 
   ''*****************************
   ''If NetBios has been allready installed, the WS_NAME is already saved
   ''else update it after calling of TOInitTransport function  
   drive$ = MID$(DEST$, 1, 1)

   trpinf$ = ""
   trpinf$ = GetIniKeyString(TOINI$, "TRANSPORT","3")

   ''Save TRSPORT.INI with path

   TRSPORT$ = GetSymbolValue("STF_SRCDIR") +  "TRSPORT.INI"
   IF IsDriveLocalHard (drive$) THEN
      GOTO LOCALDRIVE
   ELSE
      TOCopyTransport(TRSPORT$)
      GOTO TRSPORTCOPY
   ENDIF

LOCALDRIVE:
   TOInitTransport(TRSPORT$)
   trpinf2$ = ""
   wsname$ = ""
   wss% = 0
   IF trpinf$ = "" THEN
      trpinf$ = GetIniKeyString(TOINI$, "TRANSPORT","3")
   ENDIF   
   IF trpinf$ <> "" THEN 
       IF TOCOMP$ <> "" THEN
          FOR i% = 1 TO LEN(trpinf$) STEP 1       
          IF MID$(trpinf$,i%,1) = "," THEN
                ''First part is save
                wsname$ = MID$(trpinf,1,i%)
                wsname$ = wsname$ + TOCOMP$
                wss% = 1
                IF LEN(trpinf$) > i% THEN
                  trpinf2$ = MID$(trpinf,i%+1,(LEN(trpinf$)-i%))
                  wss% = 2
                  EXIT FOR
                ENDIF
          ENDIF
          NEXT i%
       ENDIF
    END IF
    IF wss% = 2  THEN 
        FOR i% = 1 TO LEN(trpinf2$) STEP 1               
           IF MID$(trpinf2,i%,1) = "," THEN              
              trpinf$ = MID$(trpinf2$,i%,LEN(trpinf2$)-(i%-1))
              wsname$ = wsname$ + trpinf$
              EXIT FOR
           END IF
        NEXT i%
    END IF
    IF wsname$ <> "" THEN
       CreateIniKeyValue TOINI$, "TRANSPORT", "3", wsname$, cmoOverwrite
    ENDIF

TRSPORTCOPY:
    i% = GetListLength("TransportFiles")
    
    IF i% > 0 THEN
       FOR j% = 1 TO i% STEP 1
           trsfile$ = GetListItem("TransportFiles",j%)
           wtrsfile$ = "TWT"+trsfile$+".DLL"
           szTrsPath$ = GetSymbolValue("STF_SRCDIR") + wtrsfile$
           tmpfile$ = MakePath(DEST$,wtrsfile$)
           CopyFile szTrsPath$,tmpfile$, cmoOverwrite,0

       NEXT j%
       RemoveSymbol "TransportFiles"
    END IF

''  Copy Files    
    SrcDir$ = GetSymbolValue("STF_SRCDIR")
''  Copy common files in Windows system directory
    ClearCopyList
    SysDir$ = GetWindowsSysDir()
    AddSectionFilesToCopyList COMFILES$, SrcDir$, SysDir$
    AddSectionFilesToCopyList TOFILES$, SrcDir$, DEST$
    CopyFilesInCopyList

''  Deletes old files,which are not anymore in use 
    tmpfile$ = MakePath(DEST$,"nwipxspx.dll")
    RemoveFile tmpfile$, cmoNone
    tmpfile$ = MakePath(DEST$,"nwnetapi.dll")
    RemoveFile tmpfile$, cmoNone


'' Change the filetime
    FileTimes
    drive$ = MID$(DEST$, 1, 1)
    IF IsDriveLocalHard (drive$) THEN
         UpdateFiles
    END IF
END SUB


'*************************************************************************
'**
'**     UPDATE THE TIME TO INSTALLED FILES PART
'**
'** Purpose:
'**     Update time 3.00 to the installed files  
'**     instead of 0.00
'**     Made in SIMPLE way    
'** Arguments:
'**     none.
'** Returns:
'**     none.
'*************************************************************************
SUB FileTimes STATIC
    
    tmpfile$ = MakePath (DEST$,"aoconn.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"ncontrol.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"wcontrol.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tdmgrmsh.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"toabt.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tocalbmp.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tocalen.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tocommon.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"toalarm.exe")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"toalarm.msh")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"totbar.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tbimg.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tobod.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"toerror.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tocal.exe")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tocal30.hlp")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"tocal30.msh")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"topassw.exe")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"todate.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"switcher.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"twsess.dll")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"readme.wri")
    SetFiletime(tmpfile$)
    tmpfile$ = MakePath (DEST$,"toconkpr.exe")
    SetFiletime(tmpfile$)


END SUB


'*************************************************************************
'**
'**     UPDATE FILES PART
'**
'** Purpose:
'**     If given drive is NOT a network drive 
'**     update needed files and make program groups
'** Arguments:
'**     none.
'** Returns:
'**     none.
'*************************************************************************

SUB UpdateFiles STATIC
   tmp$ = MakePath(DEST$,"TMP")
   CreateIniKeyValue TOINI$, "UI", "office_directory",DEST$,cmoOverwrite
   CreateIniKeyValue TOINI$, "UI", "tmp_directory",tmp$, cmoOverwrite
   CreateIniKeyValue TOINI$, "UI", "default_user",USERNAME$, cmoOverwrite

''  Custom Colors

   CreateIniKeyValue TOINI$, "Custom Colors","Cust1","255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust2","255 255 255",cmNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust3","255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust4","255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust5","255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust6","255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust7","255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust8","255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust9","255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust10", "255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust11", "255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust12", "255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust13", "255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust14", "255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust15", "255 255 255",cmoNone
   CreateIniKeyValue TOINI$, "Custom Colors","Cust16", "255 255 255",cmoNone
   
   'Making TeamOFFICE group    
   CreateProgmanGroup GROUPNAME$, "", cmoNone
   ShowProgmanGroup   GROUPNAME$, 1, cmoNone

   'Making the program item
   CreateProgmanItem GROUPNAME$, TOITEM$, MakePath(DEST$,TOEXE$) , "", cmoOverwrite
   CreateProgmanItem GROUPNAME$, TOALARM$, MakePath(DEST$,"toalarm.exe"), "", cmoOverwrite
   CreateProgmanItem GROUPNAME$, RELNOTE$, MakePath(DEST$,"README.WRI"),"", cmoOverwrite
    
   IF ALARMCHECK$ = "ON" THEN
        CreateProgmanItem STARTUP$, TOALARM$, MakePath(DEST$,"toalarm.exe"), "", cmoOverwrite
   END IF

'*************************
' Update of AUTOEXEC.BAT
'*************************    
   IF AUTOCHECK$ = "ON" THEN
       UpdateAutoexec
   END IF
'*************************************
' Update of transport_type in TOUI.INI
'*************************************    

   ''If the first installation, you have to start TOCAL.EXE
   ''for network configuration

   trpinf$ = ""
   trpinf$ = GetIniKeyString(TOINI$, "UI","transport_type")
   IF trpinf$ <> "" THEN
      GOTO INSTEND
   END IF

   SetSymbolValue "SetConfigur","1"

CONFQUIT:

   sz$ = UIStartDlg(CUIDLL$, COMPCHANGE, "FConfigDlgProc", 0, "")

   IF sz$ = "EXIT" THEN
        UIPopAll
        ERROR STFQUIT
    ELSEIF sz$ = "REACTIVATE" THEN
        GOTO CONFQUIT
    ELSEIF sz$ = "CONTINUE" THEN
        GOTO CONFIGURE
    ELSE
        UIPopALL
        ERROR STFQUIT
    ENDIF
    GOTO INSTEND

CONFIGURE:

    b$ = MakePath(DEST$, "TOCAL.EXE")  
    SetSymbolValue "StartProc",""
    StartTOProc(b$)
    b$ = GetSymbolValue("StartProc")
    IF b$ = "0" THEN
       UIPopALL 
       ERROR STFQUIT
       GOTO INSTEND
    ELSE
       UIPop 1
       RemoveSymbol "StartProc"
    ENDIF

FAILED:
sz$ = UIStartDlg(CUIDLL$, STARTFAILED, "FInfo0DlgProc", 0, "")
    IF sz$ = "REACTIVATE" THEN
        GOTO FAILED
    END IF
    UIPop 1
    GOTO CONFQUIT
    
INSTEND:    
END SUB

'*************************************************************************
'**
'**     UPDATE AUTOEXEC.BAT WHEN NEEDED
'**
'** Purpose:
'**     Update AUTOEXEC.BAT 
'** Arguments:
'**     none.
'** Returns:
'**     none.
        RestoreCursor CursorSave%
'*************************************************************************

SUB UpdateAutoexec STATIC
'********************************
'  Make temp files
'********************************
    
    CursorSave% = ShowWaitCursor()
    tmpstr$  = "REM **AO** Alfaskop Office"
    tmpstr2$ = "REM **AO** Team Office"
    tmpstr3$ = "REM **AO** Block end" 
    RIVI2$=""

    i% = LEN(DEST$)
    IF MID$(DEST$,i%,1) = "\" THEN
       i% = i% - 1
       d$ = MID$(DEST$,1,i%)
       DEST$ = d$
    ENDIF

    SetSymbolValue "DestPath",""
    SetSymbolValue "PathPath",PATHVAR$
    TOCompare(DEST$)
    d$ = GetSymbolValue("DestPath")

    IF d$ <> "" THEN
       DEST$ = d$
       d$ = GetSymbolValue("PathPath")
       PATHVAR$ = d$
    ELSE
       RemoveSymbol "DestPath"
       RemoveSymbol "PathPath"
       RestoreCursor CursorSave%
       GOTO UPDATEEND
    ENDIF

    RemoveSymbol "DestPath"
    RemoveSymbol "PathPath"

    FOR i% = 1 TO LEN(PATHVAR$) STEP 1
        path% = INSTR(PATHVAR$, DEST$)
        d$ = MID$(PATHVAR$, path% + LEN(DEST$), 1)
        IF d$ = ";" OR d$ = "" THEN
           EXIT FOR
        ENDIF 
        path% = 0
        i% = i% + LEN(DEST$)
    NEXT i%
'****************************************************
'  If given path is already in PATH-variable
'   -> no modification needed
'****************************************************
    IF (path% > 0 )  THEN
        RestoreCursor CursorSave%
        GOTO UPDATEEND
    END IF
    '**Is autoexec.bat existing?
    IsAuto% = DoesFileExist("c:\autoexec.bat",femExists)
    IF (IsAuto% = 0) THEN
        RestoreCursor CursorSave%
        GOTO UPDATEEND
    END IF
    Length& = GetSizeOfFile("c:\autoexec.bat")
'*************************************************
'** If the size of autoexec.bat = zero,
'** AO block direct to right autoexec.bat file
'*************************************************
    l% = 0    
    IF (Length& = 0) THEN
        OPEN "c:\autoexec.bat" FOR OUTPUT AS 1
        PRINT #1,tmpstr2$+"; Do not modify this block."
        l%=LEN(PATHVAR$)+ 1 + LEN(DEST$)
        IF l% < 128  THEN        
           PRINT #1, "SET PATH=%PATH%;"+DEST$
        END IF
        PRINT #1, "REM **AO** Block end"
        CLOSE #1
        GOTO TOCURSOR
    END IF      
'***********

    tmpfile$ = GetSymbolValue("STF_CWDDIR") + "tmp.tmp"
    tmpfile2$ = GetSymbolValue("STF_CWDDIR") + "tmp2.tmp"
    CopyFile "c:\autoexec.bat",tmpfile$,cmoNone, 0
    OPEN tmpfile$ FOR INPUT AS 1
    OPEN tmpfile2$ FOR OUTPUT AS 2
    aoblock%=0
    LINE INPUT  #1,RIVI$ 

'*******************************************
'  Read lines before possible AO block and
'  write them to tmp
'*******************************************
    WHILE (NOT EOF(1)) AND (aoblock%=0) 
       IF (INSTR(RIVI$,tmpstr$)) OR (INSTR(RIVI$, tmpstr2$)) THEN
           aoblock%=1
       ELSEIF INSTR(RIVI$,"SET TOCOMPUTER") THEN
                LINE INPUT #1, RIVI$
       ELSE
           PRINT #2,RIVI$
           LINE INPUT  #1,RIVI$ 
       END IF
    WEND

    IF EOF (1) THEN
         PRINT #2, RIVI$
    END IF

'*********************************
' Skip rest of AO block if found
'*******************************
    WHILE (aoblock%=1) AND (NOT EOF(1))
       IF INSTR(RIVI$,tmpstr3$) THEN
           aoblock%=0
       ELSE
           LINE INPUT #1, RIVI$
           IF INSTR(RIVI$,"PATH") THEN
               RIVI2$=RIVI$
           END IF
       END IF
    WEND

'***************************
'  WRITE AO block
'***************************
    PRINT #2,tmpstr2$+"; Do not modify this block."
    l%=LEN(PATHVAR$)+ 1 + LEN(DEST$)
    IF l% < 128  THEN        
        PRINT #2, "SET PATH=%PATH%;"+DEST$
    ELSEIF RIVI2$ <> ""  THEN
        PRINT #2, RIVI2$
    END IF
    PRINT #2, "REM **AO** Block end"
   
'************************
'  Rest of AUTOEXEC.BAT
'************************
    WHILE NOT EOF(1)
       LINE INPUT #1, RIVI$
       IF INSTR(RIVI$,"SET TOCOMPUTER") THEN
          LINE INPUT #1, RIVI$
       ELSE
          PRINT #2, RIVI$
       ENDIF
    WEND
    CLOSE #1
    CLOSE #2
    CopyFile tmpfile2$, "c:\autoexec.bat", cmoOverwrite,0
    RemoveFile tmpfile$,cmoForce
    RemoveFile tmpfile2$,cmoForce

TOCURSOR:
 
   ''TOINI$ = ""
   RestoreCursor CursorSave%

TOOLONG:
    IF l% > 127 THEN
        sz$ = UIStartDlg(CUIDLL$, TOOLONGPATH, "FInfoDlgProc", 0, "")
        IF sz$ = "REACTIVATE" THEN
           GOTO TOOLONG
        END IF
        UIPop 1
        AUTOUPDATED$ = "NO"
    ELSE 
        AUTOUPDATED$ = "YES"
    END IF    
UPDATEEND:

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


