/*
 * ac_svr.pgc - ac (매입) MODULE SERVER (thin dispatcher).
 *
 * The legacy "one module server, many services" pattern: this binary owns NO
 * business logic. Each of ac's ~30 online services now lives in its own file
 * (app/src/ac/svc/<SVCNAME>.pgc) with its own copybook header
 * (app/src/ac/svc/<SVCNAME>.h); those objects are archived into libacsvc.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.
 *
 * 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 (see
 * the ACQUIRE->RECONCILE->SETTLE chain, which crosses ac_svr/rc_svr/st_svr).
 */
#include <atmi.h>
#include <ubf.h>
#include <userlog.h>

/* every ac service's copybook header (declares its record struct + prototype) */
#include "ACQUIRE.h"
#include "ACQ_DDC.h"
#include "ACQ_EDI.h"
#include "ACQ_EDC.h"
#include "ACQ_CANCEL.h"
#include "ACQ_CORRECT.h"
#include "ACQ_PARTIAL.h"
#include "ACQ_INSTALL.h"
#include "ACQ_FOREIGN.h"
#include "ACQ_UNMATCH.h"
#include "ACQ_REPROC.h"
#include "ACQ_DUPCHK.h"
#include "ACQ_LIMITCHK.h"
#include "ACQ_FEEADJ.h"
#include "ACQ_STATUS.h"
#include "ACQ_MERCHSUM.h"
#include "ACQ_ISSUERCLS.h"
#include "ACQ_WHT.h"
#include "ACQ_TAXINV.h"
#include "ACQ_APPRLINK.h"
#include "ACQ_DATECHG.h"
#include "ACQ_AMTFIX.h"
#include "ACQ_RECLASS.h"
#include "ACQ_XFERLINK.h"
#include "ACQ_APPRMAP.h"
#include "ACQ_SETTLELINK.h"
#include "ACQ_HOLD.h"
#include "ACQ_RELEASE.h"
#include "ACQ_VOID.h"
#include "ACQ_REFUND.h"

/* ----- advertise table: {service name, function} (fn is extern from its file) */
static struct { const char *name; void (*fn)(TPSVCINFO *); } SVCS[] = {
    {"ACQUIRE", ACQUIRE},           {"ACQ_DDC", ACQ_DDC},
    {"ACQ_EDI", ACQ_EDI},           {"ACQ_EDC", ACQ_EDC},
    {"ACQ_CANCEL", ACQ_CANCEL},     {"ACQ_CORRECT", ACQ_CORRECT},
    {"ACQ_PARTIAL", ACQ_PARTIAL},   {"ACQ_INSTALL", ACQ_INSTALL},
    {"ACQ_FOREIGN", ACQ_FOREIGN},   {"ACQ_UNMATCH", ACQ_UNMATCH},
    {"ACQ_REPROC", ACQ_REPROC},     {"ACQ_DUPCHK", ACQ_DUPCHK},
    {"ACQ_LIMITCHK", ACQ_LIMITCHK}, {"ACQ_FEEADJ", ACQ_FEEADJ},
    {"ACQ_STATUS", ACQ_STATUS},     {"ACQ_MERCHSUM", ACQ_MERCHSUM},
    {"ACQ_ISSUERCLS", ACQ_ISSUERCLS},{"ACQ_WHT", ACQ_WHT},
    {"ACQ_TAXINV", ACQ_TAXINV},     {"ACQ_APPRLINK", ACQ_APPRLINK},
    {"ACQ_DATECHG", ACQ_DATECHG},   {"ACQ_AMTFIX", ACQ_AMTFIX},
    {"ACQ_RECLASS", ACQ_RECLASS},   {"ACQ_XFERLINK", ACQ_XFERLINK},
    {"ACQ_APPRMAP", ACQ_APPRMAP},   {"ACQ_SETTLELINK", ACQ_SETTLELINK},
    {"ACQ_HOLD", ACQ_HOLD},         {"ACQ_RELEASE", ACQ_RELEASE},
    {"ACQ_VOID", ACQ_VOID},         {"ACQ_REFUND", ACQ_REFUND},
    {NULL, NULL}
};

int tpsvrinit(int argc, char **argv)
{
    int i;
    if (tpopen() < 0) {
        userlog("ac_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("ac_svr: tpadvertise(%s) FAIL: %s", SVCS[i].name, tpstrerror(tperrno));
            return -1;
        }
    }
    userlog("ac_svr: %d ac services advertised, RM opened", i);
    return 0;
}

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

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