#!/bin/tcsh

# General purpose tool to create a basic BPF file with rudiment header
# and ORT/KAN tier from a two-column TXT-file per soundfile where
# the 1st column contains the orthography and the second the 
# canonical transcript in SAM-PA.
# If the input contains only one column, just the ORT tier is created;
# this will work as input to MAUS, for German, if the the helper txt2lex.pl 
# is installed.

# Note that SAMP must match the sample frequency of the sound files.


# F. Schiel 04.12.2008

set SAMP = 16000 # insert sample frequency here

# Actually do the argument parsing here

while ( "$1" != "" )
	switch ("$1")
	case *=*:
		set key = `echo $1 | awk -F= '{ print $1 }'`
                #check if option is known (set)
                eval set checkoption = '$?'$key
                if ( $checkoption == 0 ) then
                  echo "ERROR: unknown option $key - exiting"
                  exit 1
                endif
		set val = `echo $1 | awk -F= '{ print $2 }'`
		eval "set $key "= \'"$val"\'
		unset key val
		shift
		breaksw
        default:
		break
        endsw
end

# end option parser

if ( $1 == "" ) then 
  echo "usage: ${0:t} file1.txt [file2.txt ...]"
  echo "       creates basic BPF file *.par from *.txt with ORT/KAN tier"
  echo "       from a two-column TXT-file per soundfile where the 1st column"
  echo "       contains the orthography and the second the canonical transcript in SAM-PA."
  exit 2
endif

foreach tf ( *.txt )
  echo converting $tf into ${tf:r}.par
  echo "LHD: Partitur 1.2" >! ${tf:r}.par
  echo "SAM: $SAMP" >> ${tf:r}.par
  echo "LBD:" >> ${tf:r}.par
  cat $tf | awk 'BEGIN{c=0} {if($2!="")printf("ORT:\t%d\t%s\nKAN:\t%d\t%s\n",c,$1,c,$2);else printf("ORT:\t%d\t%s\n",c,$1); c++}' >> ${tf:r}.par

end

exit 0

