#!/bin/ksh
# 
# $Copyright
# Copyright 1993, 1994 , 1995 Intel Corporation
# INTEL CONFIDENTIAL
# The technical data and computer software contained herein are subject
# to the copyright notices; trademarks; and use and disclosure
# restrictions identified in the file located in /etc/copyright on
# this system.
# Copyright$
# 
 
#
# Copyright (c) 1992-1995, Locus Computing Corporation
# All rights reserved
#
#
# HISTORY
# $Log: duck,v $
# Revision 1.4  1995/02/02  00:18:52  bolsen
#  Reviewer(s): Jerry Toman
#  Risk: Medium (lots of files)
#  Module(s): Too many to list
#  Configurations built: STD, LITE, & RAMDISK
#
#  Added or Updated the Locus Copyright message.
#
# Revision 1.3  1994/11/18  20:57:16  mtm
# Copyright additions/changes
#
# Revision 1.2  1993/07/14  18:54:13  cfj
# OSF/1 AD 1.0.4 code drop from Locus.
#
# Revision 1.1  1992/12/02  21:33:59  dleslie
# New bug drop from Locus, December 2, 1992
#
# Revision 1.1.1.2  1993/07/01  21:21:53  cfj
# Adding new code from vendor
#
# Revision 3.0  92/11/23  11:44:13  chrisp
# First hatching.
# 
#
. stresslib

DELAY=10		# default working time on each node
: ${PIE=pie}
required $PIE

while getopts ":E:e:hi:L:Nnmr:q" opt; do
	case "$opt" in
	    L)	LOGFILE="$OPTARG";;
	    E)	EAT_OR_SLEEP=eat; DELAY=$OPTARG;;
	    e)	EAT_OR_SLEEP=eat; DELAY=$OPTARG; randomizing_delay=true;;
	    h)  PIE_OPT=-h;;
	    i)	i=$OPTARG;;
	    N)	RANDOMIZING_NODES=;;
	    n)	RANDOMIZING_NODES=true;;
	    m)  PIE_OPT=;;
	    q)	QUIET=please;;
	    r)	RANDOM=$OPTARG;;
	    :)	echo "$NAME: $OPTARG requires a value"
		exit 2;;
	   \?)	echo "$NAME: unknown option $OPTARG"
		echo "usage: $NAME -n -t|T<secs> <node_list>"
		exit 2;;
	esac
done
shift OPTIND-1
node_list="$*"
PIE="$PIE $PIE_OPT -i$i"

#
# Prompt for all the important stuff if not given
# 
[ "$node_list" ]  || read node_list?"Node list? "
[ "$node_list" ]  || node_list="$(node_self)"

declare_nodes $node_list

#
# Find out the correct fixed answer
#
log "first, work out where to go"
fixed=$($PIE)

trap 'kill $flotsum; exit' INT KILL TERM
CYCLING_NODES=true
log "flying through nodes ${NODES[*]}..."
$PIE |&
flotsum=$!
		
#
# Take a random walk through the nodes,
#	pausing to do a random amount of work on each.
#
let "delay_time = DELAY"
while kill -0 $flotsum 1>/dev/null 2>&1; do
	#
	# Make a (random) selection from the list of nodes
	#	and migrate there
	#
	next_node
	[ "$NODE" ] || break
	log "migrating to node $NODE"
	migrate_to_node $NODE $flotsum

	[ $DELAY -eq 0 ] && continue

	#
	# Eat cpu for a time
	#
	if [ "$randomizing_delay" ]; then
		let "delay_time = (RANDOM % DELAY) + 1"
	fi
	log "feeding on node $NODE for $delay_time secs"
	sleep $delay_time
done

read -p floating
if [ "$floating" = "$fixed" ]; then
	log "...stopping on node $NODE"
else
	log "...sunk on node $NODE"
	log "${floating} is not ${fixed}"
fi
exit 0
