#!/bin/csh 


set PARTITURROOT = /home/schiel/bas
set TARGET = /data/data1/beringer
set DIRECTORY = ""
set FILE = ""
set PARTITURSOURCE = ""
set CD = ""

# 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: mau2par CD=volume [icsioptions] [dirpattern] [filepattern]'
 echo CAUTION: If dirpattern or filepattern is ommited, the script will expand to '*'
 exit 1
endif
set cd = ${CD:t}
 
if ( $PARTITURSOURCE == '' ) then
  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

cd $TARGET
foreach mau ($DIRECTORY/$FILE.par.mau)
	cat $mau | gawk -f ~beringer/MAUSSEG/autoseg/mau2par.awk > $mau.final
	cat $PARTITURSOURCE/$mau:r $mau.final > $mau:r
	rm -f $mau.fin*
end
