/*---------------------------------------------------------------------------*\
|   GET all ALIAS from a LAN Server 3.0 and higher                            |
|   and dump it to an ASCII File                                              |
|                                   (C) Alain Rykaert IBM Belgium SEP95-MAY96 |
\*---------------------------------------------------------------------------*/

    Parse Arg Option

    Call INIT                    /* Initialisation of DLL's and other stuff*/
    Call CHKOPT                          /* Check Options & display Welcome*/
    Call CHKPWS                               /* Check the PWS & Admin name*/
    Call COLUMNS                        /* Read the Columns definition file*/
    Call MAIN                                            /* do the main job*/
    Call QUIT                                                       /* Quit*/

 MAIN:/* ------------------------------------------------------ MAIN: -------*/

    Call Time('R')
   'if exist' OUTF 'del' OUTF

    Type = 1
    Line = 0

    do while Type < 5
       if Type = 1 then TypeName = 'File  '
       if Type = 2 then TypeName = 'Print '
       if Type = 4 then TypeName = 'Serial'

       Line = Line + 1
       RC = NetEnumerate(20, 'AliasName', '\\'SRVNAME, Type)
       if RC = 0
        then do
                Call RxStemSort 'AliasName'
                Call LineOut OUTF, BANNER
                do i = 1 to AliasName.0
                if i // MAXLINES = 0
                 then Call LineOut OUTF, BANNER
                 else Nop
                if \MUTE
                 then do
                         Call SysCurState OFF
                         Call SysCurPos 20 + Line,0
                      end
                 else Nop

                say '0909'x ESC'[K Type =' TypeName 'Total Alias =' i'/'AliasName.0 ALIASNAME.i

                RC = NetGetInfo(20, 'ALIASINFO', '\\'SRVNAME, AliasName.i)
                if RC = 0
                 then Call WRITEIT
                 else Call LOGIT 'NetGetInfo Alias', AliasInfo.i, RC
            end
         end
        else do
                if Substr(RC,1,3) = '234 '
                 then do
                         if \MUTE
                          then do
                                  Call SysCurState OFF
                                  Call SysCurPos 19 + Line,0
                               end
                          else Nop
                          say '0909'x ' Type =' TypeName 'Total Alias =' 0
                      end
                 else do
                         Call LOGIT 'Get' TypeName 'Alias',SRVNAME,RC
                      end
             end
      Call LineOut OUTF, ' '
      Type = Type * 2
    end

    if \MUTE then say '0909'x ' Total Time  =' Trunc(Time('E'),2)

    Call Stream OUTF, 'C', 'CLOSE'

    Call SysSetObjectData OUTF, 'ICONFILE='Directory()'\Alias.ico'

    Return

 WRITEIT:/* ---------------------------------------------------- WRITEIT: -----*/

    ALIASINFO.OPT = Left('',COLL.1,' ')           /* Column 1 must be BLANK*/

    OUT = ''
    do j = 1 to COLT
       COLNAME = COLN.j
       DATA.j = Left(ALIASINFO.COLNAME, COLL.j, ' ')
       OUT = OUT || DATA.j || ';'
    end
    Call LineOut OUTF, OUT
    Call Stream  OUTF, 'C', 'CLOSE'

    Return

 COLUMNS: /* --------------------------------------------------- COLUMNS: -*/

    BANNER = ''
    i = 0
    do while Lines(COLF)
       LLINE = LineIn(COLF)
       if Left(LLINE, 1) = '*',
        | Strip(LLINE) = ''
        then iterate
        else Nop
       i = i + 1
       parse value LLINE with COLN ';' COLL
       COLN.i = Strip(COLN)
       COLL.i = Strip(COLL)
       BANNER = BANNER || Left(COLN.i, COLL.i, ' ') || ';'
    end
    COLT = i

    Return

 CHKOPT:/* ---------------------------------------------------- CHKOPT: -----*/

    SRVNAME = '';
    OUTF = 'ALIAS.CSV';
    LOGF = 'LSMT.LOG';
    PIPE = '';
    TRACE = 0;
    MUTE = 0;

    OPTION = Translate(OPTION)
    do while OPTION <> ''
       Parse value OPTION with ARGUMENT ' ' OPTION
       select
        when Left(ARGUMENT,5) = '/SRV:'  then SRVNAME = Substr(ARGUMENT,6)
        when Left(ARGUMENT,5) = '/OUT:'  then OUTF    = Substr(ARGUMENT,6)
        when Left(ARGUMENT,5) = '/LOG:'  then LOGF    = Substr(ARGUMENT,6)
        when Left(ARGUMENT,5) = '/PIP:'  then PIPE    = Substr(ARGUMENT,6)
        when Left(ARGUMENT,2) = '/M'     then MUTE    = 1
        when Left(ARGUMENT,2) = '/T'     then TRACE   = 1
        otherwise Nop
       end
    end

    if SRVNAME = '' then signal GETHELP

    if \MUTE
     then do
             Topic1='GETWELCOME'
             Topic_String.Topic1.1=SRVNAME;
             Topic_String.Topic1.2=OUTF;
             Topic_String.Topic1.3=LOGF;
             Topic_String.Topic1.4=PIPE' ';
             Topic_List='WELCOMELOGO' Topic1 'GETALIAS';
             Call GETANS
             Parse VALUE SysCurPos() With Old_R Old_C; '@Pause';
             Call SysCurPos Old_R, Old_C; say ESC'[K';
          end
     else do
             say 'ServerName =' SRVNAME
             say 'OutputFile =' OUTF
             say 'LogFile    =' LOGF
          end
    Return

 CHKPWS:/* ---------------------------------------------------- CHKPWS: -----*/

    RC = NetGetInfo(350, 'WKSTAINFO','')
    if RC = 0
     then do
             ADMNAME = WKSTAINFO.UserName
             PWSNAME = WKSTAINFO.ComputerName
          end
     else do
            Call LOGIT 'Get PWS Info', ,RC
            Call Quit
          end
    Return

 INIT:/* ------------------------------------------------------ INIT: -------*/

    Call RgUtil   '/m'                                    /* Rexx Utilities*/
    Call RgUtils  '/m'                                    /* Rexx Utilities*/
    Call RgNPipes '/m'                                       /* Named Pipes*/
    Call RgLSRXUT '/m'                             /* Lan Server Rexx Utils*/

    Parse Upper Source . . P_NAME
    PRGN = Filespec('N', Left(P_NAME, Length(P_NAME) -4))

   '@echo off'
    ESC     = '1B'x
    REDIR   = '>NUL 2>NUL'
    MAXLINES = 999             /* Number of Lines to separate with a header*/

    COLF    = 'ALIAS.INI'                        /* Column description file*/
    Call CHKFILE COLF

    Resource_File = 'LSMT.RSC'
    Call CHKFILE Resource_File

    Return

 GETANS:/* ---------------------------------------------------- GETANS: -----*/

    Vars_List =Ansi_Say(Resource_File Topic_List);
    Parse VALUE SysCurPos() With Old_R Old_C;
    Do While Vars_List <> '';
       Parse VALUE Vars_List With Topic_Id ';' Var_Id ';' Row ';' Column ';' Color ';' Vars_List;
       Call SysCurPos Row, Column;
       Say x2c(Color) || Topic_String.Topic_Id.Var_Id || '1B'x || '[0m';
       End;
    Call SysCurPos Old_R, Old_C;

    Return

 GETHELP:/* --------------------------------------------------- GETHELP: ----*/

    if \MUTE
     then do
             Topic1='GETHELP'
             Topic_String.Topic1.1=PRGN;
             Topic_List=Topic1;
             Call GETANS
          end
     else say 'Incorrect options.'

    Call QUIT

    Return

 CHKFILE:/* --------------------------------------------------- CHKFILE: ----*/

    Parse Arg FILE
    RC = Stream(FILE, 'C', 'QUERY EXIST')
    if RC = ''
     then do
             say ' File' FILE 'not found.'
             Call QUIT
          end
     else Nop
    Call Stream FILE, 'C', 'CLOSE'

    Return

 LOGIT:/* ----------------------------------------------------- LOGIT: ------*/

    FUNC = ARG(1); INFO = ARG(2); RCOD = ARG(3)
    RC = LLOGIT(LOGF, PIPE, ADMNAME, PRGN, FUNC, INFO, RCOD)

    Return

 QUIT:/* ------------------------------------------------------ QUIT: -------*/

    Call LineOut 'LSMT.END', PRGN, 1
    Call Stream  'LSMT.END', 'C', 'CLOSE'
    Call Stream   COLF,      'C', 'CLOSE'
    Call Stream   LOGF,      'C', 'CLOSE'
    Call Stream   OUTF,      'C', 'CLOSE'

    Exit

 /*--------------------------------------------------------------------------*/
