#!/bin/sh -efu

ZBX_DBNAME="${ZBX_DBNAME:-zabbix}"
ZBX_DBUSER="${ZBX_DBUSER:-zabbix}"
ZBX_DBPASS="${ZBX_DBPASS:-zabbix}"
ZBX_SRVCONF="${ZBX_SRVCONF:-/etc/zabbix/zabbix_server.conf}"
ZBX_FRONTCONF="${ZBX_FRONTCONF:-/var/www/webapps/zabbix/frontends/php/conf/zabbix.conf.php}"
ZBX_AGENTCONF="${ZBX_AGENTCONF:-/etc/zabbix/zabbix_agentd.conf}"

. shell-config

# Gets a named value from the default or specifeid confiuration file.
# args: default-file delim [file] val-name
def_get() {
	if [ $# -gt 3 ]; then
		shell_config_get "$3" "$4" "$2"
	else
		shell_config_get "$1" "$3" "$2"
	fi
}

# Sets a named value in the default or specified configuration file.
# args: default-file rdelim wdelim [file] name value
def_set() {
	if [ $# -gt 5 ]; then
		shell_config_set "$4" "$5" "$6" "$2" "$3"
	else
		shell_config_set "$1" "$4" "$5" "$2" "$3"
	fi
}

# Comments a named value in the default or specifeid confiuration file.
# args: default-file delim [file] val-name
def_comment() {
	if [ $# -gt 3 ]; then
		shell_config_comment "$3" "$4" "$2"
	else
		shell_config_comment "$1" "$3" "$2"
	fi
}

# Deletes a named value from the default or specifeid confiuration file.
# args: default-file delim [file] val-name
def_delete() {
	if [ $# -gt 3 ]; then
		shell_config_del "$3" "$4" "$2"
	else
		shell_config_del "$1" "$3" "$2"
	fi
}

# Gets a named value from the Zabbix server default or specified
# configuration file.
# args: [file] name
zbxsrv_get() {
	def_get "$ZBX_SRVCONF" '=' "$@"
}

# Sets a named value in the Zabbix server default or specified
# configuration file.
# args: [file] name value
zbxsrv_set() {
	def_set "$ZBX_SRVCONF" '=' '=' "$@"
}

# Comments a named value in the Zabbix server default or specified
# configuration file.
# args: [file] name
zbxsrv_comment() {
	def_comment "$ZBX_SRVCONF" '=' "$@"
}

# Deletes a named value from the Zabbix server default or specified
# configuration file.
# args: [file] name
zbxsrv_delete() {
	def_delete "$ZBX_SRVCONF" '=' "$@"
}

# Gets a named value from the Zabbix frontend default or specified
# configuration file.
# args: [file] name
zbxfront_get() {
	def_get "$ZBX_FRONTCONF" '[[:space:]]*=[[:space:]]*' "$@"
}

# Sets a named value in the Zabbix frontend default or specified
# configuration file.
# args: [file] name value
zbxfront_set() {
	def_set "$ZBX_FRONTCONF" '[[:space:]]*=[[:space:]]*' ' = ' "$@"
}

# Comments a named value in the Zabbix frontend default or specified
# configuration file.
# args: [file] name
zbxfront_comment() {
	def_comment "$ZBX_FRONTCONF" '[[:space:]]*=[[:space:]]*' "$@"
}

# Deletes a named value from the Zabbix frontend default or specified
# configuration file.
# args: [file] name
zbxfront_delete() {
	def_delete "$ZBX_FRONTCONF" '[[:space:]]*=[[:space:]]*' "$@"
}

# Gets a named value from the Zabbix agent default or specified
# configuration file.
# args: [file] name
zbxagent_get() {
	def_get "$ZBX_AGENTCONF" '=' "$@"
}

# Sets a named value in the Zabbix agent default or specified
# configuration file.
# args: [file] name value
zbxagent_set() {
	def_set "$ZBX_AGENTCONF" '=' '=' "$@"
}

# Comments a named value in the Zabbix agent default or specified
# configuration file.
# args: [file] name
zbxagent_comment() {
	def_comment "$ZBX_AGENTCONF" '=' "$@"
}

# Deletes a named value from the Zabbix agent default or specified
# configuration file.
# args: [file] name
zbxagent_delete() {
	def_delete "$ZBX_AGENTCONF" '=' "$@"
}
