/*--------------------------------------------------------------------------
   Copyright 1994, 1996, 2002 Timothy Rue
   Email: 3seas@threeseas.net
   http://threeseas.net
--------------------------------------------------------------------------


The project name is "Virtual Interaction Configuration" (VIC for short):

    What you'll need:

    Shell - (at least at this time) This project does require the shell
        interface. I recommended that you also make use of csh (found on
        aminet) for your shell interface. We may make use of csh
        functionality to accomplish some things.

    AREXX - At least at this time (to run the VIC).

    Text editor - Any text editor you want. What this will allow you to do
        is play around with the code and perhaps contribute to it's
        evolution. Perhaps writting AREXX code is not for you but
        writing/editing documentation interest you.

    Getting this going: The following is a beginning and there are of
course things I'd like to have done before posting.


************************************************************************/

/* PK file array */

/* Setting the standard variables to defaults*/

vic. = PRAGMA(ID)

vic.1   = 'AI'
vic.1.1 = 'AI-name.#'
vic.1.2 = 'PK-file directory'
vic.1.3 = 'Current Directory'

vic.2   = 'PK'
vic.2.1 = '' /*'PK-filename'*/
vic.2.2 = '' /*'last/alt. PK-filename'*/
vic.2.3 = 'default.pk' /*'opt. default PK-filename'*/

vic.3   = 'OI'
vic.3.1 = 'OI-filename'
vic.3.2 = 'last/alt. OI-filename'
vic.3.3 = 'opt. default OI-filename'

vic.4   = 'IP'
vic.4.1 = 'device'
vic.4.2 = 'preprocess,Class'
vic.4.3 = 'BOI,EOI'
vic.4.4 = 'opt. last/alt. set'

vic.5   = 'OP'
vic.5.1 = 'device'
vic.5.2 = 'postprocess,Class'
vic.5.3 = 'BOO,EOO'
vic.5.4 = 'opt. last/alt. set'

vic.6   = 'SF'
vic.6.1 = 'SF-LPC flags'
vic.6.2 = 'last/alt. flags'
vic.6.3 = 'SF-fname@line#'
vic.6.4 = 'opt. last/alt. set'
vic.6.5 = ': SF-filename@line#,....>running stack>'

vic.7   = 'IQ'
vic.7.1 = 'IQ flags'
vic.7.2 = 'last/alt. flags'
vic.7.3 = 'IQ-fname@line#'
vic.7.4 = 'opt. last/alt. set'
vic.7.5 = ': IQ-filename@line#,....>running stack>'

vic.8   = 'ID'
vic.8.1 = 'ID flags'
vic.8.2 = 'last/alt. flags'
vic.8.3 = 'ID-fname@line#'
vic.8.4 = 'opt. last/alt. set'
vic.8.5 = ': ID-filename@line#,....>running stack>'

vic.9   = 'KE'
vic.9.1 = 'Master-teeth'
vic.9.2 = 'last/alt. M-teeth'
vic.9.3 = 'KE-fname'
vic.9.4 = 'opt. last/alt. set'

/* Test!!!
SAY vic.1":" vic.1.1";"vic.1.2";"vic.1.3
SAY vic.2":" vic.2.1";"vic.2.2";"vic.2.3
SAY vic.3":" vic.3.1";"vic.3.2";"vic.3.3
SAY vic.4":" vic.4.1";"vic.4.2";"vic.4.3
SAY vic.5":" vic.5.1";"vic.5.2";"vic.5.3
SAY vic.6":" vic.6.1";"vic.6.2";"vic.6.3
SAY "  "vic.6.5
SAY vic.7":" vic.7.1";"vic.7.2";"vic.7.3
SAY "  "vic.7.5
SAY vic.8":" vic.8.1";"vic.8.2";"vic.8.3
SAY "  "vic.8.5
SAY vic.9":" vic.9.1";"vic.9.2";"vic.9.3
*/

/*==================================*/
/*Reading in the PK file.
  If no filename is present then read default.
  if default does not exist, warn and create*/

PKfilename = 0

/*check internal variables*/

IF vic.2.1 > '' THEN PKfilename = vic.2.1

    ELSE DO

    IF VIC.2.2 > '' THEN PKfilename = vic.2.2

        END


