#!/bin/sh -e

##### HARD-CODED CONFIG #####################################################

CONF_DIR=/etc/prometheus
CONF=$CONF_DIR/alertmanager.yml

##### GET SETTINGS ##########################################################

config="$(cat "$__object/parameter/config")"
retention_days="$(cat "$__object/parameter/retention-days")"
storage_path="$(cat "$__object/parameter/storage-path")"
# listen_address="$(cat "$__object/parameter/listen-address")"

##### INSTALL THE PACKAGE ###################################################

require_pkg=""  # what to require if I want to require "the package"
require=""
if [ -f "$__object/parameter/install-from-backports" ]; then
	os=$(cat "$__global/explorer/os")
	os_version=$(cat "$__global/explorer/os_version")

	case $os in
		devuan)
			[ "$os_version" = "ascii/ceres" ] && os_version='ascii'  # "ascii" used in the repo URLs
			__apt_source backports --uri http://auto.mirror.devuan.org/merged --distribution $os_version-backports --component main
			require="$require __apt_source/backports" __package_apt prometheus-alertmanager --target-release $os_version-backports
			require_pkg="__package_apt/prometheus-alertmanager"
		;;
		*)
			echo "--install-from-backports is only supported on Devuan -- ignoring." >&2
			echo "Send a pull request if you require it." >&2
		;;
	esac
else
	__package prometheus-alertmanager
	require_pkg="__package/prometheus-alertmanager"
fi

##### PREPARE PATHS AND SUCH ################################################

require="$require $require_pkg" __directory "$storage_path" --owner prometheus --parents

# TODO this is a bug in the init script, patching it like this is awful and it should be reported
require="$require $require_pkg" \
__key_value alertmanager_fix_init_script --file /etc/init.d/prometheus-alertmanager \
	--key "NAME" --value "prometheus-alertmanager" --delimiter "=" \
	--onchange "service prometheus-alertmanager restart"

##### CONFIGURE #############################################################

FLAGS="--storage.path $storage_path --data.retention $((retention_days*24))h --web.listen-address [::]:9093"

require="$require $require_pkg" \
__key_value alertmanager_args --file /etc/default/prometheus-alertmanager \
	--key "ARGS" --value "\"$FLAGS\"" --delimiter "=" \
	--onchange "service prometheus-alertmanager restart"

require="$require __directory/$storage_path $require_pkg" \
__config_file $CONF \
	--source "$config" \
	--group prometheus --mode 640 \
	--onchange "service prometheus-alertmanager reload"  # TODO when a config-check tool is available, check config here

