#!/bin/csh
# creates turns out of one big soundfile containing both channels

# 

# usage: maketurns CD=<cd-root> <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):
# maketurns CD=/cdrom/vm21 '[a-z][0-9][0-9][0-9][a-z]'
# Typical call for a single directory:
# maketurns CD=/cdrom/vm21 g101a
# Typical call for a single file:
# maketurns CD=/cdrom/vm21 g101a g101a000.a16

set CD = ""
set DIRECTORY = ""
set FILE = ""
set TARGET=/data/data1/beringer/tmp


# 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: autotest 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 ( "$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

#taking beginning and end of an utterance as well as its turnname

foreach dir ( $CD/$DIRECTORY )
	cd $dir
	set count = 0
	foreach file ( $FILE*.16)
	    cp $dir/* $TARGET
	    cd $TARGET
	 #   foreach item (`cat ~beringer/DISS/HISTPHON/trans/$FILE*.mar`)
	     foreach item (`cat $TARGET/$FILE*.mar`)
 		@ count ++
		@ flag = $count % 3
		if ( $flag == 1) then
			@ anfang = $item + 512
		endif
		if ( $flag == 2) then
			@ ende = $item + 512
		endif

		if ( $flag == 0) then
			set output = $item
			set basename = `echo $output | sed 's/^\(........\).*$/\1/'`
			set dirname = `echo $output | sed 's/^\(......\).*$/\1/'`
		@ length = $ende - $anfang
                @ length = $length + 1
#cutting it into sequences

			cd $TARGET
			if (! -e $TARGET/$dir:t ) then
				mkdir $TARGET/$dir:t
			endif
			cd $dir:t
			if (! -e $TARGET/$dir:t/$dirname ) then
				mkdir $TARGET/$dir:t/$dirname
			endif
			cd $dirname
			if ( ! -e $TARGET/$dir:t/$dirname/$output ) then
				dd if=$dir/$basename.16 of=$TARGET/$dir:t/$dirname/$output bs=2 skip=$anfang count=$length 
			endif
		endif
#		rm $TARGET/$file
#	   end
#		rm $TARGET/*	
    end
		rm $TARGET/*.16
		rm $TARGET/*.al
		rm $TARGET/*.mar
		rm $TARGET/*.rpr
end

