/*
** Module   :INSTALL.CMD
** Abstract :
**
** Copyright (C) 1999 Link Guard Solutions Ltd.
**
** Log: Tue  21/12/1999 Created
**      Thu  20/01/2000 Updated
**      Mon  12/06/2000 Updated
**      Wed  04/10/2000 Updated
**      Mon  21/05/2001 Updated
*/

'@echo off'

address CMD

rc=RxFuncAdd('BootDrive', 'bootdrv', 'BootDrive')
rc=RxFuncAdd('FullPath', 'bootdrv', 'FullPath')
rc=RxFuncAdd('SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs')
call SysLoadFuncs

signal on halt name bad_inst;

if RxFuncQuery('SysTempFileName') then
do
    say 'Unable to load REXXUTIL.DLL. Aborting installation'
    exit
end

if RxFuncQuery('BootDrive') then
do
    say 'Unable to load BOOTDRV.DLL. Aborting installation'
    exit
end

lgspath=BootDrive()':\SafeFire'
cmd=BootDrive()':\OS2\CMD.EXE /C'
replace_config='N'
continue='N'
wps='Y'

say ''
say 'SafeFire PPP/Links Install, Copyright (C) 1999 - 2001  Link Guard Solutions Ltd.'
say ''
say 'This program will install SafeFire PPP/Links to your PC.'

do while continue<>'Y'
    call ask_inst_dir
    call ask_mk_dir
    call ask_config
    call ask_wps

    say ''
    say 'þþþ Installation directory þþþ>'lgspath
    say 'þþþ Update CONFIG.SYS      þþþ>'replace_config
    say 'þþþ Create program objects þþþ>'wps
    say ''

    say 'Are these parameters correct?'
    say 'Enter '"'Y'" 'followed by Enter key to begin installation.'
    say 'Press Ctrl+Break to abort installation.'

    parse upper pull answer

    if answer='Y' then continue='Y'
end

/* Extract files */

if fileexist(lgspath, 'D') <> 1 then
do
    say 'Installation directory does no exists. Aborting'
    exit
end

if fileexist('PACKAGE.EXE', 'F') <> 1 then
do
    say 'Missing software package. Aborting'
    exit
end

parse value lgspath with drive':'path

say lgspath

'@'cmd' dir /f PACKAGE.EXE | rxqueue'

parse pull package

'@'drive':'
'cd "'path'"'

'cd'

'@'cmd package '-d -o'

if wps = 'Y' & fileexist('objects.lst', 'F') = 1 then
do
    call mkobjects 'objects.lst'
end

/* prepare CONFIG.SYS */

/* save a copy of CONFIG.SYS */

config=BootDrive()':\CONFIG.SYS'

if fileexist(config, 'F') = 1 then
do
    'copy 'config '.\CONFIG.ORG'

    if fileexist('CONFIG.ORG', 'F') <> 1 then
    do
        say 'Unable to save a copy of CONFIG.SYS.'
        say 'CONFIG.SYS will not be updated.'
    end
    else do
        fin='CONFIG.ORG'
        fout='CONFIG.SYS'
        path_ready=0

        call SysFileDelete fout

        do while lines(fin) > 0

            str=linein(fin)
            str2=strip(str)

            if left(translate(str2),7) = 'LIBPATH' then
            do
                if right(translate(str2),1) <> ';' then
                    str=str';'

                if pos(translate(lgspath'\bin'), str) > 0 then
                    iterate

                str=str||lgspath'\bin;'
            end

            parse upper value str with a_set a_var

            if a_set = 'SET' & left(a_var,6) = 'LGSDIR' then
            do
                path_ready=1
                str='SET LGSDIR='lgspath
            end

            call lineout fout, str
        end

        if path_ready=0 then
        do
            call lineout fout, 'SET LGSDIR='lgspath
        end

        call stream fin , 'c', 'close'
        call stream fout, 'c', 'close'

        if replace_config = 'Y' then
        do
            'copy 'fout config

            say ''
            say 'Original CONFIG.SYS is saved as 'lgspath'\CONFIG.ORG'
            say ''
        end
    end
end


exit

/*----------------------------------------------------------------------*/
/* Utility routines                                                     */
/*----------------------------------------------------------------------*/

ask_inst_dir:

    say ''
    say 'Please, enter directory where SafeFire PPP/Links should be installed'
    say 'or press Enter for installation to 'lgspath

    parse pull answer

    if answer<>'' then
    do
        lgspath=FullPath(answer)
        say 'Installation directory: 'lgspath
    end

return

ask_config:

    say ''
    say 'Installer will to make changes in CONFIG.SYS.'
    say 'These changes are adding of LGSDIR environment variable'
    say 'and adding of path where SafeFire PPP will be installed'
    say 'to LIBPATH statement.'
    say ''
    say 'Press '"'Y'"' to allow Installer to make changes'
    say 'Press '"'N'"' and changed CONFIG.SYS will be placed in 'lgspath

    pull answer

    if answer='Y' then replace_config='Y';

return

ask_wps:

    say ''
    say 'Create program objects on the desktop ? [Y/N]'
    say ''
    pull answer

    if answer<>'Y' then wps='N' else wps ='Y';

return


ask_mk_dir:

    if fileexist(lgspath, 'D') <> 1 then
    do
        say ''
        say 'Directory 'lgspath' does no exists.'
        say 'Create this directory ? [Y/N]'

        pull answer

        if answer='Y' then
        do
            rc=SysMkDir(lgspath)
        end
    end

return

fileexist: procedure
    call SysFileTree ARG(1), stem, ARG(2)
return stem.0;

bad_inst:
    say 'Installation aborter. Exiting'
    exit
return

mkobjects:
    fin=arg(1)
    attr.l='.LONGNAME'
    attr.t='.TYPE'

    say 'Creating WPS objects...'

    do while lines(fin) > 0
        str=linein(fin)

        if left(str,1)<>';' & strip(str)<>'' then
        do
            do while pos('%1',str) > 0
                parse value str with beg'%1'rest
                str=beg||lgspath||rest
            end

            parse value str with action'='rest
            /*
            say 'Performing' str
            */
            if action='C' then
            do
                parse value rest with class '|' title '|' location '|' setup '|' option
                call SysCreateObject class, title, location, setup, option
            end
            else if action='L' | action='T' then
            do
                parse value rest with fname '|' value
                call SysPutEa fname, attr.action, 'FDFF'x || d2c(length(value)) || '00'x || value
            end
            else if action='D' then
            do
                parse value rest with oid '|' data
                call SysSetObjectData oid, data
            end
            else if action='S' then
                call SysSaveObject rest, 0
        end
    end

    call stream fin, 'c', 'close'

    say 'Done'

return

