#!/bin/bash
##
## Build (via the reusable build.sh) + boot the Enduro/X acquiring stack, then
## stay alive. Runs inside the app container (image acquire-x/endurox:7.0.12).
##
set -e

. /app/conf/setapp.sh

echo "=================================================================="
echo " acquire-core-x : build + boot (module-server pattern)"
echo "=================================================================="

# runtime dirs
mkdir -p /app/log /app/tmp /app/tmlogs/rm1 /app/bin /app/lib /app/obj
rm -f /app/log/*.log 2>/dev/null || true

# ensure posix mqueue is available (compose provides the sysctls/ulimits)
if [ ! -d /dev/mqueue ]; then
    mkdir -p /dev/mqueue
    mount -t mqueue none /dev/mqueue 2>/dev/null || true
fi

# ---------------------------------------------------------------------------
# Build everything + generate ndrxconfig.xml (discovers all modules)
# ---------------------------------------------------------------------------
bash /app/build.sh
ls -l /app/bin

# ---------------------------------------------------------------------------
# Boot Enduro/X
# ---------------------------------------------------------------------------
echo "--- xadmin down/start ---"
xadmin down -y 2>/dev/null || true
xadmin start -y

echo "--- xadmin psc (advertised services) ---"
xadmin psc
echo "--- xadmin ppm (processes) ---"
xadmin ppm

echo "--- acq_httpgw (업무화면 HTTP 게이트웨이 :8090 — 브라우저 → tpcall) ---"
nohup /app/bin/acq_httpgw > /app/log/acq_httpgw.log 2>&1 &
sleep 1
grep -q "listening" /app/log/acq_httpgw.log && echo "acq_httpgw: UP (:8090)" || tail -3 /app/log/acq_httpgw.log

echo "--- acq_extsw (외부 카드망 시뮬레이터 :9500 — ISO 8583 전문 왕복 상대망) ---"
nohup /app/bin/acq_extsw 9500 > /app/log/acq_extsw.log 2>&1 &
sleep 1
grep -q "listening" /app/log/acq_extsw.log && echo "acq_extsw: UP (:9500)" || tail -2 /app/log/acq_extsw.log

echo "=================================================================="
echo " acquire-core-x : UP. Run the 매입 chain driver with:"
echo "   docker compose -f docker/docker-compose.yml exec app /app/run-driver.sh"
echo " 업무화면(포털): http://localhost:8090/"
echo "=================================================================="

# keep the container (and the ndrxd daemon) alive
tail -F /app/log/ndrxd.log