/* If above fails try "default.pk" */

IF PKfilename = 0 THEN DO

    IF EXISTS(vic.2.3) = 0 THEN DO

    SAY 'WARNING: default PK file ('vic.2.3') not available. Creating It'

    /* create/write PK file from internal default*/

        END

    ELSE PKfilename = vic.2.3

    END


IF PKfilename ~= 0 THEN DO

    IF OPEN('PK_file',PKfilename,'R') ~= 0 THEN DO

    /* read the result file if available */

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.1 ':' vic.1.1 ';' vic.1.2 ';' vic.1.3

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.2 ':' vic.2.1 ';' vic.2.2 ';' vic.2.3

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.3 ':' vic.3.1 ';' vic.3.2 ';' vic.3.3

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.4 ':' vic.4.1 ';' vic.4.2 ';' vic.4.3

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.5 ':' vic.5.1 ';' vic.5.2 ';' vic.5.3

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.6 ':' vic.6.1 ';' vic.6.2 ';' vic.6.3
    vic.6.5 = READLN('PK_file')

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.7 ':' vic.7.1 ';' vic.7.2 ';' vic.7.3
    vic.7.5= READLN('PK_file')

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.8 ':' vic.8.1 ';' vic.8.2 ';' vic.8.3
    vic.8.5 = READLN('PK_file')

    current_line = READLN('PK_file')
    PARSE VAR current_line vic.9 ':' vic.9.1 ';' vic.9.2 ';' vic.9.3

/* this is how we will keep track of sf,iq,id line numbers ???*/

    SAY SEEK('PK_file',0,'C')


        END


    ELSE DO

        SAY  "variable_pkfile not available"

        END

    END


/*=====================*/
/* Writing the PK file */

SAY OPEN('PK-file-out','*','W')

WRITELN('PK-file-out',vic.1":"vic.1.1";"vic.1.2";"vic.1.3)
WRITELN('PK-file-out',vic.2":"vic.2.1";"vic.2.2";"vic.2.3)
WRITELN('PK-file-out',vic.3":"vic.3.1";"vic.3.2";"vic.3.3)
WRITELN('PK-file-out',vic.4":"vic.4.1";"vic.4.2";"vic.4.3)
WRITELN('PK-file-out',vic.5":"vic.5.1";"vic.5.2";"vic.5.3)
WRITELN('PK-file-out',vic.6":"vic.6.1";"vic.6.2";"vic.6.3)
WRITELN('PK-file-out',vic.6.5)
WRITELN('PK-file-out',vic.7":"vic.7.1";"vic.7.2";"vic.7.3)
WRITELN('PK-file-out',vic.7.5)
WRITELN('PK-file-out',vic.8":"vic.8.1";"vic.8.2";"vic.8.3)
WRITELN('PK-file-out',vic.8.5)
WRITELN('PK-file-out',vic.9":"vic.9.1";"vic.9.2";"vic.9.3)

CLOSE('PK-file-out')

/*====================================*/
/* passing lines not used by the vic */

/*vic.5.1 = 'rexx_csh'

line_to_pass =   dir /*'echo "line to_pass via OP setting"'*/

ADDRESS VALUE vic.5.1 ; line_to_pass*/

echo vic.

echo "line to_pass via OP setting  vic"

/*============================================*/
DO FOREVER
OPTIONS PROMPT "INPUT PLEASE:"
PULL input_command user_instruction change2make

SELECT

    WHEN input_command = AI THEN SELECT

        WHEN user_instruction = r1 THEN SAY vic.1.1
        WHEN user_instruction = r2 THEN SAY vic.1.2
        WHEN user_instruction = r3 THEN SAY vic.1.3

        WHEN user_instruction = c1 THEN vic.1.1=change2make
        WHEN user_instruction = c2 THEN vic.1.2=change2make
        WHEN user_instruction = c3 THEN vic.1.3=change2make


