#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/packaging/deb/control/postinst
# Provenance-includes-license: Apache-2.0
# Provenance-includes-copyright: The Cortex Authors.

set -e

# shellcheck disable=1091
[ -f /etc/sysconfig/mimir ] && . /etc/default/mimir

case "$1" in
  configure)
    [ -z "$MIMIR_USER" ] && MIMIR_USER="mimir"
    [ -z "$MIMIR_GROUP" ] && MIMIR_GROUP="mimir"
    if ! getent group "$MIMIR_GROUP" >/dev/null 2>&1; then
      groupadd -r "$MIMIR_GROUP"
    fi
    if ! getent passwd "$MIMIR_USER" >/dev/null 2>&1; then
      useradd -m -r -g mimir -d /var/lib/mimir -s /sbin/nologin -c "mimir user" mimir
    fi

    chmod 640 /etc/mimir/config.example.yaml
    chown root:$MIMIR_GROUP /etc/mimir/config.example.yaml

    if [ -z ${2+x} ] && [ "$RESTART_ON_UPGRADE" = "true" ]; then
      if command -v systemctl 2>/dev/null; then
        systemctl daemon-reload
      fi
    fi
    ;;
esac
