/********************************************************************/
/*                                                                  */
/*  Program name . . . :   TS0012C                                  */
/*  Text . . . . . . . :   WRKRMTWTR: Execute the command from      */
/*                         TS0012C                                  */
/*                                                                  */
/*  Author . . . . . . :   Alexander Nubla                          */
/*  Description. . . . :   This program is called from TS0012C.     */
/*                         It executes the command passed in.       */
/*                         After the command is executed, the       */
/*                         program sends a confirmation message     */
/*                         back to the calling program thru a       */
/*                         Data queue (also) passed in as a parm.   */
/*                                                                  */
/*                         This program was created to have the     */
/*                         adopted authority pointing to ITGMGR.    */
/*                         This is the group profile used company   */
/*                         wide.                                    */
/*                                                                  */
/*                         The DDM file will be passed in as well   */
/*                         to be used by the SBMRMTCMD.             */
/*                                                                  */
/*                         To create this program, use the CRTBNDCL */
/*                         command.                                 */
/*                                                                  */
/*                         Input Parameter                          */
/*                         ---------------                          */
/*                           1 -  10  &DDMNm   DDM File             */
/*                          11 -  20  &DDMLib  DDM File Library     */
/*                          21 -  30  &RmtLoc  Remote Location Name */
/*                          31 -  40  &DtaqNm  Data Queue Name      */
/*                          41 -  50  &DtaqLib Data Queue Library   */
/*                          51 - 150  &Cmd     Command to Execute   */
/*                         151 - 152  &CmdA    Command Length Hex   */
/*                                                                  */
/*                                                                  */
/********************************************************************/
             pgm        (&DDMNm        /* DDM File              */   +
                         &DDMLib       /* DDM File Library      */   +
                         &RmtLoc       /* Remote Location Name  */   +
                         &DtaqNm       /* Data Queue Name       */   +
                         &DtaqLib      /* Data Queue Library    */   +
                         &Cmd          /* Command to Execute    */   +
                         &CmdA)        /* Command Length (Hex)  */

/*--------------------------------------------------------*/
/*  declaration                                           */
/*--------------------------------------------------------*/
             dcl        &DDMNm     *char   10
             dcl        &DDMLib    *char   10
             dcl        &RmtLoc    *char   10
             dcl        &DtaqNm    *char   10
             dcl        &DtaqLib   *char   10
             dcl        &Cmd       *char  100
             dcl        &CmdA      *char    2

             dcl        &CmdL      *dec     5
             dcl        &Interactv *char
             dcl        &Entlen    *dec     5    200
             dcl        &Entry     *char  200
             dcl        &msgtype   *char    2
             dcl        &CmdExc    *char  100

             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 */

             monmsg     msgid(cpf0000) exec(goto error)
             rtvjoba    type(&interactv)

/*--------------------------------------------------------*/
/*  Submit this program to batch in sbs(QCTL)             */
/*--------------------------------------------------------*/
             if         (&interactv  *eq '1') then(do)
               sbmjob     job(rmtcmd)            +
                            cmd(call ts0012c     +
                              parm(&DDMNm        +
                                   &DDMLib       +
                                   &RmtLoc       +
                                   &DtaqNm       +
                                   &DtaqLib      +
                                   &Cmd          +
                                   &CmdA))       +
                            log(0 00 *nolist)    +
                            logclpgm(*no)        +
                            inllibl(*current)    +
                            user(ITGMGR)         +
                            jobq(Qctl)
               return
             enddo

/*--------------------------------------------------------*/
/*  Create the DDM file connection needed to execute      */
/*  commands on the Remote Location.                      */
/*--------------------------------------------------------*/
             Crtddmf    file(&DDMLib/&DDMNm)     +
                        rmtfile(x)               +
                        rmtlocname(x)            +
                        mode(DDM)                +
                        replace(*no)             +
                        aut(*exclude)
             Monmsg     CPF7302        /* already exist   */
          /*----------------------------------------------*/
          /* Change the ownership of the DDMF to ITGMGR   */
          /*----------------------------------------------*/
             chgobjown  obj(&DDMLib/&DDMNm)      +
                        objtype(*file)           +
                        newown(ITGMGR)           +
                        curownaut(*revoke)
             Rmvmsg     clear(*all)
          /*----------------------------------------------*/
          /* Change the DDM file to point to the correct  */
          /* remote location name.                        */
          /*----------------------------------------------*/
             Chgddmf    file(&DDMLib/&DDMNm)     +
                        rmtlocname(&RmtLoc)      +
                        mode(DDM)
             Rcvmsg     msgtype(*comp)

/*--------------------------------------------------------*/
/*  Execute the remote command                            */
/*--------------------------------------------------------*/
             chgvar     &CmdL      %bin(&CmdA 1 2)
             chgvar     &CmdExc    %sst(&Cmd 1 &Cmdl)

             SbmRmtCmd  Cmd(&CmdExc)           +
                        ddmfile(&DDMLib/&DDMNm)
             Monmsg     cpf0000
             Rcvmsg     msgtype(*last)    +
                        rtntype(&msgtype) +
                        msgdta(&msgdta)   +
                        msgid(&msgid)     +
                        msgf(&msgf)       +
                        msgflib(&msgflib)
          /*----------------------------------------------*/
          /* Return the message information received      */
          /* after the submit remote command. Concatenate */
          /* the following parameter into &Entry          */
          /*                                              */
          /* Break down the data queue with the following */
          /* layout:                                      */
          /*                                              */
          /* &Msgtype     01  Completion                  */
          /* 1 - 2        02  Diagnostic                  */
          /*              04  Information                 */
          /*              05  Inquiry                     */
          /*              04  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     %sst(&Entry   1   2)    &MsgType
             chgvar     %sst(&Entry  11 100)    &MsgDta
             chgvar     %sst(&Entry 111   7)    &MsgId
             chgvar     %sst(&Entry 121  10)    &Msgf
             chgvar     %sst(&Entry 131  10)    &MsgfLib
/*--------------------------------------------------------*/
/*  Clean data queue first, then return the entries       */
/*--------------------------------------------------------*/
             clrdtaq    &DtaqLib/&DtaqNm
             Monmsg     CPF0000
             call       qsnddtaq   parm(&DtaqNm  +
                                        &DtaqLib +
                                        &Entlen  +
                                        &Entry)
             rclddmcnv
             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
