#!/bin/bash


function get_files () {
  files=`ls | egrep -v "(\.ana|\.rov|\.nop|\.rig|\.Z|\.gz)" | grep '^[0-9][0-9][0-9]'`
}

function get_owl () {
  owl=`echo $files | cut -f 1 -d ' ' | cut -f 1 -d '.'` 
}
 
function print_files () {
  get_files 
  get_owl 
  echo "Entering $1 (Owl $owl):"
  
  for fn in $files; do
    echo -n "   Printing file:$fn ..."
    xdprint $extra $fn > /tmp/.xdp.$$.$n
    lpr -s /tmp/.xdp.$$.$n
    xdp_waitforlp
    echo "done."
    rm /tmp/.xdp.$$.$n
    n=`expr $n + 1`
  done
}
  
n=0
extra=""

set -- `getopt r $*`
if test $? != 0
then 
	echo "Usage: xdprint_all [-r] <day 1> <day 2> ..."
	echo ""
	echo "    -r                 plot rayleigh test in place of PSTH"
	echo "    -t <title>         use <title> as title for all pages"
	exit 2
fi
for i
do
	case "$i"
	in
		-r)
			extra="-r"; shift;;
		--)
			shift; break;;
	esac
done

if test $# -eq 0
then
  dirs='.'
else
  dirs=$*
fi

for x in $dirs
do

  if test -d $x
  then
	  (cd $x; print_files $x)
  else
	echo "xdprint_all: unable to cd to $x"
  fi
done
