/*
 * cl_svr.pgc - cl (마감/closing) MODULE SERVER (thin dispatcher).
 *
 * The legacy "one module server, many services" pattern: this binary owns NO
 * business logic. Each of cl's 30 online services now lives in its own file
 * (app/src/cl/svc/<SVCNAME>.pgc) with its own copybook header
 * (app/src/cl/svc/<SVCNAME>.h); those objects are archived into libclsvc.a and
 * linked into this server. Here we only:
 *   - include every service copybook (for the extern prototypes),
 *   - tpopen() the ECPG XA RM,
 *   - tpadvertise() all services from a {name, fn} table,
 *   - tpclose() at shutdown.
 *
 * 마감 도메인: 일/월/분기/연 마감 이력(close_log), 가맹점별 마감 스냅샷(snapshot),
 * 기간 잠금(period_lock). 마감 상태 O=개시 -> C=마감완료 -> A=승인 -> L=잠금,
 * 취소 X / 재마감 R.
 *
 * XA: the connection is opened once by tpopen() (ECPG XA switch libndrxxaecpg.so);
 * there is NO EXEC SQL CONNECT. Each tpcall runs on this process's XA branch.
 */
#include <atmi.h>
#include <ubf.h>
#include <userlog.h>

/* every cl service's copybook header (declares its record struct + prototype) */
#include "CL_DAILY.h"
#include "CL_MONTHLY.h"
#include "CL_QUARTER.h"
#include "CL_SNAPSHOT.h"
#include "CL_RECLOSE.h"
#include "CL_VERIFY.h"
#include "CL_CANCEL.h"
#include "CL_STATS.h"
#include "CL_BALVERIFY.h"
#include "CL_SEQ.h"
#include "CL_REPORT.h"
#include "CL_ALERT.h"
#include "CL_UNCLOSED.h"
#include "CL_APPROVE.h"
#include "CL_LOCK.h"
#include "CL_UNLOCK.h"
#include "CL_AGGREGATE.h"
#include "CL_DIFFCHK.h"
#include "CL_SUMMARY.h"
#include "CL_STATUS.h"
#include "CL_ROLLBACK.h"
#include "CL_CONFIRM.h"
#include "CL_PERIOD.h"
#include "CL_YEAREND.h"
#include "CL_MIDCLOSE.h"
#include "CL_RECON.h"
#include "CL_ARCHIVE.h"
#include "CL_PURGE.h"
#include "CL_RESTORE.h"
#include "CL_INQUIRY.h"

/* ----- advertise table: {service name, function} (fn is extern from its file) */
static struct { const char *name; void (*fn)(TPSVCINFO *); } SVCS[] = {
    {"CL_DAILY", CL_DAILY},         {"CL_MONTHLY", CL_MONTHLY},
    {"CL_QUARTER", CL_QUARTER},     {"CL_SNAPSHOT", CL_SNAPSHOT},
    {"CL_RECLOSE", CL_RECLOSE},     {"CL_VERIFY", CL_VERIFY},
    {"CL_CANCEL", CL_CANCEL},       {"CL_STATS", CL_STATS},
    {"CL_BALVERIFY", CL_BALVERIFY}, {"CL_SEQ", CL_SEQ},
    {"CL_REPORT", CL_REPORT},       {"CL_ALERT", CL_ALERT},
    {"CL_UNCLOSED", CL_UNCLOSED},   {"CL_APPROVE", CL_APPROVE},
    {"CL_LOCK", CL_LOCK},           {"CL_UNLOCK", CL_UNLOCK},
    {"CL_AGGREGATE", CL_AGGREGATE}, {"CL_DIFFCHK", CL_DIFFCHK},
    {"CL_SUMMARY", CL_SUMMARY},     {"CL_STATUS", CL_STATUS},
    {"CL_ROLLBACK", CL_ROLLBACK},   {"CL_CONFIRM", CL_CONFIRM},
    {"CL_PERIOD", CL_PERIOD},       {"CL_YEAREND", CL_YEAREND},
    {"CL_MIDCLOSE", CL_MIDCLOSE},   {"CL_RECON", CL_RECON},
    {"CL_ARCHIVE", CL_ARCHIVE},     {"CL_PURGE", CL_PURGE},
    {"CL_RESTORE", CL_RESTORE},     {"CL_INQUIRY", CL_INQUIRY},
    {NULL, NULL}
};

int tpsvrinit(int argc, char **argv)
{
    int i;
    if (tpopen() < 0) {
        userlog("cl_svr: tpopen FAIL: %s", tpstrerror(tperrno));
        return -1;
    }
    for (i = 0; SVCS[i].name != NULL; i++) {
        if (tpadvertise((char *)SVCS[i].name, SVCS[i].fn) < 0) {
            userlog("cl_svr: tpadvertise(%s) FAIL: %s", SVCS[i].name, tpstrerror(tperrno));
            return -1;
        }
    }
    userlog("cl_svr: %d cl 서비스 광고 완료, RM 개방", i);
    return 0;
}

void tpsvrdone(void)
{
    tpclose();
    userlog("cl_svr: tpsvrdone");
}

/* vim: set ts=4 sw=4 et smartindent: */
