#!/bin/sh

NLPL="/projects/nlpl";
FILES="${NLPL}/operation/monitor/files";
DIRECTORIES="${NLPL}/operation/monitor/directories";

target="${1}"
if [ -z "${target}" -o ! -d "${target}" ]; then
  echo "monitor: invalid target directory ‘${target}’; exit.";
  exit 1;
fi

tmp=/tmp
files=${tmp}/.monitor.files.${USER}.$$
directories=${tmp}/.monitor.directory.${USER}.$$

{
  start=$(date +"%Y-%m-%d (%H:%M %Z)");
  echo "[${start}] ${0}"; echo; echo;

  find ${target} -type f | sed "s,^${target}/?,,g" | sort \
    > ${files} 2> /dev/null
  find ${target} -type d | sed "s,^${target}/?,,g" | sort \
    > ${directories} 2> /dev/null

  while read i; do
    stat --printf "%n\t%U\t%G\t%s\t%a\t%Y\t%X\n" "${i}";
  done < ${files} > ${FILES}
  \rm -f ${files};

  while read i; do
    stat --printf "%n\t%U\t%G\t%s\t%a\t%Y\t%X\n" "${i}";
  done < ${directories} > ${DIRECTORIES}
  \rm -f ${directories};

  svn diff ${DIRECTORIES}
  echo; echo;
  svn diff ${FILES}

  end=$(date +"%Y-%m-%d (%H:%M %Z)");
  echo; echo; echo "[${end}] exit(1).";
} | mailx -r oe@ifi.uio.no -s "[NLPL] nightly monitor on Abel" infrastructure@nlpl.eu
