/********************************************************************/
/*                                                                  */
/*  Program name . . . :   TS0011C                                  */
/*  Text . . . . . . . :   WRKRMTWTR: Local & Remote OUTQ Device    */
/*                         Driver                                   */
/*                                                                  */
/*  Author . . . . . . :   Alexander Nubla                          */
/*  Description. . . . :   This module is called from TS0010R. This */
/*                         module is used to maintain the local &   */
/*                         remote Output Queue Devices requested    */
/*                         in TS0010R.                              */
/*                                                                  */
/*                         To create this module, use the CRTCLMOD  */
/*                         command.  After the module is created,   */
/*                         use the CRTPGM/UPDPGM command for program*/
/*                         TS0010R.                                 */
/*                                                                  */
/********************************************************************/
             pgm        (&Action       /* A=Add;E=Edit;D=Delete */   +
                         &SrcOQN       /* Source OUTQ Name      */   +
                         &SrcOQL       /* Source OUTQ Library   */   +
                         &SrcOQT       /* Source OUTQ Text      */   +
                         &TgtSys       /* Target (RMTLOCNAME)   */   +
                         &TgtOQN       /* Target OUTQ Name      */   +
                         &TgtOQL       /* Target OUTQ Library   */   +
                         &Pgmq         /* Target OUTQ Library   */   +
                         &Delt)        /* B=Both,S=Src,T=Tgt    */


/*--------------------------------------------------------*/
/*  declaration                                           */
/*--------------------------------------------------------*/
             dcl        &Action    *char    1
             dcl        &SrcOQN    *char   10
             dcl        &SrcOQL    *char   10
             dcl        &SrcOQT    *char   45
             dcl        &TgtSys    *char   10
             dcl        &TgtOQN    *char   10
             dcl        &TgtOQL    *char   10
             dcl        &Pgmq      *char   10
             dcl        &Delt      *char    1

             dcl        &TgtOQT    *char   45
             dcl        &System    *char    8
             dcl        &Cmd       *char  100
             dcl        &Reply     *char    1
             dcl        &LogCl     *char   10
             dcl        &JobNo     *char    6
             dcl        &DDMNm     *char   10
             dcl        &DtaqNm    *char   10    'DTAQ'
             dcl        &DtaqTxt   *char   50
             dcl        &Entlen    *dec     5    200
             dcl        &Entry     *char  200
             dcl        &Wait      *dec     5    -1
             dcl        &DftLib    *char   10    'ITGSYS'
             dcl        &m         *char    2
             dcl        &d         *char    2
             dcl        &y         *char    2
             dcl        &msgtype   *char    2
             dcl        &CmdA      *char    2
             dcl        &SrcXsis   *lgl
             dcl        &CmdL      *dec     5
             dcl        &EndCtr    *dec     5

             dcl        &error     *lgl                   /* std err */
             dcl        &msgid     *char    7             /* std err */
             dcl        &msgkey    *char    4             /* std err */
             dcl        &msgdta    *char  100             /* std err */
             dcl        &msgf      *char   10             /* std err */
             dcl        &msgflib   *char   10             /* std err */
             dcl        &msgtyp    *char   10  '*DIAG'    /* std err */
             dcl        &msgtypctr *char    4 X'00000001' /* std err */
             dcl        &pgmmsgq   *char   10  '*'        /* std err */
             dcl        &stkctr    *char    4 X'00000001' /* std err */
             dcl        &errbytes  *char    4 X'00000000' /* std err */

             dclf       TS0011CD

             monmsg     msgid(cpf0000) exec(goto error)
             sndf       rcdfmt(ts0011d1)
             Rtvjoba    logclpgm(&Logcl) +
                        user(&DDMNm)     +
                        nbr(&JobNo)
             chgjob     logclpgm(*no)
             Chgvar     &EndCtr    0
/*--------------------------------------------------------*/
/*  Create the data queue used for triggers               */
/*--------------------------------------------------------*/
             Chgvar     &DtaqNm    (&DtaqNm |< &JobNo)
             rtvsysval  qmonth     &m
             rtvsysval  qday       &d
             rtvsysval  qyear      &y
             chgvar     &Dtaqtxt   (&m|<'/'|<&d|<'/'|<&y |< +
                                    ': Date WRKRMTWTR used')
             dltdtaq    dtaq(&DftLib/&DtaqNm)
             monmsg     (cpf2105 cpf9899)  /* Not found   */
             crtdtaq    dtaq(&DftLib/&DtaqNm)  +
                        maxlen(200)            +
                        aut(*exclude)          +
                        text(&DtaqTxt)
             Monmsg     CPF7302        /* already exist   */
          /*----------------------------------------------*/
          /* Change the ownership of the DTAQ to ITGMGR   */
          /*----------------------------------------------*/
             chgobjown  obj(&DftLib/&DtaqNm)   +
                        objtype(*dtaq)         +
                        newown(ITGMGR)         +
                        curownaut(*revoke)

