#!/bin/csh 
# creates preprocessing and alignment

# 

# usage: checkrec CD=<cdpattern> <dirpattern> <filepattern>
# If the pattern DIRECTORY or FILE is omitted, the script expands to 
# all possible directories in the volume and all possible files in the
# directories.
# Typical call for a total volume (VM21):
# checkrec mausseg CD=vm21 '[a-z][0-9][0-9][0-9][a-z]'
# Typical call for a single directory:
# checkrec mausseg CD=vm21 g101a
# Typical call for a single file:
# checkrec mausseg CD=vm21 g101a g101a000.a16


set TARGETSOURCE = ""
set PARTITURSOURCE = ""
set CD = ""
set DIRECTORY = ""
set FILE = ""
set TARGETROOT = /raid/r31/NICOLE
set PARTITURROOT = /sun1/schiel/bas

# set verbose # debugging

# Actually do the argument parsing here

while ( "$1" != "" )
        switch ("$1")
        case *=*:
                set key = `echo "$1" | cut -d= -f1`
                set val = `echo "$1" | cut -d= -f2`
                eval "set $key "= \'"$val"\'
                unset key val
                shift
                breaksw
        default:
                break
        endsw
end

# end option parser

if ( $CD == '' ) then
 echo 'usage: checkrec CD=volume [icsioptions] [dirpattern] [filepattern]'
 echo CAUTION: If dirpattern or filepattern is ommited, the script will expand to '*'
 exit 1
endif


if ( $TARGETSOURCE == '' ) then
  set TARGETSOURCE = $TARGETROOT/$CD:h
#  set TARGETSOURCE = $TARGETROOT/`echo $CD | gawk '{ print toupper($1) }'`/par*endif
if ( ! -d $TARGETSOURCE ) then
  echo ERROR: $TARGETSOURCE is empty
  exit 1
endif

if ( $PARTITURSOURCE == '' ) then
    set PARTITURSOURCE = $PARTITURROOT/$CD/par*
#    set PARTITURSOURCE = $PARTITURROOT/`echo $CD | gawk '{ print toupper($1) }'`/par*endif
if ( ! -d $PARTITURSOURCE ) then
  echo ERROR: $PARTITURSOURCE is not a directory for par files
  exit 1
endif

if ( "$1" == '' ) then
  set DIRECTORY = '*'
  echo script will expand to all directories
else 
  set DIRECTORY = "$1"
endif
if ( "$2" == '' ) then
  set FILE = '*'
  echo script will expand to all files
else 
  set FILE = "$2"
endif



# delete old files if present

echo using $TARGETSOURCE as target directory
if ( -e $TARGETSOURCE/checkrec ) then
  /bin/rm $TARGETSOURCE/checkrec
  touch $TARGETSOURCE/checkrec
endif

cd $PARTITURSOURCE
foreach dir ( * )
    echo $dir >> $TARGETSOURCE/checkrec
    ls $dir |wc -l >> $TARGETSOURCE/checkrec
    ls $TARGETSOURCE/$dir*/*.slf | wc -l >> $TARGETSOURCE/checkrec
    ls $TARGETSOURCE/$dir/*.rec | wc -l >> $TARGETSOURCE/checkrec
    ls $TARGETSOURCE/$dir/*.par.mau | wc -l >> $TARGETSOURCE/checkrec
    ls $TARGETSOURCE/$dir/*.par | wc -l >> $TARGETSOURCE/checkrec
end



