/******************************************************************************
* COPYRIGHT (C) 1990-1996 by      SECURITY DYNAMICS TECHNOLOGIES, INC.        *
*                         ---ALL RIGHTS RESERVED---                           *
*                                                                             *
* THIS SOFTWARE IS PROPRIETARY AND CONFIDENTIAL TO SECURITY DYNAMICS          *
* TECHNOLOGIES, INC., IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED *
* ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE INCLUSION    *
* OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE OR ANY OTHER COPIES THEREOF   *
* MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER PERSON.  NO    *
* TITLE TO OR OWNERSHIP OF THE SOFTWARE IS HEREBY TRANSFERRED.                *
*                                                                             *
* THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND    *
* SHOULD NOT BE CONSTRUED AS A COMMITMENT BY SECURITY DYNAMICS TECHNOLOGIES,  *
* INC.                                                                        *
******************************************************************************/

/* This file is used to calculate the total number of records in the given
 * database.  The result is stored in the given output file.
 * This file is used for sdmigrate.sh.
 */
   
DEFINE VARIABLE db-file AS CHARACTER.
DEFINE VARIABLE output-file AS CHARACTER.
DEFINE VARIABLE total-records AS INTEGER.
DEFINE VARIABLE count-eccx-recs AS LOGICAL.

  output-file="mcount.dat".
  run parse-param(INPUT SESSION:PARAMETER, 
                  INPUT-OUTPUT output-file, OUTPUT db-file).

  OUTPUT TO VALUE(output-file).

  IF ( SUBSTRING(db-file, LENGTH(db-file) - 5, 6) = "sdserv" ) then do:
    RUN count-srv(INPUT db-file, OUTPUT total-records).
  END.
  ELSE DO:
    RUN count-log(INPUT db-file, OUTPUT total-records).
  END.

  MESSAGE total-records.


PROCEDURE parse-param:
  DEFINE INPUT PARAMETER param-val AS CHARACTER.
  DEFINE INPUT-OUTPUT PARAMETER fname1 AS CHARACTER.
  DEFINE OUTPUT PARAMETER fname2 AS CHARACTER.

  DEFINE VARIABLE space-pos AS INTEGER.

  space-pos = INDEX(param-val, " ").

  IF ( space-pos <> 0 ) THEN DO:
    fname1 = SUBSTRING(param-val, 1, space-pos - 1).
  END.

  fname2 = SUBSTRING(param-val, space-pos + 1, LENGTH(param-val) - space-pos). 
  RETURN.
END.

PROCEDURE count-srv:
  DEFINE INPUT PARAMETER db-path AS CHARACTER.
  DEFINE OUTPUT PARAMETER tot-rec AS INTEGER INIT 0.

  DEFINE VARIABLE sub-total AS INTEGER.
  
  IF NOT CONNECTED("sdserv") THEN DO:
    CONNECT VALUE(db-path) -1 NO-ERROR.
  END.
  IF ERROR-STATUS:ERROR THEN DO:
    tot-rec = -1.
    IF CONNECTED("sdlog") THEN DISCONNECT sdlog.
  END.
  ELSE DO:
    RUN sdcount_rec.p ( OUTPUT sub-total) "CustClientExtension".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "CustGroupExtension".
    tot-rec = tot-rec + sub-total.
    /*** Not needed until try to migrate a 3.0.1 database ***
    RUN sdcount_rec.p ( OUTPUT sub-total) "CustRealmExtension".
    tot-rec = tot-rec + sub-total.
    ***/
    RUN sdcount_rec.p ( OUTPUT sub-total) "CustSiteExtension".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "CustSystemExtension".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "CustTokenExtension".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "CustUserExtension".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaAdministrator".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaClient".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaClientType".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaCustClientExt".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaCustGroupExt".
    tot-rec = tot-rec + sub-total.
    /***
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaCustRealmExt".
    tot-rec = tot-rec + sub-total.
    ***/
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaCustSiteExt".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaCustSystemExt".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaCustTokenExt".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaCustUserExt".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaEnabledGroup".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaEnabledUser".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaGroup".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaGroupMember".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaNode".
    tot-rec = tot-rec + sub-total.
    /***
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaOneTimePassword".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaRealm".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaRealmEnabledGroup".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaRealmEnabledUser".
    tot-rec = tot-rec + sub-total.
    ***/
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaSite".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaSystem".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaToken".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaUser".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDAdministrator".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDClient".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDClientType".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDEnabledGroup".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDEnabledUser".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDGroup".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDGroupMember".
    tot-rec = tot-rec + sub-total.
    /***
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDOneTimePassword".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDRealm".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDRealmEnabledGroup".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDRealmEnabledUser".
    tot-rec = tot-rec + sub-total.
    ***/
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDSecondaryNode".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDSite".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDSystem".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDToken".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDUser".
    tot-rec = tot-rec + sub-total.
    DISCONNECT sdserv.
  END.
  RETURN.
END.


PROCEDURE count-log:
  DEFINE INPUT PARAMETER db-path AS CHARACTER.
  DEFINE OUTPUT PARAMETER tot-rec AS INTEGER.

  DEFINE VARIABLE sub-total AS INTEGER.

  IF NOT CONNECTED("sdlog") THEN DO:
    CONNECT VALUE(db-path) -1 NO-ERROR.
  END.
  IF ERROR-STATUS:ERROR THEN DO:
    tot-rec = -1.
    IF CONNECTED("sdserv") THEN DISCONNECT sdserv.
  END.
  ELSE DO:
    /* Tables in sdlog.db */
    RUN sdcount_rec.p ( OUTPUT sub-total) "CustLogExtension".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaSDLogMessage".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaSDLogReportFormat".
    tot-rec = tot-rec + sub-total.
    /***
    RUN sdcount_rec.p ( OUTPUT sub-total) "DeltaSDSysLogCriteria".
    tot-rec = tot-rec + sub-total.
    ***/
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDLogEntry".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDLogMessage".
    tot-rec = tot-rec + sub-total.
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDLogReportFormat".
    tot-rec = tot-rec + sub-total.
    /***
    RUN sdcount_rec.p ( OUTPUT sub-total) "SDSysLogCriteria".
    tot-rec = tot-rec + sub-total.
    ***/
    DISCONNECT sdlog.
  END.
  RETURN.
END.




