#!/bin/sh
# Creates the runtime directory that the CONFIGURATION asks for.
#
# Debian ships a tmpfiles snippet for this ("d /run/zabbix 0755 zabbix
# zabbix"), and systemd-tmpfiles does not run under s6. The path is not fixed,
# though: it follows from PidFile in zabbix_proxy.conf, which is managed by
# Puppet -- on zabbixproxy-1.crb2 it says /var/run/zabbix_proxy, in the
# Ubuntu package /run/zabbix (both checked on 23.09.2026). That is why the
# path is READ and not assumed; the default only applies when nothing is
# set there.
#
# run-script-daemon would create /run/zabbix_proxy (basename of the DAEMON) --
# which happens to match the crb2 configuration and not the Ubuntu default.
# Exactly this kind of coincidence has already hit twice with mariadb/pdns.

set -u

CONF=${ZABBIX_PROXY_CONF:-/etc/zabbix/zabbix_proxy.conf}
DEFAULT_DIR=/run/zabbix

dir=''
if [ -r "$CONF" ]; then
    pidfile=$(sed -n 's/^[[:space:]]*PidFile=[[:space:]]*//p' "$CONF" 2>/dev/null | tail -n1)
    [ -n "${pidfile:-}" ] && dir=$(dirname "$pidfile")
fi
[ -n "$dir" ] || dir=$DEFAULT_DIR

# SocketDir may ask for a second directory; if it is set, it applies
# additionally (the runtime control socket lives there).
sockdir=''
if [ -r "$CONF" ]; then
    sockdir=$(sed -n 's/^[[:space:]]*SocketDir=[[:space:]]*//p' "$CONF" 2>/dev/null | tail -n1)
fi

for d in "$dir" ${sockdir:+"$sockdir"}; do
    case $d in
    /*) ;;
    *) continue ;;
    esac
    install -d -m 0755 -o zabbix -g zabbix "$d" || exit 1
done
exit 0
