#!/bin/sh

# Shellscript mit " chmod u+x dateiname " freigeben!

# Usage: VM_rpr_check Filename       (Filename kann Pfad enthalten!)

# Function: Checks RPR-File 

i=$1

if test -r $i
 then :
 else
  echo "Error:" $i "is not a readable file!"
  exit 1
fi

 
n=`echo $1 | sed 's#.*\/\([0-9a-z\.]*\)$#\1#'`
echo $n

length=`cat $i | wc -l`

length=`expr $length + 0` 
#ntig, da sonst length im Argument von head als Dateiname interpretiert wird
char=`cat $i | wc -c`
a=`head -n $length $i | wc -c`
if test "$a" -lt "$char" 
 then echo "Error: \n missing in last line!"
fi

a=`cat $i | grep '#'`
if test "$a" != ""
 then echo "Error: rpr-File should not contain '#'!"
fi

a=`cat $i | tr '\011' '#' | grep "#" | wc -l`
if test $a -lt $length
 then echo "Error: In all lines columns should be separated by 'TAB'!"
fi

runline=1 
length=`expr $length + 1` 


a=`head -n 1 $i | tr '\011' '#' | egrep "^session_no#[0-9]*$"`
if test "$a" = ""
 then 
  a=`head -n 1 $i | tr '\011' '#' | egrep "session_no"`
  if test "$a" != ""
   then
    echo "Error: If there is no entry for 'session_no' then leave out the entire line, or columns not separated by 'TAB'!" 
    runline=`expr $runline + 1`
  fi
 else
  runline=`expr $runline + 1`
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^dialogue_name#[gejm][0-9][0-9][0-9][abcd]$' | sed 's/dialogue_name#//'`
if test "$a" = ""
 then
  echo "Error: Line containing 'dialogue_name' is missing or wrong!"
  runline=`expr $runline + 1`
 else
  dianame=`echo $n | sed 's/\([gejm][0-9][0-9][0-9][abcd]\)\.rpr/\1/'`
  if test "$dianame" = "$a"
   then 
    runline=`expr $runline + 1`
   else 
    echo "Error: Line entry for 'dialogue_name' and Filename do not correspond!"
    runline=`expr $runline + 1`
  fi
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^recording_date#[09][0-9][01][0-9][0-3][0-9]$'`
if test "$a" = ""
 then
  echo "Error: Line containing 'recording_date' is missing or wrong!"
  runline=`expr $runline + 1`
 else
  runline=`expr $runline + 1`
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^scenario_date#[09][0-9][01][0-9][0-3][0-9]$'`
if test "$a" = ""
 then
  echo "Error: Line containing 'scenario_date' is missing or wrong!"
  runline=`expr $runline + 1`
 else
  runline=`expr $runline + 1`
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^recording_by#..*$'`
if test "$a" = ""
 then 
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep 'recording_by'`
  if test "$a" != ""
   then
    echo "Error: Line containing 'recording_by' is wrong . If there is no entry for then leave out the entire line!"
    runline=`expr $runline + 1`
  fi
 else
  runline=`expr $runline + 1`
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep 'recording_site'`
if test "$a" != ""
 then 
  runline=`expr $runline + 1`
  lang=`echo $n | sed 's/^\([gemj]\)[0-9][0-9][0-9][abcd].*/\1/'`
  if test "$a" = "recording_site#LMU"
   then
    a="g"
    site="LMU"
  elif test "$a" = "recording_site#CMU"
   then
    a="e"
    site="CMU"
  elif test "$a" = "recording_site#ATR"
   then
    a="j"
    site="ATR"
  elif test "$a" = "recording_site#UHH"
   then
    a="m"
    site="UHH"
  elif test "$a" = "recording_site#UBN"
   then
    a="g"
    site="UBN" 
   else
    echo "Error: Line containing 'recording_site' is wrong!"
  fi
  if test "$a" != "$lang" 
   then
    echo "Error: Language in filename and entry for 'recording_site' do not correspond!"
  fi 
 else
  echo "Error: Line containing 'recording_site' is missing!"
fi  

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^scenario_id#[abcd]$' | sed 's/scenario_id#//'`
scen=`echo $n | sed 's/^[gemj][0-9][0-9][0-9]\([abcd]\).*/\1/'`
if test "$a" = "$scen"
 then
  runline=`expr $runline + 1`
 else
  echo "Error: Line containing 'scenario_id' is wrong or missing!"
