#!/bin/bash
# Purpose: search for existing .tt2 (template toolkit) files
#          and generate configuration files based on ngcpcfg's config
################################################################################

set -e
set -u

# support testsuite
FUNCTIONS="${FUNCTIONS:-/usr/share/ngcp-ngcpcfg/functions/}"
HELPER="${HELPER:-/usr/share/ngcp-ngcpcfg/helper/}"
SCRIPTS="${SCRIPTS:-/usr/share/ngcp-ngcpcfg/scripts/}"

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

# shellcheck disable=SC1090
. "${FUNCTIONS}"/main

MODIFIED_ONLY=false
check_args=()
while [ -n "${1:-}" ] ; do
  case "$1" in
    *--modified-only*) MODIFIED_ONLY=true ; shift ;;
    *--ignore-branch-check*) check_args+=( --ignore-branch-check ) ; shift ;;
    *--ignore-shared-storage-check*) check_args+=( --ignore-shared-storage-check ) ; shift ;;
    *) break ;;
  esac
done

# Sanity check the YAML and schema files.
# NOTE: we can't blindly pass "$@" to the check script,
#       so explicitly check and add option as needed
"${SCRIPTS}"/check "${check_args[@]}"

"${SCRIPTS}"/patch "$@"

declare -a ARGS

if [ -n "${NGCP_JOBS:-}" ] ; then
  ARGS+=("--jobs=${NGCP_JOBS}")
fi

for f in ${NGCPCTL_CONFIG:-} ${NODE_CONFIG:-} ${PAIR_CONFIG:-} ${HOST_CONFIG:-} ${LOCAL_CONFIG:-} ${MAINTENANCE_CONFIG:-} ${NETWORK_CONFIG:-} "${EXTRA_CONFIG_FILES[@]}" ${CONSTANTS_CONFIG:-} ; do
  ARGS+=("-c" "$f")
done

build_config_files_instances() {
  local info
  local source
  local target
  local instance
  local inst_cfg
  local rc=0

  if [ -z "${TEMPLATE_INSTANCES:-}" ] || [ ! -f "${TEMPLATE_INSTANCES}" ] ; then
    log_debug "no template info for instances, skip step"
    return
  fi

  log_debug "${RUNNER} ${HELPER}/instances-info $*"
  inst_cfg=$(mktemp -t ngcpcfg-build.XXXXXXXXXX )
  while IFS=":" read -r -a info; do
    source=${info[0]}
    target=${info[1]}
    instance=${info[2]}
    log_debug "source:${source} target:${target} instance:${instance}"

    log_debug "${RUNNER} ${HELPER}/instance-info ${instance} > ${inst_cfg}"
    ${RUNNER} "${HELPER}/instance-info" "${instance}" > "${inst_cfg}" || rc=$?
    if [ "${rc}" != "0" ]; then
      log_error "Failed to build instance info! Aborting."
      exit $rc
    fi

    log_debug "INSTANCE_NAME=${instance} ${RUNNER} ${HELPER}/tt2-process ${ARGS[*]} -c ${inst_cfg} -r ${source}:${target} ${source}"
    INSTANCE_NAME="${instance}" ${RUNNER} "${HELPER}/tt2-process" "${ARGS[@]}" -c "${inst_cfg}" -r "${source}:${target}" "${source}" || rc=$?
    if [ "${rc}" != "0" ]; then
      log_error "Instances build errors detected (see the output above)! Aborting."
      exit $rc
    fi
  done< <(${RUNNER} "${HELPER}/instances-info" "$@")
  rm -f "${inst_cfg}"
}

build_config_files() {
  RUNNER="nice -n 19 ionice -c 3"
  local rc=0

  log_debug "${RUNNER} ${HELPER}/tt2-process ${ARGS[*]} $*"
  ${RUNNER} "${HELPER}/tt2-process" "${ARGS[@]}" "$@" || rc=$?

  if [ "${rc}" != "0" ]; then
    log_error "Build errors detected (see the output above)! Aborting."
    exit $rc
  fi

  build_config_files_instances "$@"

  record_commit_id
}

# main script

if ! $MODIFIED_ONLY ; then
  build_config_files "$@"
else
  log_info "Considering modified files only due to --modified-only option."

  if is_git_clean ; then
    log_info "No changes found, nothing to do."
    exit 0
  fi

  trigger_rebuild=false

  for file in "${EXTRA_CONFIG_FILES[@]}" ; do
    if git diff-index --name-only HEAD | grep -qe -- "$(basename "$file")" ; then
      log_debug "modification in EXTRA_CONFIG_FILES file ${file} found"
      trigger_rebuild=true
      break # no reason for further checks
    fi
  done

  declare -a configs
  for config in "${NODE_CONFIG}" "${PAIR_CONFIG}"; do
    if [ -n "${config}" ]; then
      configs+=("-e" "$(basename "${config}")")
    fi
  done
  if git diff-index --name-only HEAD | grep -q \
         -e "$(basename "$NGCPCTL_CONFIG")" \
         "${configs[@]}" \
         -e "$(basename "$HOST_CONFIG")" \
         -e "$(basename "$LOCAL_CONFIG")" \
         -e "$(basename "$MAINTENANCE_CONFIG")" \
         -e "$(basename "$NETWORK_CONFIG")" \
         -e "$(basename "$CONSTANTS_CONFIG")" ; then
    log_debug "modification in main configuration file found"
    trigger_rebuild=true
  fi

  if $trigger_rebuild ; then
    log_info "Main configuration file(s) has been changed, running full rebuild."
    log_debug "Executing: build_config_files $*"
    build_config_files "$@"
  fi

  if git diff-index --name-only HEAD | grep -q templates/ || \
     git status --porcelain | awk '/^\?\? / {print $2}' 2>/dev/null | grep -q templates/ ; then
     log_debug "Template config changed, identifying files."
     for file in $(git diff-index --name-only HEAD) \
                 $(git status --porcelain | awk '/^\?\? / {print $2}') ; do
        build_file="${file##templates/}"
        build_file="${build_file%%.services}"
        build_file="${build_file%%.customtt}"
        build_file="${build_file%%.tt2}"
        build_file="/${build_file}"

        # generate file list
        case "${file_list:-}" in
          # avoid duplicates
          *"${build_file}"*) # do nothing
            ;;
          # append to file list
          *) file_list=" ${file_list:-} $build_file"
            ;;
        esac
     done

     log_debug "Executing: build_config_files ${file_list}"
     build_config_files "${file_list}"
  fi
fi

# Apply configured file ownership and permissions
chown_configs

exit 0

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