/* AI Sets up the PK file and makes available the internal commands.
   May be used with any of the following options*/

     /*AI or -n name*/
        WHEN user_instruction = '' THEN SAY "startup of AI w/defaults"

        WHEN user_instruction = '-'n THEN SAY "startup w/AI file"

     /*AI -pk filename or filename.pk*/
        WHEN user_instruction = '-'pk THEN vic.2.1=change2make

        WHEN user_instruction = .pk THEN SAY "startup w/.pk file"

     /*AI -oi filename or filename.oi */
        WHEN user_instruction = '-'oi THEN vic.3.1=change2make

        WHEN user_instruction = .oi THEN SAY "startup w/.OI file"

     /*AI -ip (set options) or -i (set options) */

        WHEN user_instruction = '-'ip THEN vic.4.1=changes2make

        WHEN user_instruction = '-'i THEN SAY "startup w/i (ip) settings"

     /*AI -op (set options) or -o (set options) */
        WHEN user_instruction = '-'op THEN SAY "startup w/op settings"

        WHEN user_instruction = '-'o THEN SAY "startup w/i (op) settings"

     /*AI -sf filename@line# or filename.sf@line# */
        WHEN user_instruction = '-'sf THEN SAY "startup w/sf file@line#"

        WHEN user_instruction = .sf THEN SAY "startup w/.sf file@line#"

     /*AI -iq filename or filename.iq */
        WHEN user_instruction = '-'iq THEN SAY "startup w/iq file"

        WHEN user_instruction = .iq THEN SAY "startup w/.iq file"

     /*AI -id filename or filename.id */
        WHEN user_instruction = '-'id THEN SAY "startup w/id file"

        WHEN user_instruction = .id THEN SAY "startup w/.id file"

     /*AI -ke filename or filename.ke */
        WHEN user_instruction = '-'ke THEN SAY "startup w/ke file"

        WHEN user_instruction = .ke THEN SAY "startup w/.ke file"

     /* AI -? or AI -h */
        WHEN user_instruction = '-'? THEN SAY "AI help"

        WHEN user_instruction = '-'h THEN SAY "AI HELP"

     /*AI -d  or -w debug/watch toggle */
        WHEN user_instruction = '-'d THEN SAY "startup w/debug"

        WHEN user_instruction = '-'w THEN SAY "startup w/watch"

     /*AI exit or -x AI configuration */
        WHEN user_instruction = exit THEN SAY "exit AI configuration"

        WHEN user_instruction = '-'e THEN SAY "Exit AI configuration"


/* Example: AI -pk DF1:Testfile script.sf -ke Holder */

        OTHERWISE SAY "AI instruction not valid!!"
        END



    WHEN input_command = PK THEN SELECT
        WHEN user_instruction = r1 THEN SAY vic.2.1
        WHEN user_instruction = r2 THEN SAY vic.2.2
        WHEN user_instruction = r3 THEN SAY vic.2.3

        WHEN user_instruction = c1 THEN vic.2.1=change2make
        WHEN user_instruction = c2 THEN vic.2.2=change2make
        WHEN user_instruction = c3 THEN vic.2.3=change2make


/*PK*/

    /* PK returns PK pk line*/
        WHEN user_instruction = '' THEN SAY vic.2.1 vic.2.2 vic.2.3


    /* PK -? help on PK*/

        WHEN user_instruction = '-'? THEN SAY PK help

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

     /*PK pk toggle positions 1 & 2*/

        WHEN user_instruction = pk THEN SAY vic.2.2 vic.2.1 vic.2.3

     /*PK pk(filename)             make pk file current*/

        WHEN user_instruction = pk.file THEN SAY pk.file vic.2.1 vic.2.3

     /*PK -c pk                    cancel current pk file*/

        WHEN user_instruction = '-'c.pk THEN SAY vic.2.2 ',' ',' vic.2.3

     /*PK -c pk(filename)          replace current with*/

        WHEN user_instruction = '-'c.pk.file THEN SAY c.pk.file vic.2.2 vic.2.3

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

     /*PK oi                       toggle positions 1 & 2*/

     /*PK oi(filename)             make oi file current*/

     /*PK -c oi                    cancel current io file*/

     /*PK -c oi(filename)          replace current with*/

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

     /*PK ip                       toggle positions 1 & 2*/

     /*PK ip(set-options)          make ip set current*/

     /*PK -c ip                    cancel current ip set*/

     /*PK -c ip(set-options)       replace current with*/

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

     /*PK op                       toggle position 1 & 2*/

     /*PK op(set-options)          make op set current*/

     /*PK -c op                    cancel current op set*/

     /*PK -c op(set-options)       replace current with*/

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

