#!/bin/bash

CHECKLOGDIR=%%OARHOMEDIR%%/checklogs
CHECKSCRIPTDIR=%%OARCONFDIR%%/check.d
CPUSET_DIR=/dev/cpuset/oar
LOCKFILE=%%OARHOMEDIR%%/$(basename $0).lock
STAMPFILE=%%OARHOMEDIR%%/oarnodecheckrun.lastrun

if ! [ -r $CHECKLOGDIR ]; then
  echo 1>&2 "Error: Checklogs directory does not exist ($CHECKLOGDIR) !"
  exit 1
fi 

if ! [ -r $CHECKSCRIPTDIR ]; then
  echo 1>&2 "Error: Checkscripts directory does not exist ($CHECKSCRIPTDIR) !"
  exit 1
fi 

shopt -s nullglob

# If there is any running job then exit.
ls $CPUSET_DIR/*/tasks 2>/dev/null | grep -E "^.*_[0-9]*/tasks$" >/dev/null \
  && exit 0

# If there is another instance running then exit.
[ -f $LOCKFILE ] && exit 0
# Else take the lock and stamp this run.
touch $LOCKFILE $STAMPFILE

cd $CHECKSCRIPTDIR
for f in *; do
  [ -x $f ] && CHECKLOGFILE=$CHECKLOGDIR/$f.$(date +%F.%T) ./$f
done
cd - > /dev/null

# Release the lock.
rm -f $LOCKFILE

