#!/bin/bash
# Filename:      /usr/share/ngcp-ngcpcfg/helper/build_config
# Purpose:       builds output configuration file based on tt2 template file
#                using /usr/share/ngcp-ngcpcfg/helper/tt2-wrapper
################################################################################

set -e
set -u

if [ "${#:-}" -ne 1 ] ; then
  echo "Usage: /usr/share/ngcp-ngcpcfg/helper/build_config <input_file>" >&2
  exit 1
fi

# support for testsuite, assume defaults if unset
CONFIG_POOL="${CONFIG_POOL:-/etc}"
FUNCTIONS="${FUNCTIONS:-/usr/share/ngcp-ngcpcfg/functions/}"
HELPER="${HELPER:-/usr/share/ngcp-ngcpcfg/helper/}"

if ! [ -r "${FUNCTIONS}"/main ] ; then
  printf "Error: %s/main could not be read. Exiting.\n" "${FUNCTIONS}" >&2
  exit 1
fi

. "${FUNCTIONS}"/main

# main script

input_file="$1"       # like /etc/ngcp-config/templates/etc/mysql/my.cnf.tt2
# calculate output file {{{
  x=${input_file##${NGCPCTL_MAIN}/templates/}         # drop leading /etc/ngcp-config
  y=${x%%.tt2}                                        # drop trailing suffix '.tt2'
  [ -n "${HA_FILE:-}" ]   && y=${y%%.tt2${HA_FILE}}   # drop trailing suffix '.tt2.sp{1,2}'
  [ -n "${PAIR_FILE:-}" ] && y=${y%%.tt2${PAIR_FILE}} # drop trailing suffix '.tt2.pairname'
  [ -n "${HOST_FILE:-}" ] && y=${y%%.tt2${HOST_FILE}} # drop trailing suffix '.tt2.hostname'
  y=${y%%.customtt}                                   # drop trailing suffix '.customtt'
# export variable for usage within {pre,post}build scripts,
# OUTPUT_DIRECTORY is for customization during testing
if [ -n "${OUTPUT_DIRECTORY:-}" ] ; then
  log_debug "Using output directory $OUTPUT_DIRECTORY"
  export output_file="${OUTPUT_DIRECTORY}/${y}"
else
  export output_file="${y}"
fi
# }}}

# ensure we don't try to generate a file where a directory with same name exists already
if [ -d "${output_file}" ] ; then
  log_error "Generating file ${output_file} not possible, it's an existing directory." >&2
  exit 1
fi

# pre-execution script in template store:
if [ -r "${NGCPCTL_MAIN}/templates/${output_file}.prebuild" ] ; then
  log_info "Executing prebuild for ${output_file}"
  bash "${NGCPCTL_MAIN}/templates/${output_file}.prebuild"
elif [ -r "${NGCPCTL_MAIN}/templates/$(dirname "${output_file}")/ngcpcfg.prebuild" ] ; then
  log_info "Executing prebuild for ${output_file}"
  bash "${NGCPCTL_MAIN}/templates/$(dirname "${output_file}")/ngcpcfg.prebuild"
fi

# if output directory does not exist yet, create it
if ! [ -d "$(dirname "${output_file}")" ] ; then
  umask 0022 # directory permissions should be '755'
  mkdir -p "$(dirname "${output_file}")"
fi

# assume safe defaults
umask 0077

# read host specific configuration file only if it exists
[ -r "${HOST_CONFIG:-}" ] && host_conf="$HOST_CONFIG"

# read local config only if it exists
[ -r "${LOCAL_CONFIG:-}" ] && local_conf="$LOCAL_CONFIG"

TT_WRAPPER="${HELPER}/tt2-wrapper"

log_debug "Output file ${output_file} based on ${input_file}"
log_debug "Executing: $TT_WRAPPER ${input_file} > ${output_file}"
if "$TT_WRAPPER" "${input_file}" > "${output_file}" 2>/dev/null ; then
  log_info "Generating ${output_file}: OK"
  RC=0
else
  log_error "Generating ${output_file} based on ${input_file}: FAILED"
  RC=1

  log_info "NOTE: Check those files for valid syntax and encoding:"
  for f in "${input_file}" ${host_conf:-} ${local_conf:-} "$NGCPCTL_CONFIG" "${NETWORK_CONFIG:-}" ${EXTRA_CONFIG_FILES:-} "$CONSTANTS_CONFIG" ; do
    [ -r "$f" ] && log_info "$f"
  done
  log_info "Running /usr/share/ngcp-ngcpcfg/helper/tt2-wrapper <file> should provide more details."

fi

# set permissions for generated config based on the ones of the template
chmod --reference="${input_file}" "${output_file}"
# finally drop all write permissions
chmod a-w "${output_file}"

# post-execution script in template store:
if [ -r "${NGCPCTL_MAIN}/templates/${output_file}.postbuild" ] ; then
  log_info "Executing postbuild for ${output_file}"
  bash "${NGCPCTL_MAIN}/templates/${output_file}.postbuild"
elif [ -r "${NGCPCTL_MAIN}/templates/$(dirname "${output_file}")/ngcpcfg.postbuild" ] ; then
  log_info "Executing postbuild for ${output_file}"
  bash "${NGCPCTL_MAIN}/templates/$(dirname "${output_file}")/ngcpcfg.postbuild"
fi

exit $RC

## END OF FILE #################################################################