fi 

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^no_speakers#[234]$' | sed 's/no_speakers#//'`
      #In memo131 sind 2-9 Sprecher zugelassen und nicht nur 2 bis 4 Sprecher.
if test "$a" = ""
 then
  echo "Error: 'no_speakers' should be 2 to 4 or line is missing!"
  nosp=`expr 0 + 0`
 else
  nosp=`expr $a + 0`
  if test $a -eq 2
   then 
    runline=`expr $runline + 1`
    if test "$lang" = "m"
     then 
      echo "Error: Dialog" $n "should not have 2 speakers!"
    fi
  fi
  if test $a -eq 3
   then 
    runline=`expr $runline + 1`
    if test "$lang" != "m"
     then 
      echo "Error: Dialog" $n "not have 3 speakers!"
    fi
  fi    
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker1_id#[A-Z][A-Z][A-Z]$' | sed 's/speaker1_id#//'`
if test "$a" = ""
 then
  echo "Error: Line containing 'speaker1_id' is wrong or missing!"
 else
  runline=`expr $runline + 1`
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker2_id#[A-Z][A-Z][A-Z]$' | sed 's/scenario_id#//'`
if test "$a" = ""
 then
  echo "Error: Line containing 'speaker2_id' is wrong or missing!"
 else
  runline=`expr $runline + 1`
fi

if test $nosp -ge 3
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker3_id#[A-Z][A-Z][A-Z]$' | sed 's/scenario_id#//'`
  if test "$a" = ""
   then
    echo "Error: Line containing 'speaker3_id' is wrong or missing!"
   else
    runline=`expr $runline + 1`
  fi
fi

if test $nosp -ge 4
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker3_id#[A-Z][A-Z][A-Z]$' | sed 's/scenario_id#//'`
  if test "$a" = ""
   then
    echo "Error: Line containing 'speaker4_id' is wrong or missing!"
   else
    runline=`expr $runline + 1`
  fi
fi

# only german and english dialogues
if test "$lang" = "e" -o "$lang" = "j"
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker1_language#[gej][0-3]$'`
  if test "$a" = ""
   then
    echo "Error: Line containing 'speaker1_language' is wrong or missing! "
   else
    runline=`expr $runline + 1`
  fi
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker2_language#[gej][0-3]$'`
  if test "$a" = ""
   then
    echo "Error: Line containing 'speaker2_language' is wrong or missing!"
   else
    runline=`expr $runline + 1`
  fi
  if test $nosp -ge 3
   then
    a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker3_language#[gej][0-3]$'`
    if test "$a" = ""
     then
      echo "Error: Line containing 'speaker3_language' is wrong or missing!"
     else
      runline=`expr $runline + 1`
    fi
  fi
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker4_language#[gej][0-3]$'`
  if test $nosp -ge 4
   then
    if test "$a" = ""
     then
      echo "Error: Line containing 'speaker4_language' is wrong or missing!"
     else
      runline=`expr $runline + 1`
    fi  
  fi
# only multilingual dialogues
elif test "$lang" = "m"
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker1_language#[gej][0-3],*[gej]*[0-3]*$'`
  if test "$a" = ""
   then
    echo "Error: Line containing 'speaker1_language' is wrong or missing! "
   else
    runline=`expr $runline + 1`
  fi
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker2_language#[gej][0-3],*[gej]*[0-3]*$'`
  if test "$a" = ""
   then
    echo "Error: Line containing 'speaker2_language' is wrong or missing!"
   else
    runline=`expr $runline + 1`
  fi
  if test $nosp -ge 3
   then
    a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker3_language#[gej][0-3],*[gej]*[0-3]*$'`
    if test "$a" = ""
     then
      echo "Error: Line containing 'speaker3_language' is wrong or missing!"
     else
      runline=`expr $runline + 1`
    fi
  fi
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker4_language#[gej][0-3],*[gej]*[0-3]*$'`
  if test $nosp -ge 4
   then
    if test "$a" = ""
     then
      echo "Error: Line containing 'speaker4_language' is wrong or missing!"
     else
      runline=`expr $runline + 1`
    fi  
  fi
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker1_recmed_spec#[hncrmpwd]*$'`
if test "$a" = ""
 then
  echo "Error: Line containing 'speaker1_recmed_spec' is wrong or missing!"
 else
  runline=`expr $runline + 1`
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker2_recmed_spec#[hncrmpwd]*$'`
if test "$a" = ""
 then
  echo "Error: Line containing 'speaker2_recmed_spec' is wrong or missing!"
 else
  runline=`expr $runline + 1`
