#!/bin/bash
#
# sccl_test_res_on_node testet, ob eine Resource auf dem angegebenen Knoten
# im Script-Cluster laeuft.
#
# Aufruf:
#
# sccl_test_res_on_node [-r] <resource> [<node>]
#   -r: running, not stopping
#
# Die Resource <resource> wird auf <node> getestet. Ist <node> nicht angegeben, dann
# auf aktuellem node.
#
########################################################################################
#
. $(dirname $0)/globals.settings
#
if [[ "$1" = "-r" ]]; then
  RUNFLAG=1
  shift
else
  unset RUNFLAG
fi
#
PACKAGE=$1
NODE=${2:-$THISHOSTNAME}
#
if [[ ($NODE = $THISHOSTNAME || $NODE = $THISFQDN) && -r /etc/sccl/localadminpwd.dat ]]; then
  if [[ -s "$LOCKDIR/$PACKAGE$LOCKEXTENSION" ]]; then
    [[ -z "$RUNFLAG" || ! -s "$LOCKDIR/$PACKAGE$PROGRESSEXTENSION" ]] || grep -Fq ' starting' "$LOCKDIR/$PACKAGE$PROGRESSEXTENSION"
  else
    exit 1
  fi
else
  if RET=$(readnode $NODE "sccl_cat_lockfile.s3w?lockfile=$PACKAGE$LOCKEXTENSION"); then
    if [[ -n "$RET" ]]; then
      [[ -z "$RUNFLAG" || -z $(readnode $NODE "sccl_cat_lockfile.s3w?lockfile=$PACKAGE$PROGRESSEXTENSION" | grep -F 'stopping') ]]
    else
      exit 1
    fi
  elif [[ "$HBNODES" = *"$NODE$HBNODE_EXT"* ]]; then
    if [[ -n "$RUNFLAG" ]]; then
      [[ -n $(readnode $NODE$HBNODE_EXT "sccl_cat_lockfile.s3w?lockfile=$PACKAGE$LOCKEXTENSION") && -z $(readnode $NODE$HBNODE_EXT "sccl_cat_lockfile.s3w?lockfile=$PACKAGE$PROGRESSEXTENSION" | grep -F 'stopping') ]]
    else
      [[ -n $(readnode $NODE$HBNODE_EXT "sccl_cat_lockfile.s3w?lockfile=$PACKAGE$LOCKEXTENSION") ]]
    fi
  else
    exit 1
  fi
fi
