#!/bin/bash
# Filename:      /usr/share/ngcp-ngcpcfg/functions/init
# Purpose:       helper functions to init ngcpcfg global variables
################################################################################

## important variables we depend on to operate successfully {{{
# support test suite which requires system independent configuration
if [ -r "${NGCPCFG:-}" ] ; then
  log_debug "sourcing configuration file ${NGCPCFG:-}"
  # shellcheck disable=SC1090
  . "${NGCPCFG:-}"
else
  if [ -r /etc/ngcp-ngcpcfg/ngcpcfg.cfg ] ; then
    # shellcheck disable=SC1091
    . /etc/ngcp-ngcpcfg/ngcpcfg.cfg
    log_debug "sourced configuration file /etc/ngcp-ngcpcfg/ngcpcfg.cfg"

    if [ -d /etc/ngcp-ngcpcfg/ngcpcfg.d ] ; then
      for file in /etc/ngcp-ngcpcfg/ngcpcfg.d/*.cfg ; do
        if [ -r "${file}" ]; then
          # shellcheck disable=SC1090
          . "${file}"
          log_debug "sourced configuration file '${file}'"
        fi
      done
    fi
  elif [ -r /etc/ngcp-config.tgz.pgp ] ||
       [ -r /etc/ngcp-config-crypted.tgz.gpg ] ; then
    log_error "Configuration pool locked. Please contact your distributor. Exiting."
    exit 1
  else
    log_error "Could not read configuration file /etc/ngcp-ngcpcfg/ngcpcfg.cfg. Exiting."
    exit 1
  fi
fi

if ! [ -r "${NGCPCTL_CONFIG}" ] ; then
  log_error "Configuration file ${NGCPCTL_CONFIG} does not exist (unconfigured?) - exiting."
  exit 1
fi

if ! [ -r "${CONSTANTS_CONFIG}" ] ; then
  log_error "Constants file ${CONSTANTS_CONFIG} does not exist (unconfigured?) - exiting."
  exit 1
fi

if ! [ -r "${MAINTENANCE_CONFIG}" ] ; then
  log_error "Maintenance file ${MAINTENANCE_CONFIG} does not exist (unconfigured?) - exiting."
  exit 1
fi

if [ -z "${NETWORK_CONFIG:-}" ] ; then
  log_warn "NETWORK_CONFIG is not configured in ${NGCPCTL_CONFIG} - continuing anyway."
elif ! [ -r "${NETWORK_CONFIG}" ] ; then
  log_error "Constants file ${NETWORK_CONFIG} does not exist (unconfigured?) - exiting."
  exit 1
fi

if ! [ -d "${TEMPLATE_POOL_BASE}" ] ; then
  log_error "No template directory (${TEMPLATE_POOL_BASE}) found - exiting."
  exit 1
fi

if [ -d "${EXTRA_CONFIG_DIR:-}" ] && ls "${EXTRA_CONFIG_DIR}"/*.yml &>/dev/null ; then
  log_debug "EXTRA_CONFIG_DIR is configured and *.yml files are present, setting EXTRA_CONFIG_FILES"
  EXTRA_CONFIG_FILES=("${EXTRA_CONFIG_DIR}"/*.yml)
fi

## }}}

## environment variables {{{
export PN="ngcpcfg"
export HNAME
HNAME="$(ngcp-hostname)"
export NNAME
NNAME="$(ngcp-nodename)"

# avoid warnings by perl script complaining about locales
export LANG=C
export LC_ALL=C

# make sure it's available in all helper scripts
[ -n "${DEBUG:-}" ] && export DEBUG
[ -n "${NO_DB_SYNC:-}" ] && export NO_DB_SYNC

: "${CONSTANTS_CONFIG_USER:=root}"
: "${CONSTANTS_CONFIG_GROUP:=root}"
: "${CONSTANTS_CONFIG_CHMOD:=0600}"
: "${CONFIG_USER:=root}"
: "${CONFIG_GROUP:=_ngcp-admin}"
: "${CONFIG_CHMOD:=0660}"
: "${MAINTENANCE_CONFIG_USER:=root}"
: "${MAINTENANCE_CONFIG_GROUP:=_ngcp-admin}"
: "${MAINTENANCE_CONFIG_CHMOD:=0660}"
: "${NETWORK_CONFIG_USER:=root}"
: "${NETWORK_CONFIG_GROUP:=_ngcp-admin}"
: "${NETWORK_CONFIG_CHMOD:=0660}"

# export for access via build_config etc
export CONFIG_POOL
export HOST_CONFIG
export LOCAL_CONFIG
export SITES_CONFIG
export SITES_DIR
export NGCPCTL_CONFIG
export CONSTANTS_CONFIG
export MAINTENANCE_CONFIG
export NETWORK_CONFIG
export EXTRA_CONFIG_DIR
export EXTRA_CONFIG_FILES
## }}}
