#!/bin/sh

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

# Aufruf: VM_nist_check Filename       (Filename kann Pfad enthalten!)

# Funktion: berprft Nistheader auf Richtigkeit 

i=$1 

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

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

a=`cat $i | dd bs=1024 count=1 2> /dev/null | tr "\011" "#" | grep "#"`
if test "$a" != ""
 then echo "Error: The header should not contain the character 'TAB' or '#'!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep '     1 NIST_1A'`
if test "$a" != "     1 NIST_1A"
 then echo "Error: First line should contain 'NIST_1A'!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep '     2    1024'`
if test "$a" != "     2    1024"
 then echo "Fehler: Second line should contain '   1024'!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep '     3 database_id -s10 VERBMOBIL2'`
if test "$a" != "     3 database_id -s10 VERBMOBIL2"
 then echo "Error: Third line should contain 'database_id -s10 VERBMOBIL2'!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep '     4 database_version -s3 1.0'`
if test "$a" != "     4 database_version -s3 1.0"
 then echo "Error: fourth line should contain 'database_version -s3 1.0'!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null  | tr '\011' ' ' | grep '     5 scenario_language'`
if test "$a" = "     5 scenario_language -s6 german"
 then
  langu="g"
elif test "$a" = "     5 scenario_language -s12 multi_german"
 then 
  langu="m" 
elif test "$a" = "     5 scenario_language -s13 multi_english"
 then 
  langu="m"
elif test "$a" = "     5 scenario_language -s14 multi_japanese"
 then 
  langu="m"
elif test "$a" = "     5 scenario_language -s20 multi_english_german"
 then 
  langu="m"
elif test "$a" = "     5 scenario_language -s22 multi_english_japanese"
 then 
  langu="m"
elif test "$a" = "     5 scenario_language -s21 multi_german_japanese"
 then 
  langu="m"
elif test "$a" = "     5 scenario_language -s7 english"
 then
  langu="e"
elif test "$a" = "     5 scenario_language -s8 japanese"
 then
  langu="j"
 else echo "Error: Fifth line: wrong language entry or other error!"
fi
a=`echo $n | sed 's/\([a-z]\)[0-9][0-9][0-9][a-z][a-z][a-z][1234]\.[1a][6l]/\1/'`
if test "$a" != "$langu"
 then echo "Error: Filename and header entry for scenario_language in the header are different!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep '     6 scenario_id'`
if test "$a" = "     6 scenario_id -s4 main"
 then
  scenid="a"
elif test "$a" = "     6 scenario_id -s16 information_desk"
 then
  scenid="b"
 else echo "Error: Fifth line: wrong scenario Id entry or other error!"
fi
a=`echo $n | sed 's/[a-z][0-9][0-9][0-9]\([a-z]\)[a-z][a-z][1234]\.[1a][6l]/\1/'`
if test "$a" != "$scenid"
 then echo "Error: Filename and header entry scenario_id in the header are different!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep '     7 dialog_id'`
if test "$a" = ""
 then echo "Error: Seventh: line entry for dialog_id wrong!"
 else dianr=`echo $a |  sed 's/7 dialog_id -s3 \([0-9][0-9][0-9]\)/\1/'`
fi
a=`echo $n |  sed 's/[a-z]\([0-9][0-9][0-9]\)[a-z][a-z][a-z][1234]\.[1a][6l]/\1/'`
if test "$a" != "$dianr"
 then echo "Error: Filename und header entry for dialog_id in the header are different!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep '     8 speaker_id -s3 '`
if test "$a" = ""
 then echo "Error: Eighth line entry for speaker_id wrong!"
 else spk=`echo $a |  sed 's/8 speaker_id -s3 \([A-Z][A-Z][A-Z]\)/\1/'`
fi


a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep "     9 recording_site -s3 "`
if test "$a" = "     9 recording_site -s3 UBN"
 then
  rsite="UBN"
elif test  "$a" = "     9 recording_site -s3 LMU"
 then
  rsite="LMU"
elif test "$a" = "     9 recording_site -s3 CMU"
 then
  rsite="CMU"
elif test "$a" = "     9 recording_site -s3 ATR"
 then
  rsite="ATR"
elif test "$a" = "     9 recording_site -s3 UHH"
 then
  rsite="UHH"
 else echo "Error: Ninth line entry for recording_site wrong!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep "    10 recording_medium"`
if test "$a" = "    10 recording_medium -s4 room"
 then remed="r"
elif test "$a" = "    10 recording_medium -s5 close"
 then remed="c"
 elif test "$a" = "    10 recording_medium -s9 telephone"
  then remed="t"
  else echo "Error: Tenth line entry for recording_medium is wrong!"
fi

a=`cat -b $i | dd bs=1024 count=1 2> /dev/null | tr '\011' ' ' | grep "    11 recmed_spec"`
if test "$a" = "    11 recmed_spec -s6 analog" 
 then respc="p"
elif test "$a" = "    11 recmed_spec -s6 mobile"
 then respc="m"
elif test "$a" = "    11 recmed_spec -s8 wireless"
 then respc="w"
elif test "$a" = "    11 recmed_spec -s8 neckband"
 then respc="n"
elif test "$a" = "    11 recmed_spec -s4 dect"
 then respc="d"
elif test "$a" = "    11 recmed_spec -s7 headset"
 then respc="h"
elif test "$a" = "    11 recmed_spec -s4 clip"
 then respc="c"
elif test "$a" = "    11 recmed_spec -s4 room"
 then respc="r"
 else echo "Error: Eleventh line entry for recording_medium is wrong!"
fi
a=`echo $n | sed 's/[a-z][0-9][0-9][0-9][a-z]\([a-z][a-z]\)[1234]\.[1a][6l]/\1/'`
if test "$a" != "$remed$respc"
 then echo "Error: Filename und header entry for recmed_spec in the header are different or not allowed combination!"