/*--------------------------------------------------------*/
/*  Change the text for the local output queue.           */
/*--------------------------------------------------------*/
             If         cond(&SrcOQT = ' ')      then(do)
               Chgvar     var(&SrcOQT)   +
                          value('RMTWTR spools Xfr to' |> +
                          &TgtSys  |< ', OUTQ:'        |> +
                          &TgtOQL  |< '/' |<  &TgtOQN)
             Enddo
/*--------------------------------------------------------*/
/*  Change the text for the Target output queue.          */
/*--------------------------------------------------------*/
             rtvneta    sysname(&System)
             Chgvar     var(&TgtOQT)   +
                        value('OUTQ coming from:' |> +
                        &System  |< ', OUTQ:'     |> +
                        &SrcOQL  |< '/' |<  &SrcOQN)

/*--------------------------------------------------------*/
/*  Maintain the Local OUTQ based on the @Action          */
/*  requested by the user.                                */
/*--------------------------------------------------------*/
 LOCALOUTQ:
             Chgvar     &SrcXsis       '1'
             Chkobj     obj(&SrcOQL/&SrcOQN)     +
                        objtype(*outq)
             Monmsg     CPF9801        Exec(do)
               chgvar     &SrcXsis       '0'
               If         (&Action = 'D')  Then(do)
                 Goto       TARGETOUTQ
               Enddo
               If         (&Action = 'X')  Then(do)
                 Goto       DISCONNECT
               Enddo
             Enddo
          /*----------------------------------------------*/
          /* Delete Local OUTQ Device                     */
          /*----------------------------------------------*/
             If         (&Action   = 'D'   *And +
                         &Delt   *ne 'T')  Then(do)
               Clroutq    outq(&SrcOQL/&SrcOQN)
               Monmsg     cpf0000
    DelEndWtr:
               Endwtr     wtr(&SrcOQN) option(*immed)
               Monmsg     cpf0000
               Rcvmsg     msgtype(*excp)    +
                            msgdta(&msgdta) +
                            msgid(&msgid)   +
                            msgf(&msgf)     +
                            msgflib(&msgflib)
               If         (&msgid = 'CPF3339')  then(do)
                 chgvar     &EndCtr      (&EndCtr + 1)
                 If         (&EndCtr < 3)     then(do)
                   goto       DelEndWtr
                 EndDo
               EndDo
               Dltoutq    outq(&SrcOQL/&SrcOQN)
               Monmsg     cpf0000
               Rcvmsg     msgtype(*last)    +
                            msgdta(&msgdta) +
                            msgid(&msgid)   +
                            msgf(&msgf)     +
                            msgflib(&msgflib)
          /*----------------------------------------------*/
          /* DLTOUTQ fail because outq is still attached  */
          /* to a writer. Send message to user to reply   */
          /* R for Retry or C for Cancel request.         */
          /*----------------------------------------------*/
               If         (&msgid = 'CPF3360')  then(do)
                 Sndpgmmsg  msgid(cpf9898) msgf(qcpfmsg) +
                            msgdta('Cannot delete output queue. +
                            A writer is still attached to output +
                            queue' |> &SrcOQN |> '. Enter R to +
                            Retry or C to Cancel the request. +
                            (R C)') +
                            msgtype(*Inq)
                 Rcvmsg     msgtype(*inq )    +
                              msgdta(&reply)
                 If         (&Reply = 'R')    then(do)
                   chgvar     &EndCtr      0
                   goto       DelEndWtr
                 EndDo
                 goto       End
               EndDo

          /*----------------------------------------------*/
          /* Output Queue successfully deleted.           */
          /*----------------------------------------------*/
               If         (&msgid = 'CPC2191')  then(do)
                 sndpgmmsg  msgid(&msgid)          +
                              msgf(&msgflib/&msgf) +
                              msgdta(&msgdta)      +
                              topgmq(*same &pgmq)
               EndDo
               Goto       TARGETOUTQ
             Enddo
          /*----------------------------------------------*/
          /* Disconnect Local OUTQ Device                 */
          /*----------------------------------------------*/
             If         (&Action   = 'X')  Then(do)
               Chgoutq    outq(&SrcOQL/&SrcOQN)  +
                          rmtsys(*None)          +
                          rmtprtq(*User)         +
                          autostrwtr(*None)
               Monmsg     cpf0000
               Goto       DISCONNECT
             Enddo
          /*----------------------------------------------*/
          /* Update the Local OUTQ pointing to the Target */
          /* System and Target OUTQ.                      */
          /*----------------------------------------------*/
             If         &SrcXsis         Then(do)
    UpdEndWtr:
               Endwtr     wtr(&SrcOQN) option(*immed)
               Monmsg     cpf0000
               Rcvmsg     msgtype(*excp)    +
                            msgdta(&msgdta) +
                            msgid(&msgid)   +
                            msgf(&msgf)     +
                            msgflib(&msgflib)
               If         (&msgid = 'CPF3339')  then(do)
                 chgvar     &EndCtr      (&EndCtr + 1)
                 If         (&EndCtr < 3)     then(do)
                   goto       UpdEndWtr
                 EndDo
               EndDo
          /*-----------------------------------------*/
          /*  No target output queue to attach since */
          /*  the user requested to delete the OUTQ  */
          /*  in the target machine.  Change the     */
          /*  source OUTQ pointing to *NONE for the  */
          /*  remote system                          */
          /*-----------------------------------------*/
               If         (&Action = 'D'   *And +
                           &Delt   = 'T')  Then(do)
                 Chgoutq    outq(&SrcOQL/&SrcOQN)    +
                            rmtsys(*None)            +
                            rmtprtq(*User)           +
                            autostrwtr(*None)        +
                            text(&SrcOQT)
                 Monmsg     cpf0000
               EndDo
          /*-----------------------------------------*/
          /*  Check first if the target system is    */
          /*  blank.  If so, there is no remote      */
          /*  system to attach.  Therefore, change   */
          /*  the source OUTQ pointing to *NONE for  */
          /*  the remote system.                     */
          /*-----------------------------------------*/
               Else       Do
                 If         (&TgtSys = ' ')  then(do)
                   Chgoutq    outq(&SrcOQL/&SrcOQN)    +
                              rmtsys(*None)            +
                              rmtprtq(*User)           +
                              autostrwtr(*None)        +
                              text(&SrcOQT)
                   Monmsg     cpf0000
                 EndDo
          /*-----------------------------------------*/
          /*  Change the source OUTQ to point to the */
          /*  target machine for the remote system & */
          /*  target OUTQ for the remote printer     */
          /*  queue.                                 */
          /*-----------------------------------------*/
                 Else       Do
                   Chgoutq    outq(&SrcOQL/&SrcOQN)    +
                              rmtsys(&TgtSys)          +
                              rmtprtq(&TgtOQN)         +
                              autostrwtr(1)            +
                              text(&SrcOQT)
                   Monmsg     cpf0000
                 EndDo
               EndDo
               Rcvmsg     msgtype(*excp)    +
                            msgdta(&msgdta) +
                            msgid(&msgid)   +
                            msgf(&msgf)     +
                            msgflib(&msgflib)
          /*----------------------------------------------*/
          /* CHGOUTQ fail because outq is still attached  */
          /* to a writer. Send message to user to reply   */
          /* R for Retry or C for Cancel request.         */
          /*----------------------------------------------*/
               If         (&msgid = 'CPF3319')  then(do)
                 Sndpgmmsg  msgid(cpf9898) msgf(qcpfmsg) +
                            msgdta('Cannot change output queue. +
                            A writer is still attached to output +
                            queue' |> &SrcOQN |> '. Enter R to +
                            Retry or C to Cancel the request. +
                            (R C)') +
                            msgtype(*Inq)
                 Rcvmsg     msgtype(*inq )    +
                              msgdta(&reply)
                 If         (&Reply = 'R')    then(do)
                   chgvar     &EndCtr      0
                   Goto       UpdEndWtr
                 EndDo
                 Goto       End
               EndDo
               Goto       TARGETOUTQ
             EndDo
          /*----------------------------------------------*/
          /* Check first if the target system is blank.   */
          /* If so, there is no remote system to attach.  */
          /* Therefore create the source OUTQ pointing    */
          /* to *NONE for the remote system.              */
          /*----------------------------------------------*/
             If         (&TgtSys = ' ')  then(do)
               Crtoutq    outq(&SrcOQL/&SrcOQN)    +
                          rmtsys(*None)            +
                          rmtprtq(*User)           +
                          autostrwtr(*None)        +
                          text(&SrcOQT)
               Monmsg     cpf0000
             EndDo
          /*-----------------------------------------*/
          /*  Create the Local OUTQ pointing to the  */
          /*  Target System and Target OUTQ.         */
          /*-----------------------------------------*/
             Else       Do
               Crtoutq    outq(&SrcOQL/&SrcOQN)    +
                          rmtsys(&TgtSys)          +
                          rmtprtq(&TgtOQN)         +
                          autostrwtr(1)            +
                          text(&SrcOQT)
               Monmsg     cpf0000
             EndDo
             Rcvmsg     msgtype(*last)    +
                          msgdta(&msgdta) +
                          msgid(&msgid)   +
                          msgf(&msgf)     +
                          msgflib(&msgflib)
          /*----------------------------------------------*/
          /* Output Queue successfully created.           */
          /*----------------------------------------------*/
             sndpgmmsg  msgid(&msgid)          +
                          msgf(&msgflib/&msgf) +
                          msgdta(&msgdta)      +
                          topgmq(*same &pgmq)
          /*----------------------------------------------*/
          /* Change the ownership of the output queue to  */
          /* ITGMGR                                       */
          /*----------------------------------------------*/
             chgobjown  obj(&SrcOQL/&SrcOQN)   +
                        objtype(*outq)         +
                        newown(ITGMGR)         +
                        curownaut(*revoke)

/*--------------------------------------------------------*/
/*  Maintain the Remote OUTQ based on the @Action         */
/*  requested by the user.                                */
/*--------------------------------------------------------*/
 TARGETOUTQ:
          /*----------------------------------------------*/
          /* Bypass the next procedures if the request is */
          /* for Source OUTQ only                         */
          /*----------------------------------------------*/
             If         (&Delt   = 'S'   *or +
                         &TgtSys = ' ')  Then(do)
               Goto       DISCONNECT
             Enddo
          /*----------------------------------------------*/
          /* Delete the Remote OUTQ Device                */
          /*----------------------------------------------*/
             If         (&Action = 'D')  Then(do)
               Chgvar     &Cmd       ('DLTOUTQ OUTQ(')
               Chgvar     &Cmd       (&Cmd  |< &TgtOQL)
               Chgvar     &Cmd       (&Cmd  |< '/')
               Chgvar     &Cmd       (&Cmd  |< &TgtOQN)
               Chgvar     &Cmd       (&Cmd  |< ')')
             Enddo
          /*----------------------------------------------*/
          /* Create the Remote OUTQ Device                */
          /*----------------------------------------------*/
             Else       Do
               Chgvar     &Cmd       ('CRTOUTQ OUTQ(')
               Chgvar     &Cmd       (&Cmd  |< &TgtOQL)
               Chgvar     &Cmd       (&Cmd  |< '/')
               Chgvar     &Cmd       (&Cmd  |< &TgtOQN)
               Chgvar     &Cmd       (&Cmd  |< ') TEXT(''')
               Chgvar     &Cmd       (&Cmd  |< &TgtOQT)
               Chgvar     &Cmd       (&Cmd  |< ''')')
             Enddo
          /*----------------------------------------------*/
          /* Call the command processor TS0012C. After    */
          /* the call, wait for the reply back through    */
          /* the &DftLib/&DtaqNm                          */
          /*                                              */
          /* Break down the data queue with the following */
          /* layout:                                      */
          /*                                              */
          /* &Msgtype     01  Completion                  */
          /* 1 - 2        02  Diagnostic                  */
          /*              04  Information                 */
          /*              05  Inquiry                     */
          /*              14  Notify                      */
          /*              15  Escape                      */
          /*                                              */
          /* &MsgDta      The message data received after */
          /* 11 - 110     &Cmd is executed                */
          /*                                              */
          /* &MsgId       The message data received after */
          /* 111 - 117    &Cmd is executed                */
          /*                                              */
          /* &Msgf        The message file received after */
          /* 121 - 130    &Cmd is executed                */
          /*                                              */
          /* &MsgfLib     The message file library        */
          /* 131 - 140    received after the &Cmd is      */
          /*              executed                        */
          /*                                              */
          /*----------------------------------------------*/
             chgvar     &CmdL     100
          /*------------------------------------*/
          /*  Compute for command length        */
          /*------------------------------------*/
     Scan1:
             If         (%sst(&Cmd  &CmdL  1) = ' ')  Then(do)
               chgvar     &CmdL      (&CmdL - 1)
               if         (&CmdL = 0)  then(goto end)
               goto       scan1
             Enddo
             chgvar     %bin(&CmdA 1 2)     &CmdL
             call       ts0012c   (&DDMNm   +
                                   &DftLib  +
                                   &Tgtsys  +
                                   &DtaqNm  +
                                   &DftLib  +
                                   &Cmd     +
                                   &CmdA)
     Wait1:
             call       qrcvdtaq  (&DtaqNm  +
                                   &DftLib  +
                                   &EntLen  +
                                   &Entry   +
                                   &Wait)
          /*----------------------------------------------*/
          /* Break down all the fields from &Entry        */
          /*----------------------------------------------*/
             chgvar     &MsgType   %sst(&Entry   1   2)
             chgvar     &MsgDta    %sst(&Entry  11 100)
             chgvar     &MsgId     %sst(&Entry 111   7)
             chgvar     &Msgf      %sst(&Entry 121  10)
             chgvar     &MsgfLib   %sst(&Entry 131  10)
          /*----------------------------------------------*/
          /* Return the message back to TS0010R           */
          /*----------------------------------------------*/
             sndpgmmsg  msgid(&msgid)          +
                          msgf(&msgflib/&msgf) +
                          msgdta(&msgdta)      +
                          topgmq(*same &pgmq)
          /*----------------------------------------------*/
          /* Change the ownership of the output queue to  */
          /* ITGMGR                                       */
          /*----------------------------------------------*/
             If         (&Action *ne 'D'         *and   +
                         &Msgid    = 'CPC9801')  Then(do)
               Chgvar     &Cmd       ('CHGOBJOWN OBJ(')
               Chgvar     &Cmd       (&Cmd  |< &TgtOQL)
               Chgvar     &Cmd       (&Cmd  |< '/')
               Chgvar     &Cmd       (&Cmd  |< &TgtOQN)
               Chgvar     &Cmd       (&Cmd  |< ') +
                                     OBJTYPE(*OUTQ) +
                                     NEWOWN(ITGMGR) +
                                     CUROWNAUT(*REVOKE)')
               chgvar     &CmdL     100
          /*------------------------------------*/
          /*  Compute for command length        */
          /*------------------------------------*/
      Scan2:
               If         (%sst(&Cmd  &CmdL  1) = ' ')  Then(do)
                 chgvar     &CmdL      (&CmdL - 1)
                 if         (&CmdL = 0)  then(goto end)
                 goto       scan2
               Enddo
               chgvar     %bin(&CmdA 1 2)     &CmdL
               call       ts0012c   (&DDMNm   +
                                     &DftLib  +
                                     &Tgtsys  +
                                     &DtaqNm  +
                                     &DftLib  +
                                     &Cmd     +
                                     &CmdA)
     Wait2:
               call       qrcvdtaq  (&DtaqNm  +
                                     &DftLib  +
                                     &EntLen  +
                                     &Entry   +
                                     &Wait)
             Enddo


/*--------------------------------------------------------*/
/*  Disconnect from the target                            */
/*--------------------------------------------------------*/
 DISCONNECT:
             dltdtaq    dtaq(&DftLib/&DtaqNm)
             monmsg     cpf0000
             chgjob     logclpgm(&Logcl)
             Goto       End

/*--------------------------------------------------------*/
/*  error routine:                                        */
/*--------------------------------------------------------*/
 error:
             if         &error     (goto errordone)
               else      chgvar       &error  '1'
          /*----------------------------------------------*/
          /*  move all *DIAG message to *PRV program queue*/
          /*----------------------------------------------*/
             call       QMHMOVPM   (&msgkey      +
                                    &msgtyp      +
                                    &msgtypctr   +
                                    &pgmmsgq     +
                                    &stkctr      +
                                    &errbytes)
          /*----------------------------------------------*/
          /*  resend the last *ESCAPE message             */
          /*----------------------------------------------*/
 errordone:
             call       QMHRSNEM   (&msgkey      +
                                    &errbytes)
             monmsg     cpf0000    exec(do)
               sndpgmmsg  msgid(cpf3cf2) msgf(QCFPMSG) +
                            msgdta('QMHRSNEM') msgtype(*escape)
               monmsg     cpf0000
             enddo
 end:        endpgm
