#!/bin/bash
# Purpose: set config value
################################################################################

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

. "${FUNCTIONS}"/main

## functions {{{
help() {
  echo "Usage: ngcpcfg set <file> <key>"
  echo "Example: ngcpcfg set /etc/ngcp-config/config.yml general.maintenance=yes"
}

## }}}

[ "${#:-}" -ne 2 ] && (help >&2 ; exit 1)

file="$1"
data="$2"
RC=0

[ -f "${file}" ] || (log_error "missing ${file}. Exiting." ; exit 1)
[ -z "${data}" ] && ( log_error "missing data to set. Exiting." ; exit 1)

log_debug "Received data: ${data}"
option="${data/=*/}"
value="${data/*=/}"

[ -z "${option}" ] && ( log_error "missing option to set. Exiting." ; exit 1)
[ -z "${value}" ] && ( log_error "missing value to set. Exiting." ; exit 1)
log_debug "Saving option '${option}' value '${value}' into '${file}'"

if [[ ${value} =~ ^\'.*\'$ ]] ; then
  log_debug "\$value is already quoted."
else
  log_debug "\$value is NOT quoted, doing so if necessary."
  case ${value} in
    [0-9]*|\[*\]|\{*\})
      log_debug "Do not quoting \$value for integers, arrays and hashes."
      ;;
    *)
      log_debug "Quoting \$value to prevent further Perl errors."
      value="\"${value}\""
      ;;
  esac
fi

perl_line="\$yaml->{${option//./\}->\{}}=${value};"
log_debug "perl line: ${perl_line}"

tmp=$(mktemp)
log_debug "Temporary perl file: ${tmp}"

cat > "${tmp}" << EOF
use strict;
use warnings;
use YAML::XS;
my \$file="${file}";
my \$yaml = YAML::XS::LoadFile("\$file");

${perl_line}

YAML::XS::DumpFile(\$file, \$yaml);
EOF

log_debug "perl -wCSD \"${tmp}\" || RC=$?"
perl -wCSD "${tmp}" || RC=$?

if [ "${RC}" = "0" ]; then
  log_debug "ngcpcfg diff | tail -n +5 || true"
  ngcpcfg diff | tail -n +5 || true
fi

if [ -n "${DEBUG:-}" ] ; then
  log_debug "Not removing temporary file ${tmp}"
else
  rm -f "${tmp}"
fi

exit ${RC}

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