/*     PK sf                       swaps current sf filename@line# with
                                 previous sf filename@line#

     PK sf filename@line#...     add filename@line# to top of stack

     PK -c sf                    cancels PK SF line

     PK -c sf filename@line#...  changes PK SF line to
                                 *note this can be rather destructive
                                 to script execution and OI var lists

---------------------------------------------------------------------

     PK iq                       swaps current iq filename@line# with
                                 previous iq filename@line#

     PK iq filename@line#...     make IQ file current or put on top
                                 of stack

     PK -c iq                    cancels PK IQ line

     PK -c iq filename@line#...  changes PK IQ line to

---------------------------------------------------------------------

     PK id                       swaps current id filename@line# with
                                 previous id filename@line#

     PK id filename@line#...     make ID file current or put on top
                                 of stack

     PK -c id                    cancels PK ID line

     PK -c id filename@line#...  changes PK ID line to

---------------------------------------------------------------------

     PK ke                       toggles positions 1 & 2

     PK ke filename              make ke file current keychain

     PK -c ke                    cancel current keychain file

     PK -c ke filename           replace current keychain file with
*/
        OTHERWISE SAY PK help
        END


/*OI*/


    WHEN input_command = OI THEN SELECT
        WHEN user_instruction = r1 THEN SAY vic.3.1
        WHEN user_instruction = r2 THEN SAY vic.3.2
        WHEN user_instruction = r3 THEN SAY vic.3.3

        WHEN user_instruction = c1 THEN vic.3.1=change2make
        WHEN user_instruction = c2 THEN vic.3.2=change2make
        WHEN user_instruction = c3 THEN vic.3.3=change2make

        OTHERWISE SAY OI help
        END

/*
OI

     OI                          returns PK OI line

     OI -?                       help on OI

     OI -oi                      returns current list of vars & values

     OI variable                 gets input from user into variable
                                 value using IP set. if input is
                                 greater than 255 a file is created
                                 and input is to file and value is
                                 set to filename O#I.var where # is
                                 unique (perhaps filedate/time/AI#)

     OI -c var                   cancels variable

     OI -c var value             creates/changes var value

*/

    WHEN input_command = IP THEN SELECT
        WHEN user_instruction = r1 THEN SAY vic.4.1
        WHEN user_instruction = r2 THEN SAY vic.4.2
        WHEN user_instruction = r3 THEN SAY vic.4.3

        WHEN user_instruction = c1 THEN vic.4.1=change2make
        WHEN user_instruction = c2 THEN vic.4.2=change2make
        WHEN user_instruction = c3 THEN vic.4.3=change2make

        OTHERWISE SAY IP help
        END

/*
IP            only affects OI

     IP                          returns PK IP line

     IP -?                       help on IP

*/

    WHEN input_command = OP THEN SELECT
        WHEN user_instruction = r1 THEN SAY vic.5.1
        WHEN user_instruction = r2 THEN SAY vic.5.2
        WHEN user_instruction = r3 THEN SAY vic.5.3

        WHEN user_instruction = c1 THEN vic.5.1=change2make
        WHEN user_instruction = c2 THEN vic.5.2=change2make
        WHEN user_instruction = c3 THEN vic.5.3=change2make

        OTHERWISE SAY OP help
        END
/*
OP            only affects ID, IQ

     OP                          returns PK OP line

     OP -?                       help on OP

*/
    WHEN input_command = SF THEN SELECT
        WHEN user_instruction = r1 THEN SAY vic.6.1
        WHEN user_instruction = r2 THEN SAY vic.6.2
        WHEN user_instruction = r3 THEN SAY vic.6.3

        WHEN user_instruction = c1 THEN vic.6.1=change2make
        WHEN user_instruction = c2 THEN vic.6.2=change2make
        WHEN user_instruction = c3 THEN vic.6.3=change2make

        OTHERWISE SAY SF help
        END

