#
# Copyright (c) 2026 Oninit LLC. All rights reserved.
#
# Oninit Logwalker - runtime environment (/etc/logwalker/environment)
#
# Sourced by /usr/bin/logwalker before it exec's the real binary, to make
# the Informix CSDK ESQL/C runtime libraries (libthsql / libthos /
# libthgen / libthxa + libif*.so) discoverable at runtime. logwalker is
# dynamically linked against them; they live under $INFORMIXDIR, not in
# the system linker path.
#
# This file is config(noreplace) - your edits survive package upgrades.
#

# --- CSDK location -----------------------------------------------------
# Set CSDK_DIR to the Informix CSDK (or a full Informix install) that
# provides the ESQL/C libs under lib/esql/. logwalker runs locally on the
# source Informix server, so a CSDK is already present somewhere here.
CSDK_DIR=/opt/informix

# If libthsql.so is ALREADY resolvable on LD_LIBRARY_PATH (e.g. the
# informix user's profile already set up the CSDK), there's nothing to do
# — leave the environment exactly as the caller has it.
lw_have=
lw_oifs=$IFS; IFS=:
for lw_d in ${LD_LIBRARY_PATH:-}; do
	[ -n "$lw_d" ] && [ -e "$lw_d/libthsql.so" ] && { lw_have=1; break; }
done
IFS=$lw_oifs

if [ -z "$lw_have" ]; then
	# Resolve INFORMIXDIR deterministically for EVERY user (root,
	# informix, ...). The caller's own INFORMIXDIR is honoured ONLY if
	# it actually carries the ESQL/C libs; otherwise fall back to
	# CSDK_DIR above. This avoids the trap where the informix user's
	# profile sets INFORMIXDIR to the database server (no client libs),
	# shadowing the CSDK and giving "libthsql.so: cannot open shared
	# object file".
	if [ -z "${INFORMIXDIR:-}" ] || [ ! -e "$INFORMIXDIR/lib/esql/libthsql.so" ]; then
		INFORMIXDIR=$CSDK_DIR
	fi
	export INFORMIXDIR

	# If the CSDK ships its setup script, source it (sets INFORMIXSERVER,
	# PATH, LD_LIBRARY_PATH, etc.). Harmless if absent.
	[ -f "$INFORMIXDIR/bin/setcsdk" ] && . "$INFORMIXDIR/bin/setcsdk"

	# Ensure the CSDK runtime dirs are on the loader path.
	LD_LIBRARY_PATH="$INFORMIXDIR/lib:$INFORMIXDIR/lib/esql${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
	export LD_LIBRARY_PATH
fi
unset lw_have lw_d lw_oifs
