#!/usr/bin/env bash

set -e

eval `ultrapossum-config init`
tmp=`tempfile`
trap "/bin/rm -f $tmp; eval `ultrapossum-config term`" 0

# untained value: use it carefully
cat > $tmp

host=`getvalue $tmp HOST`
slapd_major_version=`getvalue $tmp SLAPD_MAJOR_VERSION`

if ! include "$host" "$SLAVES"; then
  echo "E: No such slave: $host" 1>&2
  exit 1
fi

if test "x$SLAPD_MAJOR_VERSION" != "x$slapd_major_version"; then
  echo "E: OpenLDAP version mismatch: $SLAPD_MAJOR_VERSION != $slapd_major_version" 1>&2
  exit 1
fi

ldiffile="$DIRECTORY/$host.ldif"
if ! test -f $ldiffile; then
  echo "E: $host not configured yet.. try later" 1>&2
  exit 1
fi
if ! test -s $ldiffile; then
  echo "E: Initial LDIF file already fetched." 1>&2
  exit 1
fi

cat $ldiffile && /bin/cp /dev/null $ldiffile

exit 0

