#!/bin/bash

app_id="prometheus-alertmanager"

# make alertmanager port available on localhost only
ucr set appcenter/apps/$app_id/docker/params='--network host'

mkdir -p /var/lib/univention-appcenter/apps/$app_id/data/
mkdir -p /var/lib/univention-appcenter/apps/$app_id/conf/

# add a working default config
# it will be replaced with the real config in configure_host
cat <<EOF > /var/lib/univention-appcenter/apps/$app_id/conf/alertmanager.yml
route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: 'web.hook'
receivers:
  - name: 'web.hook'
    webhook_configs:
      - url: 'http://127.0.0.1:5001/'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']
EOF

chmod 700 /var/lib/univention-appcenter/apps/$app_id/  # important! "nobody" must not reach the conf/ directory
chown 0:0 /var/lib/univention-appcenter/apps/$app_id/  # important! "nobody" must not reach the conf/ directory
chmod -R 750 /var/lib/univention-appcenter/apps/$app_id/conf/
chown -R 65534:0 /var/lib/univention-appcenter/apps/$app_id/conf/ # nobody user in container
chmod -R 750 /var/lib/univention-appcenter/apps/$app_id/data/
chown -R 65534:0 /var/lib/univention-appcenter/apps/$app_id/data/

exit 0
