# Wazuh agent. The sources unpack side by side in $B/src: Wazuh itself,
# its dependency bundles (each into its own directory, as `make deps`
# would put them under src/external) and wazuh-http-request (which `make
# deps` would put under src/shared_modules/http-request). The patches are
# relative to $B/src for that reason.
#
# The agent is installed by Wazuh's own install.sh into a staging tree,
# with install(1) replaced by everest/install-shim (no wazuh user exists
# in the build; the owners it asked for are recorded instead). The
# package ships that tree, untouched, as PREFIX/lib/wazuh-agent/ossec;
# wazuh-agent-setup copies it to /var/ossec, the one directory the agent
# runs from (it writes its state there and chroots into it).

W=$B/src/wazuh-$VER

prepare() {
	mkdir -p "$W/src/external"
	for d in "$B"/src/*/; do
		d=${d%/}
		case ${d##*/} in
		wazuh-$VER) ;;
		wazuh-http-request-*)
			rmdir "$W/src/shared_modules/http-request" &&
				mv "$d" "$W/src/shared_modules/http-request" || return 1 ;;
		*) mv "$d" "$W/src/external/" || return 1 ;;
		esac
	done
}

# TARGET=agent, dependencies from the bundles only. Left out, so FIM has
# no "whodata" (who changed a file); its realtime mode (inotify) and
# scheduled scans work:
#   - libbpf-bootstrap (eBPF whodata): it clones libbpf, bpftool and
#     vmlinux.h from GitHub at build time and needs clang;
#   - audit-userspace (USE_AUDIT=no; audit whodata): it needs autoconf
#     and automake, and Everest has no auditd for it to talk to.
# CHECK_ALPINE is Wazuh's musl switch (its build knows musl as Alpine).
wazuh_make() {
	set -- TARGET=agent EXTERNAL_SRC_ONLY=yes LIBBPF_LIB= USE_AUDIT=no INSTALLDIR=/var/ossec "$@"
	[ "$LIBC" = musl ] && set -- CHECK_ALPINE=YES "$@"
	# Not the driver's DESTDIR and PREFIX: the dependencies' own `make
	# install`s (dbus) would put their files in the package.
	env -u DESTDIR -u PREFIX make -C "$W/src" "$@"
}

compile() {
	# OpenSSL first: pacman's and rpm's builds include its headers
	# without depending on it, and race it under -j.
	wazuh_make external/openssl/libssl.a && wazuh_make build
}

package() {
	stage=$B/ossec
	perms=$B/permissions
	: > "$perms"
	mkdir -p "$B/shim"
	install -m 755 "$W/everest/install-shim" "$B/shim/install"
	# Users are wazuh-agent-setup's job, on the system.
	printf '#!/bin/sh\nexit 0\n' > "$W/src/init/adduser.sh"
	(
		cd "$W" &&
		PATH=$B/shim:$PATH WAZUH_STAGE=$stage WAZUH_PERMS=$perms \
		USER_LANGUAGE=en USER_NO_STOP=y USER_INSTALL_TYPE=agent \
		USER_DIR=$stage USER_DELETE_DIR=y USER_BINARYINSTALL=y \
		USER_ENABLE_ACTIVE_RESPONSE=y USER_ENABLE_SYSCHECK=y \
		USER_ENABLE_ROOTCHECK=y USER_ENABLE_SYSCOLLECTOR=y \
		USER_ENABLE_SCA=y USER_UPDATE=n USER_AUTO_START=n \
		USER_AGENT_SERVER_IP=MANAGER_IP \
		env -u DESTDIR -u PREFIX ./install.sh
	) || return 1
	[ -x "$stage/bin/wazuh-agentd" ] || { echo "install.sh installed no agent" >&2; return 1; }
	# install.sh writes its directory into ossec.conf (the active
	# responses' log): the agent's is /var/ossec.
	sed -i "s|$stage/|/var/ossec/|g" "$stage/etc/ossec.conf" || return 1
	if grep -rl "$stage" "$stage"; then
		echo "the staging directory is named in the files above" >&2
		return 1
	fi
	# As Wazuh's own packages: no preinstalled logs.
	rm -f "$stage"/logs/*.log "$stage"/logs/*.json

	L=$DESTDIR$PREFIX/lib/wazuh-agent
	mkdir -p "$L" "$DESTDIR$PREFIX/bin"
	mv "$stage" "$L/ossec"
	install -m 644 "$perms" "$L/permissions"
	install -m 755 "$W/everest/service" "$L/service"
	install -m 755 "$W/everest/openrc" "$L/openrc"
	install -m 755 "$W/everest/runit" "$L/runit"
	install -m 755 "$W/everest/wazuh-agent-setup" "$DESTDIR$PREFIX/bin/wazuh-agent-setup"
}