fi

if test $nosp -ge 3
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker3_recmed_spec#[hncrmpwd]*$'`
  if test "$a" = ""
   then
    echo "Error: Line containing 'speaker3_recmed_spec' is wrong or missing!"
   else
    runline=`expr $runline + 1`
  fi
fi

if test $nosp -ge 4
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker4_recmed_spec#[hncrmpwd]*$'`
  if test "$a" = ""
   then
    echo "Error: Line containing 'speaker4_recmed_spec' is wrong or missing"
   else
    runline=`expr $runline + 1`
  fi
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker1_micbrand#[-0-9a-zA-Z_][-0-9a-zA-Z_]*,* *[-0-9a-zA-Z_][-0-9a-zA-Z_]*$'`
if test "$a" != ""
 then
  runline=`expr $runline + 1`
  if test "$site" = "LMU"
   then 
    a=`echo $a | sed 's/speaker1_micbrand#//'`
    if test "$a" != "beyer_dynamic_mce_10, beyer_dynamic_nem_191"
     then
      echo "Error: Entry for 'speaker1_micbrand' is not 'beyer_dynamic_mce_10, beyer_dynamic_nem_191'! Perhaps recording without room microphon?"
    fi
  fi
 else 
  echo "Error: line containing 'speaker1_micbrand' is wrong or missing!"
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker2_micbrand#[-0-9a-zA-Z_][-0-9a-zA-Z_]*,* *[-0-9a-zA-Z_][-0-9a-zA-Z_]*$'`
if test "$a" != ""
 then
  runline=`expr $runline + 1`
  if test "$site" = "LMU"
   then 
    a=`echo $a | sed 's/speaker2_micbrand#//'`
    if test "$a" != "beyer_dynamic_mce_10, beyer_dynamic_nem_191"
     then
      echo "Error: Entry for 'speaker2_micbrand' is not 'beyer_dynamic_mce_10, beyer_dynamic_nem_191'! Perhaps recording without room microphon?"
    fi
  fi
 else 
  echo "Error: line containing 'speaker2_micbrand' is wrong or missing!"
fi

if test $nosp -ge 3
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker3_micbrand#[-0-9a-zA-Z_][-0-9a-zA-Z_]*,* *[-0-9a-zA-Z_][-0-9a-zA-Z_]*$'`
  if test "$a" != ""
   then
    runline=`expr $runline + 1`
    if test "$site" = "LMU"
     then 
      a=`echo $a | sed 's/speaker3_micbrand#//'`
      if test "$a" != "beyer_dynamic_mce_10, beyer_dynamic_nem_191"
       then
        echo "Error: Entry for 'speaker3_micbrand' is not 'beyer_dynamic_mce_10, beyer_dynamic_nem_191'! Perhaps recording without room microphon?"
      fi
    fi
   else 
    echo "Error: line containing 'speaker3_micbrand' is wrong or missing!"
  fi
fi

if test $nosp -ge 4
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^speaker4_micbrand#[-0-9a-zA-Z_][-0-9a-zA-Z_]*,* *[-0-9a-zA-Z_][-0-9a-zA-Z_]*$'`
  if test "$a" != ""
   then
    runline=`expr $runline + 1`
    if test "$site" = "LMU"
     then 
      a=`echo $a | sed 's/speaker4_micbrand#//'`
      if test "$a" != "beyer_dynamic_mce_10, beyer_dynamic_nem_191"
       then
        echo "Error: Entry for 'speaker4_micbrand' is not 'beyer_dynamic_mce_10, beyer_dynamic_nem_191'! Perhaps recording without room microphon?"
      fi
    fi
   else 
    echo "Error: line containing 'speaker4_micbrand' is wrong or missing!"
  fi
fi

a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^comments#.*$'`
if test "$a" = ""
 then
  a=`head -n $runline $i | tail -1 | tr '\011' '#' | egrep '^comments$'` 
  if test "$a" != ""
   then 
    echo "Error: If there is no entry for 'comment' eingetragen, then leave out entire line!" 
     runline=`expr $runline + 1`
  fi
 else 
  runline=`expr $runline + 1` 
fi 

if test $runline -ne $length
 then
  echo "A line is incorrect!"
fi

