#!/bin/bash
# Purpose: wrapper around etckeeper for usage inside ngcpcfg
################################################################################

set -e
set -u
set -o pipefail

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

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

timestamp_replacementchars=''
. "${FUNCTIONS}"/main

# main script

log_debug "cd /etc"
cd /etc

log_debug "type -p etckeeper"
if ! type -p etckeeper &>/dev/null ; then
  log_warn "etckeeper is not available, skipping etckeeper execution"
  exit 0
fi

log_debug "test -d .git"
if ! [ -d .git ] ; then
  log_warn "etckeeper has not been initialized yet, skipping etckeeper execution"
  exit 0
fi

log_info "Checking state of /etc files"
if is_git_clean ; then
  log_info "OK:   nothing to commit."
else
  log_debug "etckeeper commit \"ngcpcfg apply on \$(date)\""
  etckeeper commit "ngcpcfg apply on $(date)" | sed "s/^/$timestamp_replacementchars/"
fi

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