/*
SF

     SF                          returns PK SF line

     SF -?                       help on SF

     SF -sf                      returns SF line contents

     SF filename@line#...        returns given filename@line# contents

*/

    WHEN input_command = IQ THEN SELECT
        WHEN user_instruction = r1 THEN SAY vic.7.1
        WHEN user_instruction = r2 THEN SAY vic.7.2
        WHEN user_instruction = r3 THEN SAY vic.7.3

        WHEN user_instruction = c1 THEN vic.7.1=change2make
        WHEN user_instruction = c2 THEN vic.7.2=change2make
        WHEN user_instruction = c3 THEN vic.7.3=change2make

        OTHERWISE SAY IQ help
        END

/*
IQ

     IQ                          returns PK IQ line.

     IQ -?                       help on IQ

     IQ -iq                      returns IQ search path based on
                                 KEychain.

     IQ -iq word                 returns IQ search path based on
                                 KEychain and word

     IQ word                     Searches for word using KEychain
                                 access and executes line_contents
                                 commands based on KEychain teeth
                                 and OP set.

     IQ -c word                  Cancels word/line_contents from
                                 file(s) using KEychain access.


     IQ -c word line_contents    Creates or Changes word/line_contents
                                 using KE keychain access for search.
                                 If Creating it will add to the end of
                                 path - end of path is end if first
                                 file.

*/

    WHEN input_command = ID THEN SELECT
        WHEN user_instruction = r1 THEN SAY vic.8.1
        WHEN user_instruction = r2 THEN SAY vic.8.2
        WHEN user_instruction = r3 THEN SAY vic.8.3

        WHEN user_instruction = c1 THEN vic.8.1=change2make
        WHEN user_instruction = c2 THEN vic.8.2=change2make
        WHEN user_instruction = c3 THEN vic.8.3=change2make

        OTHERWISE SAY ID help
        END

/*
ID
          Test word for being a var, var-contents, alias, alias
          contents, IQ word, directory, file. If file will try to
          determine type of file. executes commands based upon
          findings (wordf).
          Tests what the word is in relationship to the related AI
          system.

     ID                          returns PK ID line.

     ID -?                       help on ID

     ID -id                      returns ID search path based on
                                 KEychain.

     ID -id word                 returns ID search path based on
                                 KEychain and wordf

     ID word                     Searches for wordf using KEychain
                                 access and executes line_contents
                                 commands based on KEychain teeth
                                 and OP set.

     ID -c wordf                 Cancels word/line_contents from
                                 file(s) using KEychain access.


     ID -c wordf line_contents   Creates or Changes word/line_contents
                                 using KE keychain access for search.
                                 If Creating it will add to the end of
                                 path - end of path is end if first
                                 file.
*/



    WHEN input_command = KE THEN SELECT
        WHEN user_instruction = r1 THEN SAY vic.9.1
        WHEN user_instruction = r2 THEN SAY vic.9.2
        WHEN user_instruction = r3 THEN SAY vic.9.3

        WHEN user_instruction = c1 THEN vic.9.1=change2make
        WHEN user_instruction = c2 THEN vic.9.2=change2make
        WHEN user_instruction = c3 THEN vic.9.3=change2make

        OTHERWISE SAY KE help
        END

/*
KE
               To be used by IQ and ID mainly.

     KE                          returns PK KE line.

     KE -?                       help on KE.

     KE -ke                      returns current keychain list w/values.

     KE keyname                  returns value of keyname if exist.

     KE -c keyname               cancels keyname.

     KE -c keyname key-teeth     creates or changes keyname.

*/


    OTHERWISE SAY Not Valid command':>' input_command user_instruction change2make
    LEAVE
    END

/* Writing the PK file */

SAY OPEN('PK-file-out','*','W')

