#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`;
dasactive() { dasadmin list $node | grep -c type ; }

dasvols=/tmp/dasvols
dasinvt=/tmp/dasinvt

rm -f $dasvols
rm -f $dasinvt

more=1
nomore="no more data"
while [ $more -ne 0 ]; do
  if [ $more -eq 1 ]; then
     dasadmin qvolsrange "" "" 1000 >$dasvols
  else
     dasadmin qvolsrange $first "" 1000 >>$dasvols
  fi
  if [ `grep -c "$nomore" $dasvols` -ne 0 ]; then break; fi
  more=`expr $more + 1`
  if [ $more -gt 30 ]; then break; fi
  first=`tail -n1 $dasvols | awk '{print $2}'`
done

grep volser $dasvols | grep attrib | grep media |while read volser volume media mediatype rest; do
  echo -n $volume $mediatype ""
  location=`dasadmin view -t $mediatype $volume | awk '/coordinate/ {print $3}'`
  echo $location
  echo "dasadmin pinvt $location $location" >> $dasinvt
done

cat $dasinvt | while read cmd; do
  echo `date` $cmd
  eval $cmd
  more=1
  sleep 1
  while [ `dasactive` = "1" ]; do
     more=`expr $more + 1`
     if [ $more -gt 300 ]; then
        echo "das seems busy... quitting"
        return 1
     fi
     sleep 1
  done
done
