#!/sbin/openrc-run

name="AppDynamics Database Agent"
description="AppDynamics Database Agent for database performance monitoring"

: ${DB_AGENT_HOME:="/opt/appdynamics/db-agent"}
: ${DB_AGENT_USER:="appdynamics"}
: ${DB_AGENT_GROUP:="appdynamics"}
: ${DB_AGENT_LOG_DIR:="/var/log/appdynamics/db-agent"}

# ✅ Entrypoint matches Docker image: appdynamics/db-agent:latest
command="/bin/busybox"
command_args="sh ${DB_AGENT_HOME}/start-appdynamics-database-monitoring"
command_user="${DB_AGENT_USER}:${DB_AGENT_GROUP}"
command_background=true
pidfile="/run/${RC_SVCNAME}.pid"
directory="${DB_AGENT_HOME}"

output_log="${DB_AGENT_LOG_DIR}/stdout.log"
error_log="${DB_AGENT_LOG_DIR}/stderr.log"

depend() {
	need net
	after firewall
}

start_pre() {
	checkpath -d -o ${DB_AGENT_USER}:${DB_AGENT_GROUP} -m 0755 \
		"${DB_AGENT_LOG_DIR}"
	checkpath -d -o ${DB_AGENT_USER}:${DB_AGENT_GROUP} -m 0755 \
		"${DB_AGENT_HOME}/logs"

	if [ ! -f "${DB_AGENT_HOME}/db-agent.jar" ]; then
		eerror "db-agent.jar not found in ${DB_AGENT_HOME}"
		return 1
	fi

	if [ ! -f "${DB_AGENT_HOME}/start-appdynamics-database-monitoring" ]; then
		eerror "Entrypoint script not found!"
		return 1
	fi
}