fi

a=`cat -b $i | head -n 19 | tr '\011' ' ' | grep "    12 sample_coding"`
if test "$a" = "    12 sample_coding -s4 alaw"
 then sampco="t"
elif test "$a" = "    12 sample_coding -s3 pcm"
 then sampco="c"
elif test "$a" = "    12 sample_coding -s4 ulaw"
 then echo "Error? Twelfth line: entry for sample_coding is ulaw!"
 else echo "Error: Twelfth line: entry for sample_coding is wrong!"
fi

exten=`echo $n | sed 's/[a-z][0-9][0-9][0-9][a-z][a-z][a-z][1234]\.\([1a][6l]\)/\1/'`
if test "$exten" = "16"
 then
  if test "$sampco" != "c"
   then echo "Error: Filename and header entry for sample_coding do not correspond!"
  fi
elif test "$exten" = "al"
 then 
  if test "$sampco" != "t"
   then echo "Error: Filename and header entry for sample_coding do not correspond!"
  fi 
 else echo "Filename and header entry for sample_coding do not correspond!"
fi

if test "$sampco" = "$remed"
 then :
elif test "$sampco" != "c"
 then echo "Error: Filename and header entry for sample_coding do not correspond!"
elif test "$remed" = "r"
 then :
 else echo "Fehler: Filename and header entry for sample_coding do not correspond!"
fi

a=`cat -b $i | head -n 19 | tr '\011' ' ' | grep "    13 sample_n_bytes -i"`
if test "$a" = "    13 sample_n_bytes -i 2"
 then 
  if test "$exten" = "16"
   then snb=2
   else echo "Error: Filename and header entry for sample_n_bytes do not correspond!" 
  fi
elif test "$a" = "    13 sample_n_bytes -i 1"
 then
  if test "$exten" = "al"
   then snb=1
   else echo "Error: Filename and header entry for sample_n_bytes do not correspond!" 
  fi
 else echo "Error: Filename and header entry for sample_n_bytes do not correspond!"
fi
 
a=`cat -b $i | head -n 19 | tr '\011' ' ' | grep "    14 channel_count -i 1"`
if test "$a" != "    14 channel_count -i 1"
 then echo "Error: Fourteenth line header entry for channel_count is wrong!"
fi

a=`cat -b $i | head -n 19 | tr '\011' ' ' | egrep '^    15 sample_count -i [0-9]*$' | awk '{printf("%s\n", $4)}'` 
if test "$a" != ""
 then
  b=`ls -l $i | awk '{printf("%s\n", $5)}'`
  c=`expr $a \* $snb + 1024  - $b` 
 else
  c=1
fi
if test $c -ne 0
 then echo "Fehler: Fifteenth line header entry for sample_count and length of file do not correspond!"
fi

a=`cat -b $i | head -n 19 | tr '\011' ' ' | grep "    16 sample_byte_format"`
if test "$a" = "    16 sample_byte_format -s2 01"
 then 
  if test "$exten" = "16"
   then :
   else echo "Error: Filename and header entry for sample_byte_format do not correspond!" 
  fi
elif test "$a" = "    16 sample_byte_format -s1 1"
 then
  if test "$exten" = "al"
   then :
   else echo "Error: Filename and header entry for sample_byte_format do not correspond !" 
  fi
 else echo "Error: Filename and header entry for sample_byte_format do not correspond !"
fi

a=`cat -b $i | head -n 19 | tr '\011' ' ' | grep "    17 sample_rate"`
if test "$a" = "    17 sample_rate -i 16000"
 then 
 if test "$exten" = "16"
   then :
   else echo "Error: Filename and header entry for sample_rate do not correspond!" 
  fi
elif test "$a" = "    17 sample_rate -i 8000"
 then
  if test "$exten" = "al"
   then :
   else echo "Error: Filename and header entry for sample_rate do not correspond!" 
  fi
 else echo "Fehler: Seventeenth line: undefined entry for sample_rate !"
fi  

a=`cat -b $i | head -n 19 | tr '\011' ' ' | grep '^    18 scenario_date -s6 [09][7890123][01][0-9][0-3][0-9]$'`
if test "$a" = ""
 then 
  echo "Error: Eighteenth line: scenario_date wrong (YYMMDD) or File too old!"
 else
  date=`echo $a | sed 's/18 scenario_date -s6 [0-9][0-9]\([01][0-9]\)[0-3][0-9]/\1/'`
  if test $date -gt 12
   then 
    echo "Error: Eighteenth line should contain 'scenario_date TAB YYMMDD'!"
  fi
  date=`echo $a | sed 's/18 scenario_date -s6 [0-9][0-9][01][0-9]\([0-3][0-9]\)/\1/'`
  if test $date -gt 31
   then 
    echo "Error: Eighteenth line should contain 'scenario_date TAB YYMMDD'!"
  fi
fi

a=`cat -b $i | head -n 19 | tr '\011' ' ' | grep '^    19 end_head$'`
if test "$a" != "    19 end_head"
 then echo "Error: Nineteenth line end_head is missing!"
fi

l=`head -n 19 $i | wc -c`
ll=`expr 1024 - $l`
a=`cat $i | dd bs=1 skip=$l count=$ll  2> /dev/null | tr '\012' 'a' | tr ' ' '\012 ' | egrep '.'`
if test "$a" = ""
 then :
 else echo "Error: Not enough SPACES at the end of the header!"
fi

chnr=`echo $n | sed 's/[a-z][0-9][0-9][0-9][a-z][a-z][a-z]\([1234]\)\.[1a][6l]/\1/'`
echo $langu$dianr$scenid$remed$respc$chnr.$exten $spk $rsite




