#!/bin/bash
#
# sccl_stop_res_rev stoppt eine Resource im Script-Cluster
#
# Aufruf:
#
# sccl_stop_res_rev [--force|--clear|--tstkill] [--clusterstop] [-p <stoppar>] <kurzname> <pidsig> <res> ...
#
# Die Resourcen <resourcen> werden in umgekehrter Reihenfolge gestoppt
#
########################################################################################
#
. $(dirname $0)/globals.settings
#
unset FORCE CLEARTSTKILL CLUSTERSTOP STOPPARS
while getopts p:-:? op; do
  case "$op" in
    p) STOPPARS="$STOPPARS -p $OPTARG";;
    -) case "$OPTARG" in
         force)   FORCE='--force';;
         clear)   CLEARTSTKILL='--clear';;
         tstkill) CLEARTSTKILL='--tstkill';;
         clusterstop) CLUSTERSTOP='--clusterstop';;
         *) echo "usage $MYPROG [--force|--clear|--tstkill] [--clusterstop] [-p <stoppar>] <kurzname> <pidsig> <res> ..."
            exit 1;;
       esac;;
    ?) echo "usage $MYPROG [--force|--clear|--tstkill] [--clusterstop] [-p <stoppar>] <kurzname> <pidsig> <res> ..."
       exit 1;;
  esac
done
shift $(( $OPTIND - 1))
#
KURZNAME=$1
shift
PIDSIG=$1
shift
#
if [[ $# -gt 0 ]]; then
  for (( i = $#; $i >= 1; i = $i - 1 )); do
    ./sccl_stop_resource $FORCE $CLEARTSTKILL $CLUSTERSTOP $STOPPARS "${!i}" $KURZNAME "$PIDSIG"
    RET=$?
    if [[ $RET = 2 ]]; then
      RET=0
      for (( j = $i + 1; $j <= $#; j = $j + 1 )); do
        ./sccl_start_resource "${!j}" $KURZNAME $PIDSIG
        R=$?
        if [[ $R != 0 ]]; then
          RET=$R
        fi
      done
      if [[ $RET = 0 ]]; then
        exit 2
      else
        exit 3
      fi
    elif [[ $RET != 0 && $RET != $LOCKNOTFOUND ]]; then
      exit $RET
    fi
  done
fi
exit 0
