: 'Copyright 1986 Intel Corp.'
# This is a shellscript version of Astage4.
# It was replaced with a C version.  This is a backup version "just in case".

# Stage4 Synchronization Processing
# Runs on the Network Administrator's node
# 03/15/86 oer
# 03/30/86 oer misc. cleanup
# 05/08/86 oer change to reference the master node
# 05/08/86 oer remove -l $LOGINID from Amonitor.ss calls, Amonitor.ss will get
# 05/08/86 oer take advantage of Unonode to produce iDIS path
# 05/12/86 oer suppress error message if node is down
# 05/17/86 oer correct problem with update of master node from another
# 05/22/86 oer handle potential race condition
# 07/11/86 oer change name to Astage4, to pave the way for a C version

MASSYNCH=$MASNODEP`$iDISBIN/Unonode $iDIS`/synch # Master node synch directory
export MASSYNCH
RAWiDIS=`$iDISBIN/Unonode $iDIS`

# traps to ignore 1 2 3 and 15 have been set previously
trap "rm -f /tmp/Astg4$$ /tmp/Astg4a$$; trap 0; exit 0" 0 13
NSDBUG=/bin/false
if [ -f $MASSYNCH/debug ] ; then
   set -x
   echo $0
   NSDBUG=/bin/true
fi

[ $OPENNET = y ] && ONFLAG='-o'

node="."			# just to start processing going
txnfound=/bin/true

# keep cycling through the "Applied Transaction Queue" as long as
# any transactions remain
while $txnfound
do
   txnfound=/bin/false
   # keep cycling until we have made a complete circuit of the nodes
   while [ "$node" != "" ]
   do
      $iDISBIN/Anxnod "$node" > /tmp/Astg4$$
      case $? in
	 1)   node=""; continue ;;	# at end of queue, completed circuit
      esac

      node=`cat /tmp/Astg4$$`
      # check the status of the node's lock file
      $iDISBIN/Applylock "$node" 1800	# lock age is 30 minutes (1800 secs)
      case $? in
	 0)  # Lock file is current
	    txnfound=/bin/true
	    continue ;;

	 1)  # Lock file has aged, assume node went down
	    cd $MASSYNCH/$node.apply
	    $iDISBIN/A1txn $node > /tmp/Astg4a$$
	    case $? in
	       0)  # everything o.k.
		  txnfound=/bin/true
		  set `cat /tmp/Astg4a$$`
		  txnum=$1
		  batch=$2
		  txn=$node.$txnum
		  $NSDBUG && cat $txn >> debug
		  case $node in
		     $PHYSYS)  # Run local transactions locally
			cp $txn /tmp/$PHYSYS.$txnum
			/iDISboot/Amonitor.ss \
			   $ONFLAG -r -i $RAWiDIS -m $MASNODE \
			   -s $PHYSYS -t $node -n $txnum -b $batch & ;;
		     *)
			cp $txn //$node/tmp/$PHYSYS.$txnum 2>/dev/null
			rexec //$node/iDISboot/Amonitor.ss \
			   -o -r -i $RAWiDIS -m $MASNODE \
			   -s $PHYSYS -t $node -n $txnum -b $batch -- /dev/null\
			   2>/dev/null ;;
		  esac
		  continue ;;
	       1)  # correct race condition
		  rm -f $node..L
		  continue ;;
	       *)  # error, couldn't find a transaction
		  rtn=$?
		  echo $0: Missing transaction for $node, $rtn >>$MASSYNCH/logfile
		  echo $0: Missing transaction for $node, $rtn > /dev/console;;
	    esac ;;
	 2)  # Lock file was created
	    cd $MASSYNCH/$node.apply
	    $iDISBIN/A1txn $node > /tmp/Astg4a$$
	    case $? in
	       0)  # everything o.k.
		  txnfound=/bin/true
		  set `cat /tmp/Astg4a$$`
		  txnum=$1
		  batch=$2
		  txn=$node.$txnum
		  $NSDBUG && cat $txn >> debug
		  case $node in
		     $PHYSYS)  # Run local transactions locally
			cp $txn /tmp/$PHYSYS.$txnum
			/iDISboot/Amonitor.ss \
			   $ONFLAG -a -i $RAWiDIS -m $MASNODE \
			   -s $PHYSYS -t $node -n $txnum -b $batch & ;;
		     *)
			cp $txn //$node/tmp/$PHYSYS.$txnum 2>/dev/null
			rexec //$node/iDISboot/Amonitor.ss \
			   -o -a -i $RAWiDIS -m $MASNODE \
			   -s $PHYSYS -t $node -n $txnum -b $batch -- /dev/null\
			   2>/dev/null ;;
		  esac
		  continue ;;
	       *)  # no transaction was found to execute
		  rm -f $node..L
		  continue ;;
	    esac ;;
	 *)  # error
	    rtn=$?
	    echo $0: Applylock error, $rtn for $node >>$MASSYNCH/logfile
	    echo $0: Applylock error, $rtn for $node > /dev/console ;;
      esac
   done
   $txnfound && sleep 90
   node="."			# just to start processing going
done
