REPORT YMASTCP .

* This program raise an event START_MCP to start an automatic
* master client copy if there are no users in the system and
* there is no sceduled background job for the time when the client
* copy is running. The time period can be changed (syuzeit), and all
* the background jobs starting with @ are avoided. During this time
* the users get a system message in case of login attempt.
* The event should be specified in CCMS and a event driven client
* copy should be scheduled by SCC0. 

TABLES: UINFO, TBTCO.

DATA: SYUZEIT TYPE T, SYDATUM TYPE D, LIN TYPE N, OPCODE TYPE X VALUE 2,
      FLAG, STCR.

DATA: BEGIN OF ITAB OCCURS 10,
   JOBNAME LIKE TBTCO-JOBNAME,
   SDLSTRTDT LIKE TBTCO-SDLSTRTDT,
   SDLSTRTTM LIKE TBTCO-SDLSTRTTM,
   EVENTID LIKE TBTCO-EVENTID,
END OF ITAB.

DATA: BEGIN OF BDC_TAB OCCURS 5.
        INCLUDE STRUCTURE BDCDATA.
DATA: END OF BDC_TAB.

DATA: BEGIN OF DATUM,
   1(2),
   2 VALUE '.',
   3(2),
   4 VALUE '.',
   5(4),
END OF DATUM.

DATA: BEGIN OF TIME,
   1(2),
   2 VALUE ':',
   3(2),
   4 VALUE ':',
   5(2),
END OF TIME.

DATA: BEGIN OF USR_TABL OCCURS 10.
        INCLUDE STRUCTURE UINFO.
DATA: END OF USR_TABL.

* Count date and time for system message

SYDATUM = SY-DATUM.
SYUZEIT = SY-UZEIT + 7200.             "MAX 12 hours overflow!!!!!
IF SY-UZEIT > SYUZEIT. SYDATUM = SY-DATUM + 1.ENDIF.
DATUM-5 = SYDATUM. DATUM-3 = SYDATUM+4(2). DATUM-1 = SYDATUM+6(2).
TIME-1 = SYUZEIT. TIME-3 = SYUZEIT+2(2). TIME-5 = SYUZEIT(2).

WRITE: / SY-DATUM, SY-UZEIT.
WRITE: / DATUM, TIME.

* Check active users

CALL 'ThUsrInfo' ID 'OPCODE' FIELD OPCODE
  ID 'TAB' FIELD USR_TABL-*SYS*.
SORT USR_TABL BY BNAME.

DESCRIBE TABLE USR_TABL LINES LIN.
IF LIN > 0.
  SKIP.
  WRITE: / LIN, 'ACTIVE USER IS IN THE SYSTEM'.
  SKIP.
  LOOP AT USR_TABL.
    WRITE: /2 USR_TABL-MANDT, 15 USR_TABL-BNAME.
  ENDLOOP.
  SKIP.
  WRITE: / 'MASTER CLIENT COPY ABORTED!!!'.
  EXIT.
ENDIF.

* Are there scheduled jobs ?

IF SYDATUM = SY-DATUM.
  SELECT * FROM TBTCO WHERE STATUS = 'S' AND SDLSTRTTM <> '        '
  AND  SDLSTRTTM > SY-UZEIT AND SDLSTRTTM < SYUZEIT
  AND  SDLSTRTDT = SYDATUM.
    MOVE-CORRESPONDING TBTCO TO ITAB.
    APPEND ITAB.
  ENDSELECT.
ELSE.
  SELECT * FROM TBTCO WHERE STATUS = 'S' AND SDLSTRTTM <> '        '
  AND ( ( ( SDLSTRTDT = SY-DATUM ) AND ( SDLSTRTTM > SYUZEIT ) ) OR
        ( ( SDLSTRTDT = SYDATUM )  AND ( SDLSTRTTM < SYUZEIT ) ) ).
    MOVE-CORRESPONDING TBTCO TO ITAB.
    APPEND ITAB.
  ENDSELECT.
ENDIF.

DESCRIBE TABLE ITAB LINES LIN.
IF LIN > 0.
  LOOP AT  ITAB.
    STCR = ITAB-JOBNAME.
    IF STCR <> '@'.
      FLAG = 'R'.
      WRITE: / ITAB.
    ENDIF.
  ENDLOOP.
  IF FLAG = 'R'.
    SKIP.
    WRITE: / 'MASTER CLIENT COPY ABORTED!!!'.
    EXIT.
  ENDIF.
ENDIF.

* Enter system message: Do not work in client 21!!!

BDC_TAB-PROGRAM = 'SAPMSSY0'.
BDC_TAB-DYNPRO = '0120'.
BDC_TAB-DYNBEGIN = 'X'.
APPEND BDC_TAB.
CLEAR BDC_TAB.

BDC_TAB-FNAM = 'BDC_OKCODE'.
BDC_TAB-FVAL = '/5'.
APPEND BDC_TAB.
CLEAR BDC_TAB.

BDC_TAB-PROGRAM = 'SAPMSEM1'.
BDC_TAB-DYNPRO = '4400'.
BDC_TAB-DYNBEGIN = 'X'.
APPEND BDC_TAB.
CLEAR BDC_TAB.

BDC_TAB-FNAM = 'EMLINE1'.
BDC_TAB-FVAL = 'DO NOT LOG IN TO CLIENT 21!!!!'.
APPEND BDC_TAB.
CLEAR BDC_TAB.

BDC_TAB-FNAM = 'EMLINE2'.
BDC_TAB-FVAL = 'OTHERWISE YOU MIGHT DAMAGE CLIENT 21 and 999!!!!'.
APPEND BDC_TAB.
CLEAR BDC_TAB.

BDC_TAB-FNAM = 'TEMSG-DATDEL'.
BDC_TAB-FVAL = DATUM.
APPEND BDC_TAB.
CLEAR BDC_TAB.

BDC_TAB-FNAM = 'TEMSG-TIMDEL'.
BDC_TAB-FVAL = TIME.
APPEND BDC_TAB.
CLEAR BDC_TAB.

BDC_TAB-PROGRAM = 'SAPMSYST'.
BDC_TAB-DYNPRO = '0040'.
BDC_TAB-DYNBEGIN = 'X'.
APPEND BDC_TAB.
CLEAR BDC_TAB.

BDC_TAB-FNAM = 'BDC_OKCODE'.
BDC_TAB-FVAL = '/3'.
APPEND BDC_TAB.
CLEAR BDC_TAB.

CALL TRANSACTION 'SM02' USING BDC_TAB MODE 'N'.

CALL FUNCTION 'BP_EVENT_RAISE'
     EXPORTING
          EVENTID = 'START_MCP'.

SKIP.
WRITE: / 'EVENT: START_MCP RAISED'.