WRITELN('PK-file-out',vic.1":"vic.1.1";"vic.1.2";"vic.1.3)
WRITELN('PK-file-out',vic.2":"vic.2.1";"vic.2.2";"vic.2.3)
WRITELN('PK-file-out',vic.3":"vic.3.1";"vic.3.2";"vic.3.3)
WRITELN('PK-file-out',vic.4":"vic.4.1";"vic.4.2";"vic.4.3)
WRITELN('PK-file-out',vic.5":"vic.5.1";"vic.5.2";"vic.5.3)
WRITELN('PK-file-out',vic.6":"vic.6.1";"vic.6.2";"vic.6.3)
WRITELN('PK-file-out',vic.6.5)
WRITELN('PK-file-out',vic.7":"vic.7.1";"vic.7.2";"vic.7.3)
WRITELN('PK-file-out',vic.7.5)
WRITELN('PK-file-out',vic.8":"vic.8.1";"vic.8.2";"vic.8.3)
WRITELN('PK-file-out',vic.8.5)
WRITELN('PK-file-out',vic.9":"vic.9.1";"vic.9.2";"vic.9.3)

CLOSE('PK-file-out')

END

/*=====================*/
/* Writing the PK file */

SAY OPEN('PK-file-out','test.pk','W')

WRITELN('PK-file-out',vic.1":"vic.1.1";"vic.1.2";"vic.1.3)
WRITELN('PK-file-out',vic.2":"vic.2.1";"vic.2.2";"vic.2.3)
WRITELN('PK-file-out',vic.3":"vic.3.1";"vic.3.2";"vic.3.3)
WRITELN('PK-file-out',vic.4":"vic.4.1";"vic.4.2";"vic.4.3)
WRITELN('PK-file-out',vic.5":"vic.5.1";"vic.5.2";"vic.5.3)
WRITELN('PK-file-out',vic.6":"vic.6.1";"vic.6.2";"vic.6.3)
WRITELN('PK-file-out',vic.6.5)
WRITELN('PK-file-out',vic.7":"vic.7.1";"vic.7.2";"vic.7.3)
WRITELN('PK-file-out',vic.7.5)
WRITELN('PK-file-out',vic.8":"vic.8.1";"vic.8.2";"vic.8.3)
WRITELN('PK-file-out',vic.8.5)
WRITELN('PK-file-out',vic.9":"vic.9.1";"vic.9.2";"vic.9.3)

CLOSE('PK-file-out')

/**********************************************************************


File/Line Formats

OI - "variable : value : opt comment"

IP - "input type/format : input device : end of input char"

OP - "output type/format : output device : selection flags"

    selection flags:

                   -f = First match
                   -a = All matches
                   -b = Best match
                   -e = Exact match(s)
                   -l = Last match

                   -w = wait until search is complete before applying
                        found. A temp file is used to hold findings.
    IQ only?       -d = dual/two way pattern matching
    IQ only?       -s = soundex (a future application maybe)

                   -p@name = position name of match(s) contents to use.
                            These variables are related to key-teeth.
                            (may specify more than one)
                   *note: standards on -p@name use should be developed.

ID - "wordf to test : command-line to do testing : command/contents
                                                   -p@name posititions"

     If file - lines beginning with FILE>var will be tested:

     "FILE>type : command-line to do testing : command/contents
                                               -p@name positions"

     "keychain-pattern : keyname : file to check"


IQ - "word-pattern : opt command-line if match : command/contents
                                                 -p@name posititions"

     "keychain-pattern : keyname : file to check"

---------------
KE - "keyname:key-teeth/-p@name (to compare w/file-comment):level-check"

            key-teeth/-p@name:
     user definable but defaults/examples are:

                            -t = text
                            -e = executable calls
                            -s = shell script code
                            -ed = education
                            -ref = referance
                            -appl = application

When the keychain is called:
                          :: key-name is looked for in current KE file
                            and if found... does level-check...
***file-comment/OP-p@name 1: key-teeth/-p@name are compared with file
                            comment and if there is a fit...
***ID/IQ LISTS            2: ID/IQ list is searched for a
                            path:filename(date) match and if not found
                            then adds filename to top of stack and list.
                            If found then...
                          3: PK's ID/IQ line is searched for filename
                            and if found moves it to top of stack and
                            picks up at line#. If not found then skips
                            filename (it's already been looked at)...

NEED TO PUT MORE THOUGHT INTO THIS!!!
***********************************************************************

                    ----- END OF DOCUMENT -----
*/