#!/usr/bin/ksh
# ACSLS robot exerciser.
# Written by Nicolai Mass  - www.mass.dk
# Function: Move all tapes within an ACSLS controlled library. Nice for stress testing a robot
# Script Parameters: ACS,LSMID {numbers to run} E.g: ./stress.sh 0,0 2


# Trap various interupt signal so I can kill the running cmd_proc before exit. Set up misc. variables.
trap 'if [ "$PID" != "" ];
then
kill ${PID}
rm /tmp/acsls_command.$$
exit;fi' 1 2 3

STRESS_OUT=/tmp/ACSLS_stress.out
ACSID=$1
RUN=$2
i=0

# Get contens from volrpt and generate file we can pipe into cmd_proc
volrpt -d -l ${ACSID} | awk '{print $1}' | while read VOLSER
do
echo "move ${VOLSER} ${ACSID}" >> /tmp/acsls_command.$$
done
echo "logoff" >>  /tmp/acsls_command.$$


# Here we call cmd_proc. Start a loop so the command can run longer for smaller robots.
# Build as a procedure so we can kill the cmd_proc upon control-c, and prevent cmd_proc from continuing it's piped work.

do_process()
{
while [ $i -lt RUN ]
 do
  let i=i+1
   cmd_proc -q < /tmp/acsls_command.$$ >> ${STRESS_OUT} 2>&1 &
   PID=$!
   echo "cmd_proc PID is ${PID}, waiting for job to exit. Output from the operation can be found in ${STRESS_OUT}"
   wait ${PID}
   echo "done move ${i}"
 done

}

do_process

