#!/bin/bash
# mr4.5+: check approx POSIX user (must be identical on mgmt peers)
# It is a refactored part of mr4.5 PRO step 'check_approx_userid'

set -e

if [ -h "/var/cache/approx" ] ; then
  echo "Found symlink '/var/cache/approx'"
else
  echo "ERROR: Missing symlink '/var/cache/approx' to '/mnt/glusterfs/mgmt-share/approx/'" >&2
  RC=1
fi

if [ -d "/mnt/glusterfs/mgmt-share/approx/" ] ; then
  echo "Found folder '/mnt/glusterfs/mgmt-share/approx/'"
else
  echo "ERROR: Missing folder '/mnt/glusterfs/mgmt-share/approx/'" >&2
  RC=1
fi

CACHE_USER=$(stat -c %U "/mnt/glusterfs/mgmt-share/approx/")
CACHE_GROUP=$(stat -c %G "/mnt/glusterfs/mgmt-share/approx/")

if [ -z "${CACHE_USER}" ] ; then
  echo "ERROR: cannot detect user for '/mnt/glusterfs/mgmt-share/approx/'" >&2
  RC=1
fi

if [ -z "${CACHE_GROUP}" ] ; then
  echo "ERROR: cannot detect group for '/mnt/glusterfs/mgmt-share/approx/'" >&2
  RC=1
fi


if [ "${CACHE_USER}" != "approx" ] || [ "${CACHE_GROUP}" != "approx" ] ; then
  echo "ERROR: Wrong user/group '${CACHE_USER}'/'${CACHE_GROUP}' for '/mnt/glusterfs/mgmt-share/approx/' (expected approx/approx)" >&2
  RC=1
else
  echo "Approx cache '/mnt/glusterfs/mgmt-share/approx/' owner/group is OK: '${CACHE_USER}'/'${CACHE_GROUP}'"
fi

exit "${RC:-0}"
